forked from konradb3/homeassistant_salus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_flow.py
64 lines (50 loc) · 2.1 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
"""Config flow to configure Salus iT600 gateway."""
import logging
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
from pyit600.exceptions import IT600AuthenticationError, IT600ConnectionError
from pyit600.gateway import IT600Gateway
# pylint: disable=unused-import
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
CONF_FLOW_TYPE = "config_flow_device"
CONF_USER = "user"
DEFAULT_GATEWAY_NAME = "Salus iT600 Gateway"
GATEWAY_SETTINGS = {
vol.Required(CONF_HOST): str,
vol.Required(CONF_TOKEN): vol.All(str, vol.Length(min=16, max=16)),
vol.Optional(CONF_NAME, default=DEFAULT_GATEWAY_NAME): str,
}
class SalusFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a Salus config flow."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user to configure a gateway."""
errors = {}
if user_input is not None:
token = user_input[CONF_TOKEN]
host = user_input[CONF_HOST]
# Try to connect to a Salus Gateway.
gateway = IT600Gateway(host=host, euid=token)
try:
unique_id = await gateway.connect()
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=user_input[CONF_NAME],
data={
CONF_FLOW_TYPE: CONF_USER,
CONF_HOST: host,
CONF_TOKEN: token,
"model": "xxx",
"mac": unique_id,
},
)
except IT600ConnectionError:
errors["base"] = "connect_error"
except IT600AuthenticationError:
errors["base"] = "auth_error"
schema = vol.Schema(GATEWAY_SETTINGS)
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)