2023-02-13 10:09:23 +00:00
|
|
|
"""Dormakaba dKey integration base entity."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2023-02-13 10:09:23 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import abc
|
|
|
|
|
|
|
|
from py_dormakaba_dkey.commands import Notifications
|
|
|
|
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers import device_registry as dr
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2025-01-24 13:14:03 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2023-02-13 10:09:23 +00:00
|
|
|
|
2025-01-24 13:14:03 +00:00
|
|
|
from .coordinator import DormakabaDkeyCoordinator
|
2023-02-13 10:09:23 +00:00
|
|
|
|
2025-01-24 13:14:03 +00:00
|
|
|
|
|
|
|
class DormakabaDkeyEntity(CoordinatorEntity[DormakabaDkeyCoordinator]):
|
2023-02-13 10:09:23 +00:00
|
|
|
"""Dormakaba dKey base entity."""
|
|
|
|
|
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2025-01-24 13:14:03 +00:00
|
|
|
def __init__(self, coordinator: DormakabaDkeyCoordinator) -> None:
|
2023-02-13 10:09:23 +00:00
|
|
|
"""Initialize a Dormakaba dKey entity."""
|
|
|
|
super().__init__(coordinator)
|
2025-01-24 13:14:03 +00:00
|
|
|
lock = coordinator.lock
|
2023-02-13 10:09:23 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
name=lock.device_info.device_name or lock.device_info.device_id,
|
|
|
|
model="MTL 9291",
|
|
|
|
sw_version=lock.device_info.sw_version,
|
|
|
|
connections={(dr.CONNECTION_BLUETOOTH, lock.address)},
|
|
|
|
)
|
|
|
|
self._async_update_attrs()
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
@callback
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Handle updating _attr values."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle data update."""
|
|
|
|
self._async_update_attrs()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_state_update(self, update: Notifications) -> None:
|
|
|
|
"""Handle data update."""
|
|
|
|
self.coordinator.async_set_updated_data(None)
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Register callbacks."""
|
2025-01-24 13:14:03 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
self.coordinator.lock.register_callback(self._handle_state_update)
|
|
|
|
)
|
2023-02-13 10:09:23 +00:00
|
|
|
return await super().async_added_to_hass()
|