core/homeassistant/components/tolo/sensor.py

135 lines
4.4 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.message_info import SettingsInfo, StatusInfo
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
2022-02-23 11:08:47 +00:00
from homeassistant.const import PERCENTAGE, TEMP_CELSIUS, TIME_MINUTES
2021-11-25 11:29:09 +00:00
from homeassistant.core import HomeAssistant
2021-12-16 12:09:35 +00:00
from homeassistant.helpers.entity import EntityCategory
2021-11-25 11:29:09 +00:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ToloSaunaCoordinatorEntity, ToloSaunaUpdateCoordinator
from .const import DOMAIN
2022-02-23 11:08:47 +00:00
@dataclass
class ToloSensorEntityDescriptionBase:
"""Required values when describing TOLO Sensor entities."""
getter: Callable[[StatusInfo], int | None]
availability_checker: Callable[[SettingsInfo, StatusInfo], bool] | None
@dataclass
class ToloSensorEntityDescription(
SensorEntityDescription, ToloSensorEntityDescriptionBase
):
"""Class describing TOLO Sensor entities."""
state_class = SensorStateClass.MEASUREMENT
SENSORS = (
ToloSensorEntityDescription(
key="water_level",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:waves-arrow-up",
name="Water Level",
native_unit_of_measurement=PERCENTAGE,
getter=lambda status: status.water_level_percent,
availability_checker=None,
),
ToloSensorEntityDescription(
key="tank_temperature",
device_class=SensorDeviceClass.TEMPERATURE,
entity_category=EntityCategory.DIAGNOSTIC,
name="Tank Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
getter=lambda status: status.tank_temperature,
availability_checker=None,
),
ToloSensorEntityDescription(
key="power_timer_remaining",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:power-settings",
name="Power Timer",
native_unit_of_measurement=TIME_MINUTES,
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",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:shaker-outline",
name="Salt Bath Timer",
native_unit_of_measurement=TIME_MINUTES,
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",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:fan-auto",
name="Fan Timer",
native_unit_of_measurement=TIME_MINUTES,
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)