core/homeassistant/components/tolo/sensor.py

129 lines
4.3 KiB
Python
Raw Normal View History

2021-11-25 11:29:09 +00:00
"""TOLO Sauna (non-binary, general) sensors."""
2022-02-23 11:08:47 +00:00
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from tololib import ToloSettings, ToloStatus
2021-11-25 11:29:09 +00:00
2021-12-16 12:09:35 +00:00
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
2022-02-23 11:08:47 +00:00
SensorEntityDescription,
2021-12-16 12:09:35 +00:00
SensorStateClass,
2021-11-25 11:29:09 +00:00
)
2021-12-16 12:09:35 +00:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
EntityCategory,
UnitOfTemperature,
UnitOfTime,
)
2021-11-25 11:29:09 +00:00
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ToloSaunaCoordinatorEntity, ToloSaunaUpdateCoordinator
from .const import DOMAIN
@dataclass(frozen=True, kw_only=True)
class ToloSensorEntityDescription(SensorEntityDescription):
"""Class describing TOLO Sensor entities."""
2022-02-23 11:08:47 +00:00
getter: Callable[[ToloStatus], int | None]
availability_checker: Callable[[ToloSettings, ToloStatus], bool] | None
2022-02-23 11:08:47 +00:00
state_class = SensorStateClass.MEASUREMENT
SENSORS = (
ToloSensorEntityDescription(
key="water_level",
2023-07-22 15:39:11 +00:00
translation_key="water_level",
2022-02-23 11:08:47 +00:00
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=PERCENTAGE,
getter=lambda status: status.water_level_percent,
availability_checker=None,
),
ToloSensorEntityDescription(
key="tank_temperature",
2023-07-22 15:39:11 +00:00
translation_key="tank_temperature",
2022-02-23 11:08:47 +00:00
device_class=SensorDeviceClass.TEMPERATURE,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
2022-02-23 11:08:47 +00:00
getter=lambda status: status.tank_temperature,
availability_checker=None,
),
ToloSensorEntityDescription(
key="power_timer_remaining",
2023-07-22 15:39:11 +00:00
translation_key="power_timer_remaining",
2022-02-23 11:08:47 +00:00
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfTime.MINUTES,
2022-02-23 11:08:47 +00:00
getter=lambda status: status.power_timer,
availability_checker=lambda settings, status: status.power_on
and settings.power_timer is not None,
),
ToloSensorEntityDescription(
key="salt_bath_timer_remaining",
2023-07-22 15:39:11 +00:00
translation_key="salt_bath_timer_remaining",
2022-02-23 11:08:47 +00:00
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfTime.MINUTES,
2022-02-23 11:08:47 +00:00
getter=lambda status: status.salt_bath_timer,
availability_checker=lambda settings, status: status.salt_bath_on
and settings.salt_bath_timer is not None,
),
ToloSensorEntityDescription(
key="fan_timer_remaining",
2023-07-22 15:39:11 +00:00
translation_key="fan_timer_remaining",
2022-02-23 11:08:47 +00:00
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=UnitOfTime.MINUTES,
2022-02-23 11:08:47 +00:00
getter=lambda status: status.fan_timer,
availability_checker=lambda settings, status: status.fan_on
and settings.fan_timer is not None,
),
)
2021-11-25 11:29:09 +00:00
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up (non-binary, general) sensors for TOLO Sauna."""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
2022-02-23 11:08:47 +00:00
ToloSensorEntity(coordinator, entry, description) for description in SENSORS
2021-11-25 11:29:09 +00:00
)
2022-02-23 11:08:47 +00:00
class ToloSensorEntity(ToloSaunaCoordinatorEntity, SensorEntity):
"""TOLO Number entity."""
2021-11-25 11:29:09 +00:00
2022-02-23 11:08:47 +00:00
entity_description: ToloSensorEntityDescription
2021-11-25 11:29:09 +00:00
def __init__(
2022-02-23 11:08:47 +00:00
self,
coordinator: ToloSaunaUpdateCoordinator,
entry: ConfigEntry,
entity_description: ToloSensorEntityDescription,
2021-11-25 11:29:09 +00:00
) -> None:
2022-02-23 11:08:47 +00:00
"""Initialize TOLO Number entity."""
2021-11-25 11:29:09 +00:00
super().__init__(coordinator, entry)
2022-02-23 11:08:47 +00:00
self.entity_description = entity_description
self._attr_unique_id = f"{entry.entry_id}_{entity_description.key}"
2021-11-25 11:29:09 +00:00
@property
2022-02-23 11:08:47 +00:00
def available(self) -> bool:
"""Return availability of the TOLO sensor."""
if self.entity_description.availability_checker is None:
return super().available
return self.entity_description.availability_checker(
self.coordinator.data.settings, self.coordinator.data.status
)
2021-11-25 11:29:09 +00:00
@property
2022-02-23 11:08:47 +00:00
def native_value(self) -> int | None:
"""Return native value of the TOLO sensor."""
return self.entity_description.getter(self.coordinator.data.status)