Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base class for KNX devices."""
2 
3 from __future__ import annotations
4 
5 from typing import TYPE_CHECKING, Any
6 
7 from xknx.devices import Device as XknxDevice
8 
9 from homeassistant.const import CONF_ENTITY_CATEGORY, EntityCategory
10 from homeassistant.helpers.device_registry import DeviceInfo
11 from homeassistant.helpers.entity import Entity
12 from homeassistant.helpers.entity_platform import EntityPlatform
13 from homeassistant.helpers.entity_registry import RegistryEntry
14 
15 from .const import DOMAIN
16 from .storage.config_store import PlatformControllerBase
17 from .storage.const import CONF_DEVICE_INFO
18 
19 if TYPE_CHECKING:
20  from . import KNXModule
21 
22 
24  """Class to manage dynamic adding and reloading of UI entities."""
25 
26  def __init__(
27  self,
28  knx_module: KNXModule,
29  entity_platform: EntityPlatform,
30  entity_class: type[KnxUiEntity],
31  ) -> None:
32  """Initialize the UI platform."""
33  self._knx_module_knx_module = knx_module
34  self._entity_platform_entity_platform = entity_platform
35  self._entity_class_entity_class = entity_class
36 
37  async def create_entity(self, unique_id: str, config: dict[str, Any]) -> None:
38  """Add a new UI entity."""
39  await self._entity_platform_entity_platform.async_add_entities(
40  [self._entity_class_entity_class(self._knx_module_knx_module, unique_id, config)]
41  )
42 
43  async def update_entity(
44  self, entity_entry: RegistryEntry, config: dict[str, Any]
45  ) -> None:
46  """Update an existing UI entities configuration."""
47  await self._entity_platform_entity_platform.async_remove_entity(entity_entry.entity_id)
48  await self.create_entitycreate_entitycreate_entity(unique_id=entity_entry.unique_id, config=config)
49 
50 
52  """Representation of a KNX entity."""
53 
54  _attr_should_poll = False
55  _knx_module: KNXModule
56  _device: XknxDevice
57 
58  @property
59  def name(self) -> str:
60  """Return the name of the KNX device."""
61  return self._device.name
62 
63  @property
64  def available(self) -> bool:
65  """Return True if entity is available."""
66  return self._knx_module.connected
67 
68  async def async_update(self) -> None:
69  """Request a state update from KNX bus."""
70  await self._device.sync()
71 
72  def after_update_callback(self, _device: XknxDevice) -> None:
73  """Call after device was updated."""
74  self.async_write_ha_stateasync_write_ha_state()
75 
76  async def async_added_to_hass(self) -> None:
77  """Store register state change callback and start device object."""
78  self._device.register_device_updated_cb(self.after_update_callbackafter_update_callback)
79  self._device.xknx.devices.async_add(self._device)
80  # super call needed to have methods of multi-inherited classes called
81  # eg. for restoring state (like _KNXSwitch)
82  await super().async_added_to_hass()
83 
84  async def async_will_remove_from_hass(self) -> None:
85  """Disconnect device object when removed."""
86  self._device.unregister_device_updated_cb(self.after_update_callbackafter_update_callback)
87  self._device.xknx.devices.async_remove(self._device)
88 
89 
91  """Representation of a KNX entity configured from YAML."""
92 
93  def __init__(self, knx_module: KNXModule, device: XknxDevice) -> None:
94  """Initialize the YAML entity."""
95  self._knx_module_knx_module = knx_module
96  self._device_device = device
97 
98 
100  """Representation of a KNX UI entity."""
101 
102  _attr_unique_id: str
103  _attr_has_entity_name = True
104 
105  def __init__(
106  self, knx_module: KNXModule, unique_id: str, entity_config: dict[str, Any]
107  ) -> None:
108  """Initialize the UI entity."""
109  self._knx_module_knx_module = knx_module
110  self._attr_unique_id_attr_unique_id = unique_id
111  if entity_category := entity_config.get(CONF_ENTITY_CATEGORY):
112  self._attr_entity_category_attr_entity_category = EntityCategory(entity_category)
113  if device_info := entity_config.get(CONF_DEVICE_INFO):
114  self._attr_device_info_attr_device_info = DeviceInfo(identifiers={(DOMAIN, device_info)})
None create_entity(self, str unique_id, dict[str, Any] config)
Definition: entity.py:37
None update_entity(self, RegistryEntry entity_entry, dict[str, Any] config)
Definition: entity.py:45
None __init__(self, KNXModule knx_module, EntityPlatform entity_platform, type[KnxUiEntity] entity_class)
Definition: entity.py:31
None __init__(self, KNXModule knx_module, str unique_id, dict[str, Any] entity_config)
Definition: entity.py:107
None __init__(self, KNXModule knx_module, XknxDevice device)
Definition: entity.py:93
None after_update_callback(self, XknxDevice _device)
Definition: entity.py:72
None create_entity(self, str unique_id, dict[str, Any] config)
Definition: config_store.py:38