hass-core/homeassistant/components/dynalite/bridge.py
Ziv e13d5bdc10
Refactor dynalite integration for multi-platform (#32335)
* refactoring for multi platform

* adopted test_bridge to refactoring

* refactoring tests for multi-platform
additional coverage in config and init

* comment for clarity

* more specific imports from lib

* library version bump

* removed async_update

* changed parameter order to start with hass

* removed pylint disable

* unsubscribe from signal dispatcher
inherit from Entity

* use device.unique_id

* changed hass_obj to hass

* added test for remove entity
bug fix from the test

* removed the polling try_connect. hate polling... it is now part of the async_setup()
significantly makes the code clearer and simplifies the tests

* removed leftover debug logs in the library

* changed tests to get the entry_id from hass

* changed place to assign hass.data only after success

* fixes for test_init

* removed assert

* removed device_info

* removed bridge internal from common

* modified test_bridge to work without the bridge directly

* removed bridge from test_existing_update

* changed update to not use bridge internals

* dyn_bridge fixture no longer used - removed
2020-03-01 22:44:24 +01:00

78 lines
3.1 KiB
Python
Executable file

"""Code to handle a Dynalite bridge."""
from dynalite_devices_lib.dynalite_devices import DynaliteDevices
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_send
from .const import CONF_ALL, CONF_HOST, ENTITY_PLATFORMS, LOGGER
class DynaliteBridge:
"""Manages a single Dynalite bridge."""
def __init__(self, hass, config):
"""Initialize the system based on host parameter."""
self.hass = hass
self.area = {}
self.async_add_devices = {}
self.waiting_devices = {}
self.host = config[CONF_HOST]
# Configure the dynalite devices
self.dynalite_devices = DynaliteDevices(
newDeviceFunc=self.add_devices_when_registered,
updateDeviceFunc=self.update_device,
)
self.dynalite_devices.configure(config)
async def async_setup(self):
"""Set up a Dynalite bridge."""
# Configure the dynalite devices
LOGGER.debug("Setting up bridge - host %s", self.host)
return await self.dynalite_devices.async_setup()
async def reload_config(self, config):
"""Reconfigure a bridge when config changes."""
LOGGER.debug("Reloading bridge - host %s, config %s", self.host, config)
self.dynalite_devices.configure(config)
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
@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.
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())
else:
async_dispatcher_send(self.hass, self.update_signal(device))
@callback
def register_add_devices(self, platform, async_add_devices):
"""Add an async_add_entities for a category."""
self.async_add_devices[platform] = async_add_devices
if platform in self.waiting_devices:
self.async_add_devices[platform](self.waiting_devices[platform])
def add_devices_when_registered(self, devices):
"""Add the devices to HA if the add devices callback was registered, otherwise queue until it is."""
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)