Home Assistant Unofficial Reference 2024.12.1
discovery.py
Go to the documentation of this file.
1 """The lifx integration discovery."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from collections.abc import Collection, Iterable
7 
8 from aiolifx.aiolifx import LifxDiscovery, Light, ScanManager
9 
10 from homeassistant import config_entries
11 from homeassistant.components import network
12 from homeassistant.const import CONF_HOST
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers import discovery_flow
15 
16 from .const import CONF_SERIAL, DOMAIN
17 
18 DEFAULT_TIMEOUT = 8.5
19 
20 
21 async def async_discover_devices(hass: HomeAssistant) -> Collection[Light]:
22  """Discover lifx devices."""
23  all_lights: dict[str, Light] = {}
24  broadcast_addrs = await network.async_get_ipv4_broadcast_addresses(hass)
25  discoveries = []
26  for address in broadcast_addrs:
27  manager = ScanManager(str(address))
28  lifx_discovery = LifxDiscovery(hass.loop, manager, broadcast_ip=str(address))
29  discoveries.append(lifx_discovery)
30  lifx_discovery.start()
31 
32  await asyncio.sleep(DEFAULT_TIMEOUT)
33  for discovery in discoveries:
34  all_lights.update(discovery.lights)
35  discovery.cleanup()
36 
37  return all_lights.values()
38 
39 
40 @callback
41 def async_init_discovery_flow(hass: HomeAssistant, host: str, serial: str) -> None:
42  """Start discovery of devices."""
43  discovery_flow.async_create_flow(
44  hass,
45  DOMAIN,
46  context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
47  data={CONF_HOST: host, CONF_SERIAL: serial},
48  )
49 
50 
51 @callback
53  hass: HomeAssistant,
54  discovered_devices: Iterable[Light],
55 ) -> None:
56  """Trigger config flows for discovered devices."""
57  for device in discovered_devices:
58  # device.mac_addr is not the mac_address, its the serial number
59  async_init_discovery_flow(hass, device.ip_addr, device.mac_addr)
None async_trigger_discovery(HomeAssistant hass, Iterable[Light] discovered_devices)
Definition: discovery.py:55
None async_init_discovery_flow(HomeAssistant hass, str host, str serial)
Definition: discovery.py:41
Collection[Light] async_discover_devices(HomeAssistant hass)
Definition: discovery.py:21