1 """Code to handle a Dynalite bridge."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from types
import MappingProxyType
9 from dynalite_devices_lib.dynalite_devices
import (
10 CONF_AREA
as dyn_CONF_AREA,
11 CONF_PRESET
as dyn_CONF_PRESET,
23 from .const
import ATTR_AREA, ATTR_HOST, ATTR_PACKET, ATTR_PRESET, LOGGER, PLATFORMS
24 from .convert_config
import convert_config
28 """Manages a single Dynalite bridge."""
30 def __init__(self, hass: HomeAssistant, config: dict[str, Any]) ->
None:
31 """Initialize the system based on host parameter."""
33 self.async_add_devices: dict[str, Callable] = {}
34 self.waiting_devices: dict[str, list[str]] = {}
35 self.
hosthost = config[CONF_HOST]
45 """Set up a Dynalite bridge."""
47 LOGGER.debug(
"Setting up bridge - host %s", self.
hosthost)
51 """Reconfigure a bridge when config changes."""
52 LOGGER.debug(
"Reloading bridge - host %s, config %s", self.
hosthost, config)
55 def update_signal(self, device: DynaliteBaseDevice |
None =
None) -> str:
56 """Create signal to use to trigger entity update."""
58 signal = f
"dynalite-update-{self.host}-{device.unique_id}"
60 signal = f
"dynalite-update-{self.host}"
64 def update_device(self, device: DynaliteBaseDevice |
None =
None) ->
None:
65 """Call when a device or all devices should be updated."""
69 "Connected" if self.
dynalite_devicesdynalite_devices.connected
else "Disconnected"
71 LOGGER.debug(
"%s to dynalite host", log_string)
78 """Handle a notification from the platform and issue events."""
79 if notification.notification == NOTIFICATION_PACKET:
80 self.
hasshass.bus.async_fire(
83 ATTR_HOST: self.
hosthost,
84 ATTR_PACKET: notification.data[NOTIFICATION_PACKET],
87 if notification.notification == NOTIFICATION_PRESET:
88 self.
hasshass.bus.async_fire(
91 ATTR_HOST: self.
hosthost,
92 ATTR_AREA: notification.data[dyn_CONF_AREA],
93 ATTR_PRESET: notification.data[dyn_CONF_PRESET],
99 """Add an async_add_entities for a category."""
100 self.async_add_devices[platform] = async_add_devices
101 if platform
in self.waiting_devices:
102 self.async_add_devices[platform](self.waiting_devices[platform])
105 """Add the devices to HA if the add devices callback was registered, otherwise queue until it is."""
106 for platform
in PLATFORMS:
108 device
for device
in devices
if device.category == platform
110 if platform
in self.async_add_devices:
111 self.async_add_devices[platform](platform_devices)
113 if platform
not in self.waiting_devices:
114 self.waiting_devices[platform] = []
115 self.waiting_devices[platform].extend(platform_devices)
None add_devices_when_registered(self, list[DynaliteBaseDevice] devices)
None update_device(self, DynaliteBaseDevice|None device=None)
None handle_notification(self, DynaliteNotification notification)
None reload_config(self, MappingProxyType[str, Any] config)
str update_signal(self, DynaliteBaseDevice|None device=None)
None register_add_devices(self, str platform, Callable async_add_devices)
None __init__(self, HomeAssistant hass, dict[str, Any] config)
dict[str, Any] convert_config(dict[str, Any]|MappingProxyType[str, Any] config)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)