Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """TOLO Sauna (non-binary, general) sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from tololib import ToloSettings, ToloStatus
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import (
18  PERCENTAGE,
19  EntityCategory,
20  UnitOfTemperature,
21  UnitOfTime,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 
26 from .const import DOMAIN
27 from .coordinator import ToloSaunaUpdateCoordinator
28 from .entity import ToloSaunaCoordinatorEntity
29 
30 
31 @dataclass(frozen=True, kw_only=True)
33  """Class describing TOLO Sensor entities."""
34 
35  getter: Callable[[ToloStatus], int | None]
36  availability_checker: Callable[[ToloSettings, ToloStatus], bool] | None
37 
38  state_class = SensorStateClass.MEASUREMENT
39 
40 
41 SENSORS = (
43  key="water_level",
44  translation_key="water_level",
45  entity_category=EntityCategory.DIAGNOSTIC,
46  native_unit_of_measurement=PERCENTAGE,
47  getter=lambda status: status.water_level_percent,
48  availability_checker=None,
49  ),
51  key="tank_temperature",
52  translation_key="tank_temperature",
53  device_class=SensorDeviceClass.TEMPERATURE,
54  entity_category=EntityCategory.DIAGNOSTIC,
55  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
56  getter=lambda status: status.tank_temperature,
57  availability_checker=None,
58  ),
60  key="power_timer_remaining",
61  translation_key="power_timer_remaining",
62  entity_category=EntityCategory.DIAGNOSTIC,
63  native_unit_of_measurement=UnitOfTime.MINUTES,
64  getter=lambda status: status.power_timer,
65  availability_checker=lambda settings, status: status.power_on
66  and settings.power_timer is not None,
67  ),
69  key="salt_bath_timer_remaining",
70  translation_key="salt_bath_timer_remaining",
71  entity_category=EntityCategory.DIAGNOSTIC,
72  native_unit_of_measurement=UnitOfTime.MINUTES,
73  getter=lambda status: status.salt_bath_timer,
74  availability_checker=lambda settings, status: status.salt_bath_on
75  and settings.salt_bath_timer is not None,
76  ),
78  key="fan_timer_remaining",
79  translation_key="fan_timer_remaining",
80  entity_category=EntityCategory.DIAGNOSTIC,
81  native_unit_of_measurement=UnitOfTime.MINUTES,
82  getter=lambda status: status.fan_timer,
83  availability_checker=lambda settings, status: status.fan_on
84  and settings.fan_timer is not None,
85  ),
86 )
87 
88 
90  hass: HomeAssistant,
91  entry: ConfigEntry,
92  async_add_entities: AddEntitiesCallback,
93 ) -> None:
94  """Set up (non-binary, general) sensors for TOLO Sauna."""
95  coordinator = hass.data[DOMAIN][entry.entry_id]
97  ToloSensorEntity(coordinator, entry, description) for description in SENSORS
98  )
99 
100 
102  """TOLO Number entity."""
103 
104  entity_description: ToloSensorEntityDescription
105 
106  def __init__(
107  self,
108  coordinator: ToloSaunaUpdateCoordinator,
109  entry: ConfigEntry,
110  entity_description: ToloSensorEntityDescription,
111  ) -> None:
112  """Initialize TOLO Number entity."""
113  super().__init__(coordinator, entry)
114  self.entity_descriptionentity_description = entity_description
115  self._attr_unique_id_attr_unique_id = f"{entry.entry_id}_{entity_description.key}"
116 
117  @property
118  def available(self) -> bool:
119  """Return availability of the TOLO sensor."""
120  if self.entity_descriptionentity_description.availability_checker is None:
121  return super().available
122  return self.entity_descriptionentity_description.availability_checker(
123  self.coordinator.data.settings, self.coordinator.data.status
124  )
125 
126  @property
127  def native_value(self) -> int | None:
128  """Return native value of the TOLO sensor."""
129  return self.entity_descriptionentity_description.getter(self.coordinator.data.status)
None __init__(self, ToloSaunaUpdateCoordinator coordinator, ConfigEntry entry, ToloSensorEntityDescription entity_description)
Definition: sensor.py:111
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:93