-
Notifications
You must be signed in to change notification settings - Fork 34
/
config_flow.py
111 lines (98 loc) · 4.25 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from homeassistant import config_entries
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.network import get_url
from .constants import DOMAIN
from .nuki import NukiInterface
import logging
import voluptuous as vol
_LOGGER = logging.getLogger(__name__)
class OpenWrtConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_reauth(self, user_input):
return await self.async_step_user(user_input)
async def find_nuki_devices(self, config: dict):
nuki = NukiInterface(
self.hass,
bridge=config["address"],
token=config["token"],
web_token=config["web_token"]
)
title = None
if config["token"]:
try:
response = await nuki.bridge_list()
_LOGGER.debug(f"bridge devices: {response}")
title = response[0]["name"]
except Exception as err:
_LOGGER.exception(
f"Failed to get list of devices from bridge: {err}")
return title, "invalid_bridge_token"
if config["web_token"]:
try:
response = await nuki.web_list()
_LOGGER.debug(f"web devices: {response}")
if not title:
title = response[0]["name"]
except Exception as err:
_LOGGER.exception(
f"Failed to get list of devices from web API: {err}")
return title, "invalid_web_token"
return title, None
def _get_hass_url(self, hass):
try:
return get_url(hass)
except Exception as err:
_LOGGER.exception(f"Error getting HASS url: {err}")
return ""
async def async_step_user(self, user_input):
errors = None
_LOGGER.debug(f"Input: {user_input}")
if user_input is None:
nuki = NukiInterface(self.hass)
bridge_address = await nuki.discover_bridge()
hass_url = self._get_hass_url(self.hass)
user_input = {
"address": bridge_address,
"hass_url": hass_url
}
elif not user_input.get("token") and not user_input.get("web_token"):
errors = dict(base="no_token")
elif user_input.get("token") and not user_input.get("address"):
errors = dict(base="no_bridge_url")
elif user_input.get("token") or user_input.get("web_token"):
title, err = await self.find_nuki_devices(user_input)
if not err:
return self.async_create_entry(
title=user_input.get("name") or title,
data=user_input
)
errors = dict(base=err)
schema = vol.Schema({
vol.Optional("address", default=user_input.get("address", "")): cv.string,
vol.Optional("hass_url", default=user_input.get("hass_url", "")): cv.string,
vol.Optional("token", default=user_input.get("token", "")): cv.string,
vol.Optional("web_token", default=user_input.get("web_token", "")): cv.string,
vol.Optional("name", default=user_input.get("name", "")): cv.string,
vol.Required("update_seconds", default=user_input.get("update_seconds", 30)): vol.All(
cv.positive_int,
vol.Range(min=10, max=600)
),
})
return self.async_show_form(
step_id="user", data_schema=schema, errors=errors
)
# def async_get_options_flow(config_entry):
# return OptionsFlowHandler(config_entry)
class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry):
self.config_entry = config_entry
async def async_step_init(self, user_input=None):
data = self.config_entry.as_dict()["data"]
_LOGGER.debug(f"OptionsFlowHandler: {data} {self.config_entry}")
schema = vol.Schema({
vol.Required("hass_url", default=data.get("hass_url")): cv.string,
vol.Required("token", default=data.get("token")): cv.string,
vol.Optional("web_token", default=data.get("web_token")): cv.string,
})
return self.async_show_form(
step_id="options", data_schema=schema
)