-
-
Notifications
You must be signed in to change notification settings - Fork 32k
/
Copy pathconfig_flow.py
442 lines (397 loc) · 16.9 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""Config flow for ONVIF."""
from __future__ import annotations
from collections.abc import Mapping
from pprint import pformat
from typing import Any
from urllib.parse import urlparse
from onvif.util import is_auth_error, stringify_onvif_error
import voluptuous as vol
from wsdiscovery.discovery import ThreadedWSDiscovery as WSDiscovery
from wsdiscovery.scope import Scope
from wsdiscovery.service import Service
from zeep.exceptions import Fault
from homeassistant import config_entries
from homeassistant.components import dhcp
from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS
from homeassistant.components.stream import (
CONF_RTSP_TRANSPORT,
CONF_USE_WALLCLOCK_AS_TIMESTAMPS,
RTSP_TRANSPORTS,
)
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_USERNAME,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import AbortFlow, FlowResult
from homeassistant.helpers import device_registry as dr
from .const import (
CONF_DEVICE_ID,
CONF_ENABLE_WEBHOOKS,
CONF_HARDWARE,
DEFAULT_ARGUMENTS,
DEFAULT_ENABLE_WEBHOOKS,
DEFAULT_PORT,
DOMAIN,
GET_CAPABILITIES_EXCEPTIONS,
LOGGER,
)
from .device import get_device
CONF_MANUAL_INPUT = "Manually configure ONVIF device"
def wsdiscovery() -> list[Service]:
"""Get ONVIF Profile S devices from network."""
discovery = WSDiscovery(ttl=4)
try:
discovery.start()
return discovery.searchServices(
scopes=[Scope("onvif://www.onvif.org/Profile/Streaming")]
)
finally:
discovery.stop()
# Stop the threads started by WSDiscovery since otherwise there is a leak.
discovery._stopThreads() # pylint: disable=protected-access
async def async_discovery(hass: HomeAssistant) -> list[dict[str, Any]]:
"""Return if there are devices that can be discovered."""
LOGGER.debug("Starting ONVIF discovery")
services = await hass.async_add_executor_job(wsdiscovery)
devices = []
for service in services:
url = urlparse(service.getXAddrs()[0])
device = {
CONF_DEVICE_ID: None,
CONF_NAME: service.getEPR(),
CONF_HOST: url.hostname,
CONF_PORT: url.port or 80,
CONF_HARDWARE: None,
}
for scope in service.getScopes():
scope_str = scope.getValue()
if scope_str.lower().startswith("onvif://www.onvif.org/name"):
device[CONF_NAME] = scope_str.split("/")[-1]
if scope_str.lower().startswith("onvif://www.onvif.org/hardware"):
device[CONF_HARDWARE] = scope_str.split("/")[-1]
if scope_str.lower().startswith("onvif://www.onvif.org/mac"):
device[CONF_DEVICE_ID] = scope_str.split("/")[-1]
devices.append(device)
return devices
class OnvifFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a ONVIF config flow."""
VERSION = 1
_reauth_entry: config_entries.ConfigEntry
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> OnvifOptionsFlowHandler:
"""Get the options flow for this handler."""
return OnvifOptionsFlowHandler(config_entry)
def __init__(self):
"""Initialize the ONVIF config flow."""
self.device_id = None
self.devices = []
self.onvif_config = {}
async def async_step_user(self, user_input=None):
"""Handle user flow."""
if user_input:
if user_input["auto"]:
return await self.async_step_device()
return await self.async_step_configure()
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({vol.Required("auto", default=True): bool}),
)
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
"""Handle re-authentication of an existing config entry."""
reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
assert reauth_entry is not None
self._reauth_entry = reauth_entry
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Confirm reauth."""
entry = self._reauth_entry
errors: dict[str, str] | None = {}
description_placeholders: dict[str, str] | None = None
if user_input is not None:
entry_data = entry.data
self.onvif_config = entry_data | user_input
errors, description_placeholders = await self.async_setup_profiles(
configure_unique_id=False
)
if not errors:
hass = self.hass
entry_id = entry.entry_id
hass.config_entries.async_update_entry(entry, data=self.onvif_config)
hass.async_create_task(hass.config_entries.async_reload(entry_id))
return self.async_abort(reason="reauth_successful")
username = (user_input or {}).get(CONF_USERNAME) or entry.data[CONF_USERNAME]
return self.async_show_form(
step_id="reauth_confirm",
data_schema=vol.Schema(
{
vol.Required(CONF_USERNAME, default=username): str,
vol.Required(CONF_PASSWORD): str,
}
),
errors=errors,
description_placeholders=description_placeholders,
)
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle dhcp discovery."""
hass = self.hass
mac = discovery_info.macaddress
registry = dr.async_get(self.hass)
if not (
device := registry.async_get_device(
identifiers=set(), connections={(dr.CONNECTION_NETWORK_MAC, mac)}
)
):
return self.async_abort(reason="no_devices_found")
for entry_id in device.config_entries:
if (
not (entry := hass.config_entries.async_get_entry(entry_id))
or entry.domain != DOMAIN
or entry.state is config_entries.ConfigEntryState.LOADED
):
continue
if hass.config_entries.async_update_entry(
entry, data=entry.data | {CONF_HOST: discovery_info.ip}
):
hass.async_create_task(self.hass.config_entries.async_reload(entry_id))
return self.async_abort(reason="already_configured")
async def async_step_device(self, user_input=None):
"""Handle WS-Discovery.
Let user choose between discovered devices and manual configuration.
If no device is found allow user to manually input configuration.
"""
if user_input:
if user_input[CONF_HOST] == CONF_MANUAL_INPUT:
return await self.async_step_configure()
for device in self.devices:
if device[CONF_HOST] == user_input[CONF_HOST]:
self.device_id = device[CONF_DEVICE_ID]
self.onvif_config = {
CONF_NAME: device[CONF_NAME],
CONF_HOST: device[CONF_HOST],
CONF_PORT: device[CONF_PORT],
}
return await self.async_step_configure()
discovery = await async_discovery(self.hass)
for device in discovery:
configured = any(
entry.unique_id == device[CONF_DEVICE_ID]
for entry in self._async_current_entries()
)
if not configured:
self.devices.append(device)
LOGGER.debug("Discovered ONVIF devices %s", pformat(self.devices))
if self.devices:
devices = {CONF_MANUAL_INPUT: CONF_MANUAL_INPUT}
for device in self.devices:
description = f"{device[CONF_NAME]} ({device[CONF_HOST]})"
if hardware := device[CONF_HARDWARE]:
description += f" [{hardware}]"
devices[device[CONF_HOST]] = description
return self.async_show_form(
step_id="device",
data_schema=vol.Schema({vol.Optional(CONF_HOST): vol.In(devices)}),
)
return await self.async_step_configure()
async def async_step_configure(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Device configuration."""
errors: dict[str, str] = {}
description_placeholders: dict[str, str] = {}
if user_input:
self.onvif_config = user_input
errors, description_placeholders = await self.async_setup_profiles()
if not errors:
title = f"{self.onvif_config[CONF_NAME]} - {self.device_id}"
return self.async_create_entry(title=title, data=self.onvif_config)
def conf(name, default=None):
return self.onvif_config.get(name, default)
# Username and Password are optional and default empty
# due to some cameras not allowing you to change ONVIF user settings.
# See https://github.com/home-assistant/core/issues/39182
# and https://github.com/home-assistant/core/issues/35904
return self.async_show_form(
step_id="configure",
data_schema=vol.Schema(
{
vol.Required(CONF_NAME, default=conf(CONF_NAME)): str,
vol.Required(CONF_HOST, default=conf(CONF_HOST)): str,
vol.Required(CONF_PORT, default=conf(CONF_PORT, DEFAULT_PORT)): int,
vol.Optional(CONF_USERNAME, default=conf(CONF_USERNAME, "")): str,
vol.Optional(CONF_PASSWORD, default=conf(CONF_PASSWORD, "")): str,
}
),
errors=errors,
description_placeholders=description_placeholders,
)
async def async_setup_profiles(
self, configure_unique_id: bool = True
) -> tuple[dict[str, str], dict[str, str]]:
"""Fetch ONVIF device profiles."""
LOGGER.debug(
"Fetching profiles from ONVIF device %s", pformat(self.onvif_config)
)
device = get_device(
self.hass,
self.onvif_config[CONF_HOST],
self.onvif_config[CONF_PORT],
self.onvif_config[CONF_USERNAME],
self.onvif_config[CONF_PASSWORD],
)
try:
await device.update_xaddrs()
device_mgmt = await device.create_devicemgmt_service()
# Get the MAC address to use as the unique ID for the config flow
if not self.device_id:
try:
network_interfaces = await device_mgmt.GetNetworkInterfaces()
interface = next(
filter(lambda interface: interface.Enabled, network_interfaces),
None,
)
if interface:
self.device_id = interface.Info.HwAddress
except Fault as fault:
if "not implemented" not in fault.message:
raise fault
LOGGER.debug(
"%s: Could not get network interfaces: %s",
self.onvif_config[CONF_NAME],
stringify_onvif_error(fault),
)
# If no network interfaces are exposed, fallback to serial number
if not self.device_id:
device_info = await device_mgmt.GetDeviceInformation()
self.device_id = device_info.SerialNumber
if not self.device_id:
raise AbortFlow(reason="no_mac")
if configure_unique_id:
await self.async_set_unique_id(self.device_id, raise_on_progress=False)
self._abort_if_unique_id_configured(
updates={
CONF_HOST: self.onvif_config[CONF_HOST],
CONF_PORT: self.onvif_config[CONF_PORT],
CONF_NAME: self.onvif_config[CONF_NAME],
CONF_USERNAME: self.onvif_config[CONF_USERNAME],
CONF_PASSWORD: self.onvif_config[CONF_PASSWORD],
}
)
# Verify there is an H264 profile
media_service = await device.create_media_service()
profiles = await media_service.GetProfiles()
except AttributeError: # Likely an empty document or 404 from the wrong port
LOGGER.debug(
"%s: No ONVIF service found at %s:%s",
self.onvif_config[CONF_NAME],
self.onvif_config[CONF_HOST],
self.onvif_config[CONF_PORT],
exc_info=True,
)
return {CONF_PORT: "no_onvif_service"}, {}
except Fault as err:
stringified_error = stringify_onvif_error(err)
description_placeholders = {"error": stringified_error}
if is_auth_error(err):
LOGGER.debug(
"%s: Could not authenticate with camera: %s",
self.onvif_config[CONF_NAME],
stringified_error,
)
return {CONF_PASSWORD: "auth_failed"}, description_placeholders
LOGGER.debug(
"%s: Could not determine camera capabilities: %s",
self.onvif_config[CONF_NAME],
stringified_error,
exc_info=True,
)
return {"base": "onvif_error"}, description_placeholders
except GET_CAPABILITIES_EXCEPTIONS as err:
LOGGER.debug(
"%s: Could not determine camera capabilities: %s",
self.onvif_config[CONF_NAME],
stringify_onvif_error(err),
exc_info=True,
)
return {"base": "onvif_error"}, {"error": stringify_onvif_error(err)}
else:
if not any(
profile.VideoEncoderConfiguration
and profile.VideoEncoderConfiguration.Encoding == "H264"
for profile in profiles
):
raise AbortFlow(reason="no_h264")
return {}, {}
finally:
await device.close()
class OnvifOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle ONVIF options."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize ONVIF options flow."""
self.config_entry = config_entry
self.options = dict(config_entry.options)
async def async_step_init(self, user_input=None):
"""Manage the ONVIF options."""
return await self.async_step_onvif_devices()
async def async_step_onvif_devices(self, user_input=None):
"""Manage the ONVIF devices options."""
if user_input is not None:
self.options[CONF_EXTRA_ARGUMENTS] = user_input[CONF_EXTRA_ARGUMENTS]
self.options[CONF_RTSP_TRANSPORT] = user_input[CONF_RTSP_TRANSPORT]
self.options[CONF_USE_WALLCLOCK_AS_TIMESTAMPS] = user_input.get(
CONF_USE_WALLCLOCK_AS_TIMESTAMPS,
self.config_entry.options.get(CONF_USE_WALLCLOCK_AS_TIMESTAMPS, False),
)
self.options[CONF_ENABLE_WEBHOOKS] = user_input.get(
CONF_ENABLE_WEBHOOKS,
self.config_entry.options.get(
CONF_ENABLE_WEBHOOKS, DEFAULT_ENABLE_WEBHOOKS
),
)
return self.async_create_entry(title="", data=self.options)
advanced_options = {}
if self.show_advanced_options:
advanced_options[
vol.Optional(
CONF_USE_WALLCLOCK_AS_TIMESTAMPS,
default=self.config_entry.options.get(
CONF_USE_WALLCLOCK_AS_TIMESTAMPS, False
),
)
] = bool
return self.async_show_form(
step_id="onvif_devices",
data_schema=vol.Schema(
{
vol.Optional(
CONF_EXTRA_ARGUMENTS,
default=self.config_entry.options.get(
CONF_EXTRA_ARGUMENTS, DEFAULT_ARGUMENTS
),
): str,
vol.Optional(
CONF_RTSP_TRANSPORT,
default=self.config_entry.options.get(
CONF_RTSP_TRANSPORT, next(iter(RTSP_TRANSPORTS))
),
): vol.In(RTSP_TRANSPORTS),
vol.Optional(
CONF_ENABLE_WEBHOOKS,
default=self.config_entry.options.get(
CONF_ENABLE_WEBHOOKS, DEFAULT_ENABLE_WEBHOOKS
),
): bool,
**advanced_options,
}
),
)