/
config_flow.py
73 lines (55 loc) · 2.46 KB
/
config_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Adds config flow for GIOS."""
import asyncio
from aiohttp.client_exceptions import ClientConnectorError
from async_timeout import timeout
from gios import ApiError, Gios, NoStationError
import voluptuous as vol
from homeassistant import config_entries, exceptions
from homeassistant.const import CONF_NAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import CONF_STATION_ID, DEFAULT_NAME, DOMAIN # pylint:disable=unused-import
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_STATION_ID): int,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
}
)
class GiosFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for GIOS."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
errors = {}
if user_input is not None:
try:
already_configured = self._async_current_ids()
if user_input[CONF_STATION_ID] in already_configured:
raise StationIdExists()
websession = async_get_clientsession(self.hass)
with timeout(30):
gios = Gios(user_input[CONF_STATION_ID], websession)
await gios.update()
if not gios.available:
raise InvalidSensorsData()
await self.async_set_unique_id(
user_input[CONF_STATION_ID], raise_on_progress=False
)
return self.async_create_entry(
title=user_input[CONF_STATION_ID], data=user_input,
)
except StationIdExists:
errors[CONF_STATION_ID] = "station_id_exists"
except (ApiError, ClientConnectorError, asyncio.TimeoutError):
errors["base"] = "cannot_connect"
except NoStationError:
errors[CONF_STATION_ID] = "wrong_station_id"
except InvalidSensorsData:
errors[CONF_STATION_ID] = "invalid_sensors_data"
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)
class InvalidSensorsData(exceptions.HomeAssistantError):
"""Error to indicate invalid sensors data."""
class StationIdExists(exceptions.HomeAssistantError):
"""Error to indicate that station with station_id is already configured."""