Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Home Connect entity base class."""
2 
3 import logging
4 
5 from homeassistant.core import callback
6 from homeassistant.helpers.device_registry import DeviceInfo
7 from homeassistant.helpers.dispatcher import async_dispatcher_connect
8 from homeassistant.helpers.entity import Entity, EntityDescription
9 
10 from .api import HomeConnectDevice
11 from .const import DOMAIN, SIGNAL_UPDATE_ENTITIES
12 
13 _LOGGER = logging.getLogger(__name__)
14 
15 
17  """Generic Home Connect entity (base class)."""
18 
19  _attr_should_poll = False
20  _attr_has_entity_name = True
21 
22  def __init__(self, device: HomeConnectDevice, desc: EntityDescription) -> None:
23  """Initialize the entity."""
24  self.devicedevice = device
25  self.entity_descriptionentity_description = desc
26  self._attr_unique_id_attr_unique_id = f"{device.appliance.haId}-{self.bsh_key}"
27  self._attr_device_info_attr_device_info = DeviceInfo(
28  identifiers={(DOMAIN, device.appliance.haId)},
29  manufacturer=device.appliance.brand,
30  model=device.appliance.vib,
31  name=device.appliance.name,
32  )
33 
34  async def async_added_to_hass(self) -> None:
35  """Register callbacks."""
36  self.async_on_removeasync_on_remove(
38  self.hasshass, SIGNAL_UPDATE_ENTITIES, self._update_callback_update_callback
39  )
40  )
41 
42  @callback
43  def _update_callback(self, ha_id: str) -> None:
44  """Update data."""
45  if ha_id == self.devicedevice.appliance.haId:
46  self.async_entity_updateasync_entity_update()
47 
48  @callback
49  def async_entity_update(self) -> None:
50  """Update the entity."""
51  _LOGGER.debug("Entity update triggered on %s", self)
52  self.async_schedule_update_ha_stateasync_schedule_update_ha_state(True)
53 
54  @property
55  def bsh_key(self) -> str:
56  """Return the BSH key."""
57  return self.entity_descriptionentity_description.key
None __init__(self, HomeConnectDevice device, EntityDescription desc)
Definition: entity.py:22
None async_schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1265
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103