1 """The Switcher integration."""
3 from __future__
import annotations
7 from aioswitcher.bridge
import SwitcherBridge
8 from aioswitcher.device
import SwitcherBase
15 from .const
import DOMAIN
16 from .coordinator
import SwitcherDataUpdateCoordinator
27 _LOGGER = logging.getLogger(__name__)
30 type SwitcherConfigEntry = ConfigEntry[dict[str, SwitcherDataUpdateCoordinator]]
34 """Set up Switcher from a config entry."""
36 token = entry.data.get(CONF_TOKEN)
39 def on_device_data_callback(device: SwitcherBase) ->
None:
40 """Use as a callback for device data."""
42 coordinators = entry.runtime_data
45 if coordinator := coordinators.get(device.device_id):
46 coordinator.async_set_updated_data(device)
51 "Discovered Switcher device - id: %s, key: %s, name: %s, type: %s (%s), is_token_needed: %s",
55 device.device_type.value,
56 device.device_type.hex_rep,
60 if device.token_needed
and not token:
61 entry.async_start_reauth(hass)
65 coordinator.async_setup()
66 coordinators[device.device_id] = coordinator
69 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
71 entry.runtime_data = {}
72 bridge = SwitcherBridge(on_device_data_callback)
75 async
def stop_bridge(event: Event |
None =
None) ->
None:
78 entry.async_on_unload(stop_bridge)
80 entry.async_on_unload(
81 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_bridge)
88 """Unload a config entry."""
89 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
93 hass: HomeAssistant, config_entry: SwitcherConfigEntry, device_entry: dr.DeviceEntry
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
bool async_unload_entry(HomeAssistant hass, SwitcherConfigEntry entry)
bool async_remove_config_entry_device(HomeAssistant hass, SwitcherConfigEntry config_entry, dr.DeviceEntry device_entry)
bool async_setup_entry(HomeAssistant hass, SwitcherConfigEntry entry)