Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for DROP sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import logging
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import (
17  CONCENTRATION_PARTS_PER_MILLION,
18  PERCENTAGE,
19  EntityCategory,
20  UnitOfPressure,
21  UnitOfTemperature,
22  UnitOfVolume,
23  UnitOfVolumeFlowRate,
24 )
25 from homeassistant.core import HomeAssistant
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 
28 from .const import (
29  CONF_DEVICE_TYPE,
30  DEV_ALERT,
31  DEV_FILTER,
32  DEV_HUB,
33  DEV_LEAK_DETECTOR,
34  DEV_PROTECTION_VALVE,
35  DEV_PUMP_CONTROLLER,
36  DEV_RO_FILTER,
37  DEV_SOFTENER,
38  DOMAIN,
39 )
40 from .coordinator import DROPDeviceDataUpdateCoordinator
41 from .entity import DROPEntity
42 
43 _LOGGER = logging.getLogger(__name__)
44 
45 
46 # Sensor type constants
47 CURRENT_FLOW_RATE = "current_flow_rate"
48 PEAK_FLOW_RATE = "peak_flow_rate"
49 WATER_USED_TODAY = "water_used_today"
50 AVERAGE_WATER_USED = "average_water_used"
51 CAPACITY_REMAINING = "capacity_remaining"
52 CURRENT_SYSTEM_PRESSURE = "current_system_pressure"
53 HIGH_SYSTEM_PRESSURE = "high_system_pressure"
54 LOW_SYSTEM_PRESSURE = "low_system_pressure"
55 BATTERY = "battery"
56 TEMPERATURE = "temperature"
57 INLET_TDS = "inlet_tds"
58 OUTLET_TDS = "outlet_tds"
59 CARTRIDGE_1_LIFE = "cart1"
60 CARTRIDGE_2_LIFE = "cart2"
61 CARTRIDGE_3_LIFE = "cart3"
62 
63 
64 @dataclass(kw_only=True, frozen=True)
66  """Describes DROP sensor entity."""
67 
68  value_fn: Callable[[DROPDeviceDataUpdateCoordinator], float | int | None]
69 
70 
71 SENSORS: list[DROPSensorEntityDescription] = [
73  key=CURRENT_FLOW_RATE,
74  translation_key=CURRENT_FLOW_RATE,
75  device_class=SensorDeviceClass.VOLUME_FLOW_RATE,
76  native_unit_of_measurement=UnitOfVolumeFlowRate.GALLONS_PER_MINUTE,
77  suggested_display_precision=1,
78  value_fn=lambda device: device.drop_api.current_flow_rate(),
79  state_class=SensorStateClass.MEASUREMENT,
80  ),
82  key=PEAK_FLOW_RATE,
83  translation_key=PEAK_FLOW_RATE,
84  device_class=SensorDeviceClass.VOLUME_FLOW_RATE,
85  native_unit_of_measurement=UnitOfVolumeFlowRate.GALLONS_PER_MINUTE,
86  suggested_display_precision=1,
87  value_fn=lambda device: device.drop_api.peak_flow_rate(),
88  state_class=SensorStateClass.MEASUREMENT,
89  ),
91  key=WATER_USED_TODAY,
92  translation_key=WATER_USED_TODAY,
93  device_class=SensorDeviceClass.WATER,
94  native_unit_of_measurement=UnitOfVolume.GALLONS,
95  suggested_display_precision=1,
96  value_fn=lambda device: device.drop_api.water_used_today(),
97  state_class=SensorStateClass.TOTAL,
98  ),
100  key=AVERAGE_WATER_USED,
101  translation_key=AVERAGE_WATER_USED,
102  device_class=SensorDeviceClass.WATER,
103  native_unit_of_measurement=UnitOfVolume.GALLONS,
104  suggested_display_precision=0,
105  value_fn=lambda device: device.drop_api.average_water_used(),
106  state_class=SensorStateClass.TOTAL,
107  ),
109  key=CAPACITY_REMAINING,
110  translation_key=CAPACITY_REMAINING,
111  device_class=SensorDeviceClass.WATER,
112  native_unit_of_measurement=UnitOfVolume.GALLONS,
113  suggested_display_precision=0,
114  value_fn=lambda device: device.drop_api.capacity_remaining(),
115  state_class=SensorStateClass.TOTAL,
116  ),
118  key=CURRENT_SYSTEM_PRESSURE,
119  translation_key=CURRENT_SYSTEM_PRESSURE,
120  device_class=SensorDeviceClass.PRESSURE,
121  native_unit_of_measurement=UnitOfPressure.PSI,
122  suggested_display_precision=1,
123  value_fn=lambda device: device.drop_api.current_system_pressure(),
124  state_class=SensorStateClass.MEASUREMENT,
125  ),
127  key=HIGH_SYSTEM_PRESSURE,
128  translation_key=HIGH_SYSTEM_PRESSURE,
129  device_class=SensorDeviceClass.PRESSURE,
130  native_unit_of_measurement=UnitOfPressure.PSI,
131  suggested_display_precision=0,
132  value_fn=lambda device: device.drop_api.high_system_pressure(),
133  state_class=SensorStateClass.MEASUREMENT,
134  ),
136  key=LOW_SYSTEM_PRESSURE,
137  translation_key=LOW_SYSTEM_PRESSURE,
138  device_class=SensorDeviceClass.PRESSURE,
139  native_unit_of_measurement=UnitOfPressure.PSI,
140  suggested_display_precision=0,
141  value_fn=lambda device: device.drop_api.low_system_pressure(),
142  state_class=SensorStateClass.MEASUREMENT,
143  ),
145  key=BATTERY,
146  device_class=SensorDeviceClass.BATTERY,
147  native_unit_of_measurement=PERCENTAGE,
148  suggested_display_precision=0,
149  value_fn=lambda device: device.drop_api.battery(),
150  state_class=SensorStateClass.MEASUREMENT,
151  entity_category=EntityCategory.DIAGNOSTIC,
152  ),
154  key=TEMPERATURE,
155  device_class=SensorDeviceClass.TEMPERATURE,
156  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
157  suggested_display_precision=1,
158  value_fn=lambda device: device.drop_api.temperature(),
159  state_class=SensorStateClass.MEASUREMENT,
160  ),
162  key=INLET_TDS,
163  translation_key=INLET_TDS,
164  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
165  state_class=SensorStateClass.MEASUREMENT,
166  suggested_display_precision=0,
167  value_fn=lambda device: device.drop_api.inlet_tds(),
168  ),
170  key=OUTLET_TDS,
171  translation_key=OUTLET_TDS,
172  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
173  state_class=SensorStateClass.MEASUREMENT,
174  suggested_display_precision=0,
175  value_fn=lambda device: device.drop_api.outlet_tds(),
176  ),
178  key=CARTRIDGE_1_LIFE,
179  translation_key=CARTRIDGE_1_LIFE,
180  native_unit_of_measurement=PERCENTAGE,
181  state_class=SensorStateClass.MEASUREMENT,
182  entity_category=EntityCategory.DIAGNOSTIC,
183  suggested_display_precision=0,
184  value_fn=lambda device: device.drop_api.cart1(),
185  ),
187  key=CARTRIDGE_2_LIFE,
188  translation_key=CARTRIDGE_2_LIFE,
189  native_unit_of_measurement=PERCENTAGE,
190  state_class=SensorStateClass.MEASUREMENT,
191  entity_category=EntityCategory.DIAGNOSTIC,
192  suggested_display_precision=0,
193  value_fn=lambda device: device.drop_api.cart2(),
194  ),
196  key=CARTRIDGE_3_LIFE,
197  translation_key=CARTRIDGE_3_LIFE,
198  native_unit_of_measurement=PERCENTAGE,
199  state_class=SensorStateClass.MEASUREMENT,
200  entity_category=EntityCategory.DIAGNOSTIC,
201  suggested_display_precision=0,
202  value_fn=lambda device: device.drop_api.cart3(),
203  ),
204 ]
205 
206 # Defines which sensors are used by each device type
207 DEVICE_SENSORS: dict[str, list[str]] = {
208  DEV_HUB: [
209  AVERAGE_WATER_USED,
210  BATTERY,
211  CURRENT_FLOW_RATE,
212  CURRENT_SYSTEM_PRESSURE,
213  HIGH_SYSTEM_PRESSURE,
214  LOW_SYSTEM_PRESSURE,
215  PEAK_FLOW_RATE,
216  WATER_USED_TODAY,
217  ],
218  DEV_SOFTENER: [
219  BATTERY,
220  CAPACITY_REMAINING,
221  CURRENT_FLOW_RATE,
222  CURRENT_SYSTEM_PRESSURE,
223  ],
224  DEV_FILTER: [BATTERY, CURRENT_FLOW_RATE, CURRENT_SYSTEM_PRESSURE],
225  DEV_LEAK_DETECTOR: [BATTERY, TEMPERATURE],
226  DEV_ALERT: [BATTERY, TEMPERATURE],
227  DEV_PROTECTION_VALVE: [
228  BATTERY,
229  CURRENT_FLOW_RATE,
230  CURRENT_SYSTEM_PRESSURE,
231  TEMPERATURE,
232  ],
233  DEV_PUMP_CONTROLLER: [CURRENT_FLOW_RATE, CURRENT_SYSTEM_PRESSURE, TEMPERATURE],
234  DEV_RO_FILTER: [
235  CARTRIDGE_1_LIFE,
236  CARTRIDGE_2_LIFE,
237  CARTRIDGE_3_LIFE,
238  INLET_TDS,
239  OUTLET_TDS,
240  ],
241 }
242 
243 
245  hass: HomeAssistant,
246  config_entry: ConfigEntry,
247  async_add_entities: AddEntitiesCallback,
248 ) -> None:
249  """Set up the DROP sensors from config entry."""
250  _LOGGER.debug(
251  "Set up sensor for device type %s with entry_id is %s",
252  config_entry.data[CONF_DEVICE_TYPE],
253  config_entry.entry_id,
254  )
255 
256  if config_entry.data[CONF_DEVICE_TYPE] in DEVICE_SENSORS:
258  DROPSensor(hass.data[DOMAIN][config_entry.entry_id], sensor)
259  for sensor in SENSORS
260  if sensor.key in DEVICE_SENSORS[config_entry.data[CONF_DEVICE_TYPE]]
261  )
262 
263 
265  """Representation of a DROP sensor."""
266 
267  entity_description: DROPSensorEntityDescription
268 
269  def __init__(
270  self,
271  coordinator: DROPDeviceDataUpdateCoordinator,
272  entity_description: DROPSensorEntityDescription,
273  ) -> None:
274  """Initialize the sensor."""
275  super().__init__(entity_description.key, coordinator)
276  self.entity_descriptionentity_description = entity_description
277 
278  @property
279  def native_value(self) -> float | int | None:
280  """Return the value reported by the sensor."""
281  return self.entity_descriptionentity_description.value_fn(self.coordinator)
None __init__(self, DROPDeviceDataUpdateCoordinator coordinator, DROPSensorEntityDescription entity_description)
Definition: sensor.py:273
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:248