2020-03-01 21:44:24 +00:00
|
|
|
"""Support for the Dynalite devices as entities."""
|
2021-03-17 22:43:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-29 14:15:36 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from typing import Any
|
2020-04-02 18:26:36 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2020-03-01 21:44:24 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-05-01 22:37:19 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
2021-05-04 21:36:48 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-03-01 21:44:24 +00:00
|
|
|
|
2021-12-13 19:03:01 +00:00
|
|
|
from .bridge import DynaliteBridge
|
2020-03-01 21:44:24 +00:00
|
|
|
from .const import DOMAIN, LOGGER
|
|
|
|
|
|
|
|
|
|
|
|
def async_setup_entry_base(
|
2020-04-02 18:26:36 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
2021-05-04 21:36:48 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-04-02 18:26:36 +00:00
|
|
|
platform: str,
|
|
|
|
entity_from_device: Callable,
|
|
|
|
) -> None:
|
2020-03-01 21:44:24 +00:00
|
|
|
"""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:
|
2020-04-02 18:26:36 +00:00
|
|
|
added_entities.append(entity_from_device(device, bridge))
|
2020-03-01 21:44:24 +00:00
|
|
|
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."""
|
|
|
|
|
2020-04-02 18:26:36 +00:00
|
|
|
def __init__(self, device: Any, bridge: DynaliteBridge) -> None:
|
2020-03-01 21:44:24 +00:00
|
|
|
"""Initialize the base class."""
|
|
|
|
self._device = device
|
|
|
|
self._bridge = bridge
|
2021-07-20 16:28:31 +00:00
|
|
|
self._unsub_dispatchers: list[Callable[[], None]] = []
|
2020-03-01 21:44:24 +00:00
|
|
|
|
|
|
|
@property
|
2020-04-02 18:26:36 +00:00
|
|
|
def name(self) -> str:
|
2020-03-01 21:44:24 +00:00
|
|
|
"""Return the name of the entity."""
|
|
|
|
return self._device.name
|
|
|
|
|
|
|
|
@property
|
2020-04-02 18:26:36 +00:00
|
|
|
def unique_id(self) -> str:
|
2020-03-01 21:44:24 +00:00
|
|
|
"""Return the unique ID of the entity."""
|
|
|
|
return self._device.unique_id
|
|
|
|
|
|
|
|
@property
|
2020-04-02 18:26:36 +00:00
|
|
|
def available(self) -> bool:
|
2020-03-01 21:44:24 +00:00
|
|
|
"""Return if entity is available."""
|
|
|
|
return self._device.available
|
|
|
|
|
|
|
|
@property
|
2021-05-01 22:37:19 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-03-01 21:44:24 +00:00
|
|
|
"""Device info for this entity."""
|
2021-10-22 15:00:00 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self._device.unique_id)},
|
|
|
|
manufacturer="Dynalite",
|
|
|
|
name=self.name,
|
|
|
|
)
|
2020-03-01 21:44:24 +00:00
|
|
|
|
2020-04-02 18:26:36 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2020-03-01 21:44:24 +00:00
|
|
|
"""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,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-04-02 18:26:36 +00:00
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
2020-03-01 21:44:24 +00:00
|
|
|
"""Unregister signal dispatch listeners when being removed."""
|
|
|
|
for unsub in self._unsub_dispatchers:
|
|
|
|
unsub()
|
|
|
|
self._unsub_dispatchers = []
|