Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for the Dynalite devices as entities."""
2 
3 from __future__ import annotations
4 
5 from abc import ABC, abstractmethod
6 from collections.abc import Callable
7 from typing import Any
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.dispatcher import async_dispatcher_connect
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.helpers.restore_state import RestoreEntity
15 
16 from .bridge import DynaliteBridge
17 from .const import DOMAIN, LOGGER
18 
19 
21  hass: HomeAssistant,
22  config_entry: ConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24  platform: str,
25  entity_from_device: Callable,
26 ) -> None:
27  """Record the async_add_entities function to add them later when received from Dynalite."""
28  LOGGER.debug("Setting up %s entry = %s", platform, config_entry.data)
29  bridge = hass.data[DOMAIN][config_entry.entry_id]
30 
31  @callback
32  def async_add_entities_platform(devices):
33  # assumes it is called with a single platform
34  added_entities = [entity_from_device(device, bridge) for device in devices]
35  async_add_entities(added_entities)
36 
37  bridge.register_add_devices(platform, async_add_entities_platform)
38 
39 
41  """Base class for the Dynalite entities."""
42 
43  _attr_has_entity_name = True
44  _attr_name = None
45 
46  def __init__(self, device: Any, bridge: DynaliteBridge) -> None:
47  """Initialize the base class."""
48  self._device_device = device
49  self._bridge_bridge = bridge
50  self._unsub_dispatchers_unsub_dispatchers: list[Callable[[], None]] = []
51 
52  @property
53  def unique_id(self) -> str:
54  """Return the unique ID of the entity."""
55  return self._device_device.unique_id
56 
57  @property
58  def available(self) -> bool:
59  """Return if entity is available."""
60  return self._device_device.available
61 
62  @property
63  def device_info(self) -> DeviceInfo:
64  """Device info for this entity."""
65  return DeviceInfo(
66  identifiers={(DOMAIN, self._device_device.unique_id)},
67  manufacturer="Dynalite",
68  name=self._device_device.name,
69  )
70 
71  async def async_added_to_hass(self) -> None:
72  """Handle addition to hass: restore state and register to dispatch."""
73  # register for device specific update
74  await super().async_added_to_hass()
75 
76  cur_state = await self.async_get_last_stateasync_get_last_state()
77  if cur_state:
78  self.initialize_stateinitialize_state(cur_state)
79  else:
80  LOGGER.warning("Restore state not available for %s", self.entity_identity_id)
81 
82  self._unsub_dispatchers_unsub_dispatchers.append(
84  self.hasshass,
85  self._bridge_bridge.update_signal(self._device_device),
86  self.async_schedule_update_ha_stateasync_schedule_update_ha_state,
87  )
88  )
89  # register for wide update
90  self._unsub_dispatchers_unsub_dispatchers.append(
92  self.hasshass,
93  self._bridge_bridge.update_signal(),
94  self.async_schedule_update_ha_stateasync_schedule_update_ha_state,
95  )
96  )
97 
98  @abstractmethod
99  def initialize_state(self, state):
100  """Initialize the state from cache."""
101 
102  async def async_will_remove_from_hass(self) -> None:
103  """Unregister signal dispatch listeners when being removed."""
104  for unsub in self._unsub_dispatchers:
105  unsub()
106  self._unsub_dispatchers_unsub_dispatchers = []
None __init__(self, Any device, DynaliteBridge bridge)
Definition: entity.py:46
None async_schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1265
None async_setup_entry_base(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities, str platform, Callable entity_from_device)
Definition: entity.py:26
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103