hass-reos-integration/custom_components/reos_integration/sensor.py

96 lines
3.3 KiB
Python

from __future__ import annotations
from datetime import datetime
import logging
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .reos_api import ReosApi, ReosLockModel
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None:
coordinator = hass.data[DOMAIN][config_entry.data["username"]]["coordinator"]
async_add_entities(ReosDoorUpdatedAt(lock, coordinator) for _, lock in coordinator.data.items())
async_add_entities(ReosDoorCreatedAt(lock, coordinator) for _, lock in coordinator.data.items())
async_add_entities(ReosDoorAvailableFrom(lock, coordinator) for _, lock in coordinator.data.items())
async_add_entities(ReosDoorAvailableTo(lock, coordinator) for _, lock in coordinator.data.items())
class SensorBase(CoordinatorEntity, SensorEntity):
def update_value(self) -> None:
pass
def __format_id(self, name: str) -> str:
return name.lower().replace(' ', '_')
def __init__(self, model: ReosLockModel, coordinator, name: str) -> None:
super().__init__(coordinator, context=model.lock_id)
self._model = model
self.idx = self._model.lock_id
self._attr_unique_id = f"reos_lock_{self._model.lock_id}_{self.__format_id(name)}"
self._attr_name = f"{self._model.display} {name}"
self.update_value()
@callback
def _handle_coordinator_update(self) -> None:
self._model = self.coordinator.data[self.idx]
self.update_value()
self.async_write_ha_state()
@property
def device_info(self) -> DeviceInfo | None:
return DeviceInfo(
identifiers={(DOMAIN, self.idx)},
)
class TimestampSensorBase(SensorBase):
def __init__(self, model: ReosLockModel, coordinator, name: str) -> None:
super().__init__(model, coordinator, name)
self._attr_device_class = SensorDeviceClass.TIMESTAMP
class ReosDoorUpdatedAt(TimestampSensorBase):
def update_value(self) -> None:
self._attr_native_value = self._model.updated_at
def __init__(self, model: ReosLockModel, coordinator) -> None:
super().__init__(model, coordinator, "Updated At")
class ReosDoorCreatedAt(TimestampSensorBase):
def update_value(self) -> None:
self._attr_native_value = self._model.created_at
def __init__(self, model: ReosLockModel, coordinator) -> None:
super().__init__(model, coordinator, "Created At")
class ReosDoorAvailableFrom(TimestampSensorBase):
def update_value(self) -> None:
self._attr_native_value = self._model.available_from
def __init__(self, model: ReosLockModel, coordinator) -> None:
super().__init__(model, coordinator, "Available From")
class ReosDoorAvailableTo(TimestampSensorBase):
def update_value(self) -> None:
self._attr_native_value = self._model.available_to
def __init__(self, model: ReosLockModel, coordinator) -> None:
super().__init__(model, coordinator, "Available To")