Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Wyoming integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryNotReady
11 from homeassistant.helpers import device_registry as dr
12 
13 from .const import ATTR_SPEAKER, DOMAIN
14 from .data import WyomingService
15 from .devices import SatelliteDevice
16 from .models import DomainDataItem
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 SATELLITE_PLATFORMS = [
21  Platform.ASSIST_SATELLITE,
22  Platform.BINARY_SENSOR,
23  Platform.SELECT,
24  Platform.SWITCH,
25  Platform.NUMBER,
26 ]
27 
28 __all__ = [
29  "ATTR_SPEAKER",
30  "DOMAIN",
31  "async_setup_entry",
32  "async_unload_entry",
33 ]
34 
35 
36 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
37  """Load Wyoming."""
38  service = await WyomingService.create(entry.data["host"], entry.data["port"])
39 
40  if service is None:
41  raise ConfigEntryNotReady("Unable to connect")
42 
43  item = DomainDataItem(service=service)
44  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = item
45 
46  await hass.config_entries.async_forward_entry_setups(entry, service.platforms)
47  entry.async_on_unload(entry.add_update_listener(update_listener))
48 
49  if (satellite_info := service.info.satellite) is not None:
50  # Create satellite device
51  dev_reg = dr.async_get(hass)
52 
53  # Use config entry id since only one satellite per entry is supported
54  satellite_id = entry.entry_id
55  device = dev_reg.async_get_or_create(
56  config_entry_id=entry.entry_id,
57  identifiers={(DOMAIN, satellite_id)},
58  name=satellite_info.name,
59  suggested_area=satellite_info.area,
60  )
61 
62  item.device = SatelliteDevice(
63  satellite_id=satellite_id,
64  device_id=device.id,
65  )
66 
67  # Set up satellite entity, sensors, switches, etc.
68  await hass.config_entries.async_forward_entry_setups(entry, SATELLITE_PLATFORMS)
69 
70  return True
71 
72 
73 async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
74  """Handle options update."""
75  await hass.config_entries.async_reload(entry.entry_id)
76 
77 
78 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
79  """Unload Wyoming."""
80  item: DomainDataItem = hass.data[DOMAIN][entry.entry_id]
81 
82  platforms = list(item.service.platforms)
83  if item.device is not None:
84  platforms += SATELLITE_PLATFORMS
85 
86  unload_ok = await hass.config_entries.async_unload_platforms(entry, platforms)
87  if unload_ok:
88  del hass.data[DOMAIN][entry.entry_id]
89 
90  return unload_ok
def update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:73
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:78
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:36