Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for SONOS."""
2 
3 from collections.abc import Awaitable
4 import dataclasses
5 
6 from homeassistant.components import ssdp, zeroconf
7 from homeassistant.config_entries import ConfigFlowResult
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.config_entry_flow import DiscoveryFlowHandler
10 
11 from .const import DATA_SONOS_DISCOVERY_MANAGER, DOMAIN, UPNP_ST
12 from .helpers import hostname_to_uid
13 
14 
15 async def _async_has_devices(hass: HomeAssistant) -> bool:
16  """Return if Sonos devices have been seen recently with SSDP."""
17  return bool(await ssdp.async_get_discovery_info_by_st(hass, UPNP_ST))
18 
19 
20 class SonosDiscoveryFlowHandler(DiscoveryFlowHandler[Awaitable[bool]], domain=DOMAIN):
21  """Sonos discovery flow that callsback zeroconf updates."""
22 
23  def __init__(self) -> None:
24  """Init discovery flow."""
25  super().__init__(DOMAIN, "Sonos", _async_has_devices)
26 
28  self, discovery_info: zeroconf.ZeroconfServiceInfo
29  ) -> ConfigFlowResult:
30  """Handle a flow initialized by zeroconf."""
31  hostname = discovery_info.hostname
32  if hostname is None or not hostname.lower().startswith("sonos"):
33  return self.async_abort(reason="not_sonos_device")
34  await self.async_set_unique_id(self._domain, raise_on_progress=False)
35  host = discovery_info.host
36  mdns_name = discovery_info.name
37  properties = discovery_info.properties
38  boot_seqnum = properties.get("bootseq")
39  model = properties.get("model")
40  uid = hostname_to_uid(hostname)
41  if discovery_manager := self.hass.data.get(DATA_SONOS_DISCOVERY_MANAGER):
42  discovery_manager.async_discovered_player(
43  "Zeroconf", properties, host, uid, boot_seqnum, model, mdns_name
44  )
45  return await self.async_step_discovery(dataclasses.asdict(discovery_info))
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
Definition: config_flow.py:29
bool _async_has_devices(HomeAssistant hass)
Definition: config_flow.py:15