73 lines
1.8 KiB
Python
73 lines
1.8 KiB
Python
|
"""Linak Bluetooth data coordinator"""
|
||
|
|
||
|
from datetime import timedelta
|
||
|
import logging
|
||
|
|
||
|
from homeassistant.helpers.update_coordinator import (
|
||
|
CoordinatorEntity,
|
||
|
DataUpdateCoordinator,
|
||
|
)
|
||
|
from homeassistant.helpers.entity import DeviceInfo
|
||
|
from homeassistant.core import HomeAssistant, callback
|
||
|
|
||
|
from .api import Device
|
||
|
|
||
|
|
||
|
from .const import DOMAIN
|
||
|
|
||
|
_LOGGER = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
class LinakDataUpdateCoordinator(DataUpdateCoordinator[Device]):
|
||
|
"""Linak data update coordinator"""
|
||
|
|
||
|
_device: Device
|
||
|
|
||
|
def __init__(self, hass: HomeAssistant, device: Device) -> None:
|
||
|
"""Initialize coordinator"""
|
||
|
super().__init__(
|
||
|
hass,
|
||
|
_LOGGER,
|
||
|
name="Linak Bluetooth",
|
||
|
update_interval=timedelta(milliseconds=250),
|
||
|
)
|
||
|
self._device = device
|
||
|
|
||
|
async def _async_update_data(self):
|
||
|
"""Update device state"""
|
||
|
|
||
|
await self._device.fetch_state()
|
||
|
return self._device
|
||
|
|
||
|
|
||
|
class LinakBluetoothEntity(CoordinatorEntity[LinakDataUpdateCoordinator]):
|
||
|
"""Base class for Linak entities"""
|
||
|
|
||
|
_device: Device
|
||
|
_attr_has_entity_name = True
|
||
|
|
||
|
def __init__(
|
||
|
self,
|
||
|
coordinator: LinakDataUpdateCoordinator,
|
||
|
) -> None:
|
||
|
"""Initialize Linak base entity"""
|
||
|
super().__init__(coordinator)
|
||
|
|
||
|
self._device = coordinator.data
|
||
|
|
||
|
self._attr_device_info = DeviceInfo(
|
||
|
identifiers={(DOMAIN, self._device.mac)},
|
||
|
manufacturer="Linak",
|
||
|
name=self._device.name,
|
||
|
)
|
||
|
|
||
|
@callback
|
||
|
def _handle_coordinator_update(self) -> None:
|
||
|
"""Handle updated data from the coordinator"""
|
||
|
self._device = self.coordinator.data
|
||
|
self.async_write_ha_state()
|
||
|
|
||
|
@property
|
||
|
def available(self) -> bool:
|
||
|
return self._device.is_connected
|