Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Aseko Pool Live sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from aioaseko import Unit
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.const import UnitOfElectricPotential, UnitOfTemperature
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.typing import StateType
20 
21 from .coordinator import AsekoConfigEntry
22 from .entity import AsekoEntity
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  """Describes an Aseko sensor entity."""
28 
29  value_fn: Callable[[Unit], StateType]
30 
31 
32 SENSORS: list[AsekoSensorEntityDescription] = [
34  key="airTemp",
35  translation_key="air_temperature",
36  device_class=SensorDeviceClass.TEMPERATURE,
37  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
38  state_class=SensorStateClass.MEASUREMENT,
39  value_fn=lambda unit: unit.air_temperature,
40  ),
42  key="electrolyzer",
43  translation_key="electrolyzer",
44  native_unit_of_measurement="g/h",
45  state_class=SensorStateClass.MEASUREMENT,
46  value_fn=lambda unit: unit.electrolyzer,
47  ),
49  key="free_chlorine",
50  translation_key="free_chlorine",
51  native_unit_of_measurement="mg/l",
52  state_class=SensorStateClass.MEASUREMENT,
53  value_fn=lambda unit: unit.cl_free,
54  ),
56  key="ph",
57  device_class=SensorDeviceClass.PH,
58  state_class=SensorStateClass.MEASUREMENT,
59  value_fn=lambda unit: unit.ph,
60  ),
62  key="rx",
63  translation_key="redox",
64  native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT,
65  state_class=SensorStateClass.MEASUREMENT,
66  value_fn=lambda unit: unit.redox,
67  ),
69  key="salinity",
70  translation_key="salinity",
71  native_unit_of_measurement="kg/m³",
72  state_class=SensorStateClass.MEASUREMENT,
73  value_fn=lambda unit: unit.salinity,
74  ),
76  key="waterTemp",
77  translation_key="water_temperature",
78  device_class=SensorDeviceClass.TEMPERATURE,
79  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
80  state_class=SensorStateClass.MEASUREMENT,
81  value_fn=lambda unit: unit.water_temperature,
82  ),
83 ]
84 
85 
87  hass: HomeAssistant,
88  config_entry: AsekoConfigEntry,
89  async_add_entities: AddEntitiesCallback,
90 ) -> None:
91  """Set up the Aseko Pool Live sensors."""
92  coordinator = config_entry.runtime_data
93  units = coordinator.data.values()
95  AsekoSensorEntity(unit, coordinator, description)
96  for description in SENSORS
97  for unit in units
98  if description.value_fn(unit) is not None
99  )
100 
101 
103  """Representation of an Aseko unit sensor entity."""
104 
105  entity_description: AsekoSensorEntityDescription
106 
107  @property
108  def native_value(self) -> StateType:
109  """Return the state of the sensor."""
110  return self.entity_descriptionentity_description.value_fn(self.unitunit)
None async_setup_entry(HomeAssistant hass, AsekoConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:90