Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensors exposing properties of the softener device."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from aioaquacell import Softener
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.const import PERCENTAGE, UnitOfTime
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.typing import StateType
20 
21 from . import AquacellConfigEntry
22 from .coordinator import AquacellCoordinator
23 from .entity import AquacellEntity
24 
25 PARALLEL_UPDATES = 1
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """Describes Softener sensor entity."""
31 
32  value_fn: Callable[[Softener], StateType]
33 
34 
35 SENSORS: tuple[SoftenerSensorEntityDescription, ...] = (
37  key="salt_left_side_percentage",
38  translation_key="salt_left_side_percentage",
39  state_class=SensorStateClass.MEASUREMENT,
40  native_unit_of_measurement=PERCENTAGE,
41  value_fn=lambda softener: softener.salt.leftPercent,
42  ),
44  key="salt_right_side_percentage",
45  translation_key="salt_right_side_percentage",
46  state_class=SensorStateClass.MEASUREMENT,
47  native_unit_of_measurement=PERCENTAGE,
48  value_fn=lambda softener: softener.salt.rightPercent,
49  ),
51  key="salt_left_side_time_remaining",
52  translation_key="salt_left_side_time_remaining",
53  device_class=SensorDeviceClass.DURATION,
54  native_unit_of_measurement=UnitOfTime.DAYS,
55  value_fn=lambda softener: softener.salt.leftDays,
56  ),
58  key="salt_right_side_time_remaining",
59  translation_key="salt_right_side_time_remaining",
60  device_class=SensorDeviceClass.DURATION,
61  native_unit_of_measurement=UnitOfTime.DAYS,
62  value_fn=lambda softener: softener.salt.rightDays,
63  ),
65  key="battery",
66  device_class=SensorDeviceClass.BATTERY,
67  native_unit_of_measurement=PERCENTAGE,
68  value_fn=lambda softener: softener.battery,
69  ),
71  key="wi_fi_strength",
72  translation_key="wi_fi_strength",
73  value_fn=lambda softener: softener.wifiLevel,
74  device_class=SensorDeviceClass.ENUM,
75  options=[
76  "high",
77  "medium",
78  "low",
79  ],
80  ),
81 )
82 
83 
85  hass: HomeAssistant,
86  config_entry: AquacellConfigEntry,
87  async_add_entities: AddEntitiesCallback,
88 ) -> None:
89  """Set up the sensors."""
90  softeners = config_entry.runtime_data.data
92  SoftenerSensor(config_entry.runtime_data, sensor, softener_key)
93  for sensor in SENSORS
94  for softener_key in softeners
95  )
96 
97 
99  """Softener sensor."""
100 
101  entity_description: SoftenerSensorEntityDescription
102 
103  def __init__(
104  self,
105  coordinator: AquacellCoordinator,
106  description: SoftenerSensorEntityDescription,
107  softener_key: str,
108  ) -> None:
109  """Pass coordinator to CoordinatorEntity."""
110  super().__init__(coordinator, softener_key, description.key)
111 
112  self.entity_descriptionentity_description = description
113 
114  @property
115  def native_value(self) -> StateType:
116  """Return the state of the sensor."""
117  return self.entity_descriptionentity_description.value_fn(self.softenersoftener)
None __init__(self, AquacellCoordinator coordinator, SoftenerSensorEntityDescription description, str softener_key)
Definition: sensor.py:108
None async_setup_entry(HomeAssistant hass, AquacellConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:88