-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathconfig_flow.py
executable file
·358 lines (319 loc) · 13.8 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""Config flow for tapo integration."""
import dataclasses
import logging
from typing import Any
from typing import Optional
import aiohttp
import voluptuous as vol
from homeassistant import config_entries
from homeassistant import data_entry_flow
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_SCAN_INTERVAL
from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.typing import DiscoveryInfoType
from plugp100.common.credentials import AuthCredential
from plugp100.discovery.discovered_device import DiscoveredDevice
from plugp100.new.device_factory import DeviceConnectConfiguration, connect
from plugp100.new.tapodevice import TapoDevice
from plugp100.responses.tapo_exception import TapoError
from plugp100.responses.tapo_exception import TapoException
from custom_components.tapo.const import CONF_ADVANCED_SETTINGS
from custom_components.tapo.const import CONF_DISCOVERED_DEVICE_INFO
from custom_components.tapo.const import CONF_HOST
from custom_components.tapo.const import CONF_MAC
from custom_components.tapo.const import CONF_PASSWORD
from custom_components.tapo.const import CONF_USERNAME
from custom_components.tapo.const import DEFAULT_POLLING_RATE_S
from custom_components.tapo.const import DOMAIN
from custom_components.tapo.const import STEP_ADVANCED_SETTINGS
from custom_components.tapo.const import STEP_DISCOVERY_REQUIRE_AUTH
from custom_components.tapo.const import STEP_INIT
from custom_components.tapo.discovery import discover_tapo_device
from custom_components.tapo.errors import CannotConnect
from custom_components.tapo.errors import InvalidAuth
from custom_components.tapo.errors import InvalidHost
from custom_components.tapo.setup_helpers import get_host_port, create_aiohttp_session
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(
CONF_HOST,
description="The IP address of your tapo device (must be static)",
): str,
vol.Required(
CONF_USERNAME, description="The username used with Tapo App, so your email"
): str,
vol.Required(CONF_PASSWORD, description="The password used with Tapo App"): str,
}
)
STEP_AUTH_DATA_SCHEMA = vol.Schema(
{
vol.Required(
CONF_USERNAME, description="The username used with Tapo App, so your email"
): str,
vol.Required(CONF_PASSWORD, description="The password used with Tapo App"): str,
}
)
STEP_ADVANCED_CONFIGURATION = vol.Schema(
{
vol.Optional(
CONF_SCAN_INTERVAL,
description="Polling rate in seconds (e.g. 0.5 seconds means 500ms)",
default=DEFAULT_POLLING_RATE_S,
): vol.All(vol.Coerce(float), vol.Clamp(min=0)),
}
)
def step_options(entry: config_entries.ConfigEntry) -> vol.Schema:
return vol.Schema(
{
vol.Required(
CONF_HOST,
description="The IP address of your tapo device (must be static)",
default=entry.data.get(CONF_HOST),
): str,
vol.Optional(
CONF_SCAN_INTERVAL,
description="Polling rate in seconds (e.g. 0.5 seconds means 500ms)",
default=entry.data.get(CONF_SCAN_INTERVAL, DEFAULT_POLLING_RATE_S),
): vol.All(vol.Coerce(float), vol.Clamp(min=1)),
}
)
@dataclasses.dataclass(frozen=False)
class FirstStepData:
device: Optional[TapoDevice]
user_input: Optional[dict[str, Any]]
@config_entries.HANDLERS.register(DOMAIN)
class TapoConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for tapo."""
VERSION = 5
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
def __init__(self) -> None:
super().__init__()
self.first_step_data: Optional[FirstStepData] = None
self._discovered_info: DiscoveredDevice | None = None
async def async_step_dhcp(
self, discovery_info: DhcpServiceInfo
) -> data_entry_flow.FlowResult:
"""Handle discovery via dhcp."""
mac_address = dr.format_mac(discovery_info.macaddress)
if discovered_device := await discover_tapo_device(discovery_info.ip):
return await self._async_handle_discovery(
discovery_info.ip, mac_address, discovered_device
)
async def async_step_integration_discovery(
self, discovery_info: DiscoveryInfoType
) -> data_entry_flow.FlowResult:
"""Handle integration discovery."""
discovered_device = DiscoveredDevice.from_dict(discovery_info[CONF_DISCOVERED_DEVICE_INFO])
return await self._async_handle_discovery(
discovery_info[CONF_HOST],
discovery_info[CONF_MAC],
discovered_device,
)
async def async_step_user(
self, user_input: Optional[dict[str, Any]] = None
) -> data_entry_flow.FlowResult:
"""Handle the initial step."""
self.hass.data.setdefault(DOMAIN, {})
errors = {}
if user_input is not None:
try:
device = await self._async_get_device(user_input)
await self.async_set_unique_id(dr.format_mac(device.mac))
self._abort_if_unique_id_configured()
self._async_abort_entries_match({CONF_HOST: device.host})
if user_input.get(CONF_ADVANCED_SETTINGS, False):
self.first_step_data = FirstStepData(device, user_input)
return await self.async_step_advanced_config()
else:
return await self._async_create_config_entry_from_device_info(
device, user_input
)
except InvalidAuth as error:
errors["base"] = "invalid_auth"
_LOGGER.exception("Failed to setup, invalid auth %s", str(error))
except CannotConnect as error:
errors["base"] = "cannot_connect"
_LOGGER.exception("Failed to setup cannot connect %s", str(error))
except InvalidHost as error:
errors["base"] = "invalid_hostname"
_LOGGER.exception("Failed to setup invalid host %s", str(error))
except data_entry_flow.AbortFlow:
return self.async_abort(reason="already_configured")
except Exception as error: # pylint: disable=broad-except
errors["base"] = "unknown"
_LOGGER.exception("Failed to setup %s", str(error), exc_info=True)
return self.async_show_form(
step_id=STEP_INIT, data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
return OptionsFlowHandler(config_entry)
async def async_step_advanced_config(
self, user_input: Optional[dict[str, Any]] = None
) -> data_entry_flow.FlowResult:
errors = {}
if user_input is not None:
polling_rate = user_input.get(CONF_SCAN_INTERVAL, DEFAULT_POLLING_RATE_S)
return self.async_create_entry(
title=self.first_step_data.device.nickname,
data=self.first_step_data.user_input
| {CONF_SCAN_INTERVAL: polling_rate},
)
else:
return self.async_show_form(
step_id=STEP_ADVANCED_SETTINGS,
data_schema=STEP_ADVANCED_CONFIGURATION,
errors=errors,
)
async def _async_handle_discovery(
self,
host: str,
mac_address: str,
discovered_device: DiscoveredDevice,
) -> data_entry_flow.FlowResult:
self._discovered_info = discovered_device
existing_entry = await self.async_set_unique_id(
mac_address, raise_on_progress=False
)
if existing_entry:
if result := self._recover_config_on_entry_error(
existing_entry, discovered_device.ip
):
return result
self._abort_if_unique_id_configured(updates={CONF_HOST: host})
self._async_abort_entries_match({CONF_HOST: host})
if is_supported_device(discovered_device):
return await self.async_step_discovery_auth_confirm()
else:
return self.async_abort(reason="Device not supported")
async def async_step_discovery_auth_confirm(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
assert self._discovered_info is not None
errors = {}
if user_input:
try:
device = await self._async_get_device_from_discovered(
self._discovered_info, user_input
)
await self.async_set_unique_id(dr.format_mac(device.mac))
self._abort_if_unique_id_configured()
except InvalidAuth as error:
errors["base"] = "invalid_auth"
_LOGGER.exception("Failed to setup, invalid auth %s", str(error))
except CannotConnect as error:
errors["base"] = "cannot_connect"
_LOGGER.exception("Failed to setup cannot connect %s", str(error))
except InvalidHost as error:
errors["base"] = "invalid_hostname"
_LOGGER.exception("Failed to setup invalid host %s", str(error))
else:
return await self._async_create_config_entry_from_device_info(
device, user_input
)
discovery_data = {
"name": self._discovered_info.device_model,
"mac": self._discovered_info.mac.replace("-", "")[:5],
"host": self._discovered_info.ip,
}
self.context.update({"title_placeholders": discovery_data})
return self.async_show_form(
step_id=STEP_DISCOVERY_REQUIRE_AUTH,
data_schema=STEP_AUTH_DATA_SCHEMA,
errors=errors,
description_placeholders=discovery_data,
)
@callback
def _recover_config_on_entry_error(
self, entry: ConfigEntry, host: str
) -> data_entry_flow.FlowResult | None:
if entry.state not in (
ConfigEntryState.SETUP_ERROR,
ConfigEntryState.SETUP_RETRY,
):
return None
if entry.data[CONF_HOST] != host:
return self.async_update_reload_and_abort(
entry, data={**entry.data, CONF_HOST: host}, reason="already_configured"
)
return None
async def _async_create_config_entry_from_device_info(
self, device: TapoDevice, options: dict[str, Any]
):
return self.async_create_entry(
title=device.nickname,
data=options
| {
CONF_HOST: device.host,
CONF_MAC: device.mac,
CONF_SCAN_INTERVAL: DEFAULT_POLLING_RATE_S,
CONF_DISCOVERED_DEVICE_INFO: self._discovered_info.as_dict if self._discovered_info is not None else None
},
)
async def _async_get_device_from_discovered(
self, discovered: DiscoveredDevice, config: dict[str, Any]
) -> TapoDevice:
return await self._async_get_device(config | {CONF_HOST: discovered.ip}, discovered)
async def _async_get_device(
self,
config: dict[str, Any],
discovered_device: DiscoveredDevice | None = None,
) -> TapoDevice:
if not config[CONF_HOST]:
raise InvalidHost
try:
session = create_aiohttp_session(self.hass)
credential = AuthCredential(config[CONF_USERNAME], config[CONF_PASSWORD])
if discovered_device is None:
host, port = get_host_port(config[CONF_HOST])
config = DeviceConnectConfiguration(
credentials=credential,
host=host,
port=port,
)
device = await connect(config=config, session=session)
else:
device = await discovered_device.get_tapo_device(credential, session)
await device.update()
return device
except TapoException as error:
self._raise_from_tapo_exception(error)
except (aiohttp.ClientError, Exception) as error:
raise CannotConnect from error
def _raise_from_tapo_exception(self, exception: TapoException):
_LOGGER.error("Tapo exception %s", str(exception.error_code))
if exception.error_code == TapoError.INVALID_CREDENTIAL.value:
raise InvalidAuth from exception
else:
raise CannotConnect from exception
class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
"""Manage the options."""
if user_input is not None:
self.hass.config_entries.async_update_entry(
self.config_entry, data=self.config_entry.data | user_input
)
return self.async_create_entry(title="", data={})
return self.async_show_form(
step_id="init",
data_schema=step_options(self.config_entry),
)
def is_supported_device(discovered_device: DiscoveredDevice) -> bool:
model = discovered_device.device_model.lower()
kind = discovered_device.device_type.upper()
if kind == "SMART.IPCAMERA" or "CAMERA" in kind:
_LOGGER.info("Found device model: %s, but type not supported %s", model, kind)
return False
return True