Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Switcher integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from aioswitcher.bridge import SwitcherBridge
8 from aioswitcher.device import SwitcherBase
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_TOKEN, EVENT_HOMEASSISTANT_STOP, Platform
12 from homeassistant.core import Event, HomeAssistant, callback
13 from homeassistant.helpers import device_registry as dr
14 
15 from .const import DOMAIN
16 from .coordinator import SwitcherDataUpdateCoordinator
17 
18 PLATFORMS = [
19  Platform.BUTTON,
20  Platform.CLIMATE,
21  Platform.COVER,
22  Platform.LIGHT,
23  Platform.SENSOR,
24  Platform.SWITCH,
25 ]
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 
30 type SwitcherConfigEntry = ConfigEntry[dict[str, SwitcherDataUpdateCoordinator]]
31 
32 
33 async def async_setup_entry(hass: HomeAssistant, entry: SwitcherConfigEntry) -> bool:
34  """Set up Switcher from a config entry."""
35 
36  token = entry.data.get(CONF_TOKEN)
37 
38  @callback
39  def on_device_data_callback(device: SwitcherBase) -> None:
40  """Use as a callback for device data."""
41 
42  coordinators = entry.runtime_data
43 
44  # Existing device update device data
45  if coordinator := coordinators.get(device.device_id):
46  coordinator.async_set_updated_data(device)
47  return
48 
49  # New device - create device
50  _LOGGER.info(
51  "Discovered Switcher device - id: %s, key: %s, name: %s, type: %s (%s), is_token_needed: %s",
52  device.device_id,
53  device.device_key,
54  device.name,
55  device.device_type.value,
56  device.device_type.hex_rep,
57  device.token_needed,
58  )
59 
60  if device.token_needed and not token:
61  entry.async_start_reauth(hass)
62  return
63 
64  coordinator = SwitcherDataUpdateCoordinator(hass, entry, device)
65  coordinator.async_setup()
66  coordinators[device.device_id] = coordinator
67 
68  # Must be ready before dispatcher is called
69  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
70 
71  entry.runtime_data = {}
72  bridge = SwitcherBridge(on_device_data_callback)
73  await bridge.start()
74 
75  async def stop_bridge(event: Event | None = None) -> None:
76  await bridge.stop()
77 
78  entry.async_on_unload(stop_bridge)
79 
80  entry.async_on_unload(
81  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_bridge)
82  )
83 
84  return True
85 
86 
87 async def async_unload_entry(hass: HomeAssistant, entry: SwitcherConfigEntry) -> bool:
88  """Unload a config entry."""
89  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
90 
91 
93  hass: HomeAssistant, config_entry: SwitcherConfigEntry, device_entry: dr.DeviceEntry
94 ) -> bool:
95  """Remove a config entry from a device."""
96  return not device_entry.identifiers.intersection(
97  (DOMAIN, device_id) for device_id in config_entry.runtime_data
98  )
bool async_unload_entry(HomeAssistant hass, SwitcherConfigEntry entry)
Definition: __init__.py:87
bool async_remove_config_entry_device(HomeAssistant hass, SwitcherConfigEntry config_entry, dr.DeviceEntry device_entry)
Definition: __init__.py:94
bool async_setup_entry(HomeAssistant hass, SwitcherConfigEntry entry)
Definition: __init__.py:33