1 """The unifiprotect integration discovery."""
3 from __future__
import annotations
5 from dataclasses
import asdict
6 from datetime
import timedelta
10 from unifi_discovery
import AIOUnifiScanner, UnifiDevice, UnifiService
12 from homeassistant
import config_entries
17 from .const
import DOMAIN
19 _LOGGER = logging.getLogger(__name__)
21 DISCOVERY =
"discovery"
27 """Start discovery."""
28 domain_data = hass.data.setdefault(DOMAIN, {})
29 if DISCOVERY
in domain_data:
31 domain_data[DISCOVERY] =
True
33 async
def _async_discovery() -> None:
37 def _async_start_background_discovery(*_: Any) ->
None:
38 """Run discovery in the background."""
39 hass.async_create_background_task(_async_discovery(),
"unifiprotect-discovery")
42 _async_start_background_discovery()
45 _async_start_background_discovery,
47 cancel_on_shutdown=
True,
52 """Discover devices."""
53 scanner = AIOUnifiScanner()
54 devices = await scanner.async_scan()
55 _LOGGER.debug(
"Found devices: %s", devices)
62 discovered_devices: list[UnifiDevice],
64 """Trigger config flows for discovered devices."""
65 for device
in discovered_devices:
66 if device.services[UnifiService.Protect]
and device.hw_addr:
67 discovery_flow.async_create_flow(
70 context={
"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
None async_trigger_discovery(HomeAssistant hass, list[UnifiDevice] discovered_devices)
list[UnifiDevice] async_discover_devices()
None async_start_discovery(HomeAssistant hass)
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)