2020-02-10 21:16:04 +00:00
|
|
|
"""Code to handle a Dynalite bridge."""
|
|
|
|
|
2020-03-01 21:44:24 +00:00
|
|
|
from dynalite_devices_lib.dynalite_devices import DynaliteDevices
|
2020-02-10 21:16:04 +00:00
|
|
|
|
|
|
|
from homeassistant.core import callback
|
2020-02-21 22:29:59 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2020-02-10 21:16:04 +00:00
|
|
|
|
2020-03-01 21:44:24 +00:00
|
|
|
from .const import CONF_ALL, CONF_HOST, ENTITY_PLATFORMS, LOGGER
|
2020-02-10 21:16:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DynaliteBridge:
|
|
|
|
"""Manages a single Dynalite bridge."""
|
|
|
|
|
2020-02-21 22:29:59 +00:00
|
|
|
def __init__(self, hass, config):
|
2020-02-10 21:16:04 +00:00
|
|
|
"""Initialize the system based on host parameter."""
|
|
|
|
self.hass = hass
|
|
|
|
self.area = {}
|
2020-03-01 21:44:24 +00:00
|
|
|
self.async_add_devices = {}
|
|
|
|
self.waiting_devices = {}
|
2020-02-21 22:29:59 +00:00
|
|
|
self.host = config[CONF_HOST]
|
2020-02-10 21:16:04 +00:00
|
|
|
# Configure the dynalite devices
|
|
|
|
self.dynalite_devices = DynaliteDevices(
|
2020-03-05 06:05:39 +00:00
|
|
|
new_device_func=self.add_devices_when_registered,
|
|
|
|
update_device_func=self.update_device,
|
2020-02-10 21:16:04 +00:00
|
|
|
)
|
2020-02-28 06:05:55 +00:00
|
|
|
self.dynalite_devices.configure(config)
|
2020-02-10 21:16:04 +00:00
|
|
|
|
2020-02-21 22:29:59 +00:00
|
|
|
async def async_setup(self):
|
2020-02-10 21:16:04 +00:00
|
|
|
"""Set up a Dynalite bridge."""
|
|
|
|
# Configure the dynalite devices
|
2020-02-28 06:05:55 +00:00
|
|
|
LOGGER.debug("Setting up bridge - host %s", self.host)
|
2020-02-21 22:29:59 +00:00
|
|
|
return await self.dynalite_devices.async_setup()
|
2020-02-10 21:16:04 +00:00
|
|
|
|
2020-03-05 06:05:39 +00:00
|
|
|
def reload_config(self, config):
|
2020-02-28 06:05:55 +00:00
|
|
|
"""Reconfigure a bridge when config changes."""
|
2020-03-01 21:44:24 +00:00
|
|
|
LOGGER.debug("Reloading bridge - host %s, config %s", self.host, config)
|
2020-02-28 06:05:55 +00:00
|
|
|
self.dynalite_devices.configure(config)
|
|
|
|
|
2020-02-21 22:29:59 +00:00
|
|
|
def update_signal(self, device=None):
|
|
|
|
"""Create signal to use to trigger entity update."""
|
|
|
|
if device:
|
|
|
|
signal = f"dynalite-update-{self.host}-{device.unique_id}"
|
|
|
|
else:
|
|
|
|
signal = f"dynalite-update-{self.host}"
|
|
|
|
return signal
|
2020-02-10 21:16:04 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_device(self, device):
|
|
|
|
"""Call when a device or all devices should be updated."""
|
|
|
|
if device == CONF_ALL:
|
|
|
|
# This is used to signal connection or disconnection, so all devices may become available or not.
|
2020-02-21 22:29:59 +00:00
|
|
|
log_string = (
|
|
|
|
"Connected" if self.dynalite_devices.available else "Disconnected"
|
|
|
|
)
|
|
|
|
LOGGER.info("%s to dynalite host", log_string)
|
|
|
|
async_dispatcher_send(self.hass, self.update_signal())
|
2020-02-10 21:16:04 +00:00
|
|
|
else:
|
2020-02-21 22:29:59 +00:00
|
|
|
async_dispatcher_send(self.hass, self.update_signal(device))
|
|
|
|
|
2020-02-10 21:16:04 +00:00
|
|
|
@callback
|
2020-03-01 21:44:24 +00:00
|
|
|
def register_add_devices(self, platform, async_add_devices):
|
2020-02-10 21:16:04 +00:00
|
|
|
"""Add an async_add_entities for a category."""
|
2020-03-01 21:44:24 +00:00
|
|
|
self.async_add_devices[platform] = async_add_devices
|
|
|
|
if platform in self.waiting_devices:
|
|
|
|
self.async_add_devices[platform](self.waiting_devices[platform])
|
2020-02-10 21:16:04 +00:00
|
|
|
|
2020-02-21 22:29:59 +00:00
|
|
|
def add_devices_when_registered(self, devices):
|
|
|
|
"""Add the devices to HA if the add devices callback was registered, otherwise queue until it is."""
|
2020-03-01 21:44:24 +00:00
|
|
|
for platform in ENTITY_PLATFORMS:
|
|
|
|
platform_devices = [
|
|
|
|
device for device in devices if device.category == platform
|
|
|
|
]
|
|
|
|
if platform in self.async_add_devices:
|
|
|
|
self.async_add_devices[platform](platform_devices)
|
|
|
|
else: # handle it later when it is registered
|
|
|
|
if platform not in self.waiting_devices:
|
|
|
|
self.waiting_devices[platform] = []
|
|
|
|
self.waiting_devices[platform].extend(platform_devices)
|