Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Dormakaba dKey integration base entity."""
2 
3 from __future__ import annotations
4 
5 import abc
6 
7 from py_dormakaba_dkey import DKEYLock
8 from py_dormakaba_dkey.commands import Notifications
9 
10 from homeassistant.core import callback
11 from homeassistant.helpers import device_registry as dr
12 from homeassistant.helpers.device_registry import DeviceInfo
14  CoordinatorEntity,
15  DataUpdateCoordinator,
16 )
17 
18 
19 class DormakabaDkeyEntity(CoordinatorEntity[DataUpdateCoordinator[None]]):
20  """Dormakaba dKey base entity."""
21 
22  _attr_has_entity_name = True
23 
24  def __init__(
25  self, coordinator: DataUpdateCoordinator[None], lock: DKEYLock
26  ) -> None:
27  """Initialize a Dormakaba dKey entity."""
28  super().__init__(coordinator)
29  self._lock_lock = lock
30  self._attr_device_info_attr_device_info = DeviceInfo(
31  name=lock.device_info.device_name or lock.device_info.device_id,
32  model="MTL 9291",
33  sw_version=lock.device_info.sw_version,
34  connections={(dr.CONNECTION_BLUETOOTH, lock.address)},
35  )
36  self._async_update_attrs_async_update_attrs()
37 
38  @abc.abstractmethod
39  @callback
40  def _async_update_attrs(self) -> None:
41  """Handle updating _attr values."""
42 
43  @callback
44  def _handle_coordinator_update(self) -> None:
45  """Handle data update."""
46  self._async_update_attrs_async_update_attrs()
47  self.async_write_ha_state()
48 
49  @callback
50  def _handle_state_update(self, update: Notifications) -> None:
51  """Handle data update."""
52  self.coordinator.async_set_updated_data(None)
53 
54  async def async_added_to_hass(self) -> None:
55  """Register callbacks."""
56  self.async_on_remove(self._lock_lock.register_callback(self._handle_state_update_handle_state_update))
57  return await super().async_added_to_hass()
None __init__(self, DataUpdateCoordinator[None] coordinator, DKEYLock lock)
Definition: entity.py:26