hass-core/homeassistant/components/dynalite/dynalitebase.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

85 lines
2.7 KiB
Python
Executable file

"""Support for the Dynalite devices as entities."""
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from .const import DOMAIN, LOGGER
def async_setup_entry_base(
hass, config_entry, async_add_entities, platform, entity_from_device
):
"""Record the async_add_entities function to add them later when received from Dynalite."""
LOGGER.debug("Setting up %s entry = %s", platform, config_entry.data)
bridge = hass.data[DOMAIN][config_entry.entry_id]
@callback
def async_add_entities_platform(devices):
# assumes it is called with a single platform
added_entities = []
for device in devices:
if device.category == platform:
added_entities.append(entity_from_device(device, bridge))
if added_entities:
async_add_entities(added_entities)
bridge.register_add_devices(platform, async_add_entities_platform)
class DynaliteBase(Entity):
"""Base class for the Dynalite entities."""
def __init__(self, device, bridge):
"""Initialize the base class."""
self._device = device
self._bridge = bridge
self._unsub_dispatchers = []
@property
def name(self):
"""Return the name of the entity."""
return self._device.name
@property
def unique_id(self):
"""Return the unique ID of the entity."""
return self._device.unique_id
@property
def available(self):
"""Return if entity is available."""
return self._device.available
@property
def device_info(self):
"""Device info for this entity."""
return {
"identifiers": {(DOMAIN, self._device.unique_id)},
"name": self.name,
"manufacturer": "Dynalite",
}
async def async_added_to_hass(self):
"""Added to hass so need to register to dispatch."""
# register for device specific update
self._unsub_dispatchers.append(
async_dispatcher_connect(
self.hass,
self._bridge.update_signal(self._device),
self.async_schedule_update_ha_state,
)
)
# register for wide update
self._unsub_dispatchers.append(
async_dispatcher_connect(
self.hass,
self._bridge.update_signal(),
self.async_schedule_update_ha_state,
)
)
async def async_will_remove_from_hass(self):
"""Unregister signal dispatch listeners when being removed."""
for unsub in self._unsub_dispatchers:
unsub()
self._unsub_dispatchers = []