Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Plugwise Sensor component for Home Assistant."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from plugwise.constants import SensorType
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.const import (
16  LIGHT_LUX,
17  PERCENTAGE,
18  EntityCategory,
19  UnitOfElectricPotential,
20  UnitOfEnergy,
21  UnitOfPower,
22  UnitOfPressure,
23  UnitOfTemperature,
24  UnitOfVolume,
25  UnitOfVolumeFlowRate,
26 )
27 from homeassistant.core import HomeAssistant, callback
28 from homeassistant.helpers.entity_platform import AddEntitiesCallback
29 
30 from . import PlugwiseConfigEntry
31 from .coordinator import PlugwiseDataUpdateCoordinator
32 from .entity import PlugwiseEntity
33 
34 
35 @dataclass(frozen=True)
37  """Describes Plugwise sensor entity."""
38 
39  key: SensorType
40 
41 
42 SENSORS: tuple[PlugwiseSensorEntityDescription, ...] = (
44  key="setpoint",
45  translation_key="setpoint",
46  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
47  device_class=SensorDeviceClass.TEMPERATURE,
48  state_class=SensorStateClass.MEASUREMENT,
49  entity_category=EntityCategory.DIAGNOSTIC,
50  ),
52  key="setpoint_high",
53  translation_key="cooling_setpoint",
54  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
55  device_class=SensorDeviceClass.TEMPERATURE,
56  state_class=SensorStateClass.MEASUREMENT,
57  entity_category=EntityCategory.DIAGNOSTIC,
58  ),
60  key="setpoint_low",
61  translation_key="heating_setpoint",
62  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
63  device_class=SensorDeviceClass.TEMPERATURE,
64  state_class=SensorStateClass.MEASUREMENT,
65  entity_category=EntityCategory.DIAGNOSTIC,
66  ),
68  key="temperature",
69  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
70  device_class=SensorDeviceClass.TEMPERATURE,
71  entity_category=EntityCategory.DIAGNOSTIC,
72  state_class=SensorStateClass.MEASUREMENT,
73  ),
75  key="intended_boiler_temperature",
76  translation_key="intended_boiler_temperature",
77  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
78  device_class=SensorDeviceClass.TEMPERATURE,
79  entity_category=EntityCategory.DIAGNOSTIC,
80  state_class=SensorStateClass.MEASUREMENT,
81  ),
83  key="temperature_difference",
84  translation_key="temperature_difference",
85  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
86  device_class=SensorDeviceClass.TEMPERATURE,
87  entity_category=EntityCategory.DIAGNOSTIC,
88  state_class=SensorStateClass.MEASUREMENT,
89  ),
91  key="outdoor_temperature",
92  translation_key="outdoor_temperature",
93  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
94  device_class=SensorDeviceClass.TEMPERATURE,
95  state_class=SensorStateClass.MEASUREMENT,
96  ),
98  key="outdoor_air_temperature",
99  translation_key="outdoor_air_temperature",
100  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
101  device_class=SensorDeviceClass.TEMPERATURE,
102  entity_category=EntityCategory.DIAGNOSTIC,
103  state_class=SensorStateClass.MEASUREMENT,
104  ),
106  key="water_temperature",
107  translation_key="water_temperature",
108  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
109  device_class=SensorDeviceClass.TEMPERATURE,
110  entity_category=EntityCategory.DIAGNOSTIC,
111  state_class=SensorStateClass.MEASUREMENT,
112  ),
114  key="return_temperature",
115  translation_key="return_temperature",
116  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
117  device_class=SensorDeviceClass.TEMPERATURE,
118  entity_category=EntityCategory.DIAGNOSTIC,
119  state_class=SensorStateClass.MEASUREMENT,
120  ),
122  key="electricity_consumed",
123  translation_key="electricity_consumed",
124  native_unit_of_measurement=UnitOfPower.WATT,
125  device_class=SensorDeviceClass.POWER,
126  state_class=SensorStateClass.MEASUREMENT,
127  ),
129  key="electricity_produced",
130  translation_key="electricity_produced",
131  native_unit_of_measurement=UnitOfPower.WATT,
132  device_class=SensorDeviceClass.POWER,
133  state_class=SensorStateClass.MEASUREMENT,
134  entity_registry_enabled_default=False,
135  ),
137  key="electricity_consumed_interval",
138  translation_key="electricity_consumed_interval",
139  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
140  device_class=SensorDeviceClass.ENERGY,
141  state_class=SensorStateClass.TOTAL,
142  ),
144  key="electricity_consumed_peak_interval",
145  translation_key="electricity_consumed_peak_interval",
146  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
147  device_class=SensorDeviceClass.ENERGY,
148  state_class=SensorStateClass.TOTAL,
149  ),
151  key="electricity_consumed_off_peak_interval",
152  translation_key="electricity_consumed_off_peak_interval",
153  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
154  device_class=SensorDeviceClass.ENERGY,
155  state_class=SensorStateClass.TOTAL,
156  ),
158  key="electricity_produced_interval",
159  translation_key="electricity_produced_interval",
160  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
161  device_class=SensorDeviceClass.ENERGY,
162  state_class=SensorStateClass.TOTAL,
163  entity_registry_enabled_default=False,
164  ),
166  key="electricity_produced_peak_interval",
167  translation_key="electricity_produced_peak_interval",
168  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
169  device_class=SensorDeviceClass.ENERGY,
170  state_class=SensorStateClass.TOTAL,
171  ),
173  key="electricity_produced_off_peak_interval",
174  translation_key="electricity_produced_off_peak_interval",
175  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
176  device_class=SensorDeviceClass.ENERGY,
177  state_class=SensorStateClass.TOTAL,
178  ),
180  key="electricity_consumed_point",
181  translation_key="electricity_consumed_point",
182  device_class=SensorDeviceClass.POWER,
183  native_unit_of_measurement=UnitOfPower.WATT,
184  state_class=SensorStateClass.MEASUREMENT,
185  ),
187  key="electricity_consumed_off_peak_point",
188  translation_key="electricity_consumed_off_peak_point",
189  native_unit_of_measurement=UnitOfPower.WATT,
190  device_class=SensorDeviceClass.POWER,
191  state_class=SensorStateClass.MEASUREMENT,
192  ),
194  key="electricity_consumed_peak_point",
195  translation_key="electricity_consumed_peak_point",
196  native_unit_of_measurement=UnitOfPower.WATT,
197  device_class=SensorDeviceClass.POWER,
198  state_class=SensorStateClass.MEASUREMENT,
199  ),
201  key="electricity_consumed_off_peak_cumulative",
202  translation_key="electricity_consumed_off_peak_cumulative",
203  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
204  device_class=SensorDeviceClass.ENERGY,
205  state_class=SensorStateClass.TOTAL_INCREASING,
206  ),
208  key="electricity_consumed_peak_cumulative",
209  translation_key="electricity_consumed_peak_cumulative",
210  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
211  device_class=SensorDeviceClass.ENERGY,
212  state_class=SensorStateClass.TOTAL_INCREASING,
213  ),
215  key="electricity_produced_point",
216  translation_key="electricity_produced_point",
217  device_class=SensorDeviceClass.POWER,
218  native_unit_of_measurement=UnitOfPower.WATT,
219  state_class=SensorStateClass.MEASUREMENT,
220  ),
222  key="electricity_produced_off_peak_point",
223  translation_key="electricity_produced_off_peak_point",
224  native_unit_of_measurement=UnitOfPower.WATT,
225  device_class=SensorDeviceClass.POWER,
226  state_class=SensorStateClass.MEASUREMENT,
227  ),
229  key="electricity_produced_peak_point",
230  translation_key="electricity_produced_peak_point",
231  native_unit_of_measurement=UnitOfPower.WATT,
232  device_class=SensorDeviceClass.POWER,
233  state_class=SensorStateClass.MEASUREMENT,
234  ),
236  key="electricity_produced_off_peak_cumulative",
237  translation_key="electricity_produced_off_peak_cumulative",
238  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
239  device_class=SensorDeviceClass.ENERGY,
240  state_class=SensorStateClass.TOTAL_INCREASING,
241  ),
243  key="electricity_produced_peak_cumulative",
244  translation_key="electricity_produced_peak_cumulative",
245  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
246  device_class=SensorDeviceClass.ENERGY,
247  state_class=SensorStateClass.TOTAL_INCREASING,
248  ),
250  key="electricity_phase_one_consumed",
251  translation_key="electricity_phase_one_consumed",
252  device_class=SensorDeviceClass.POWER,
253  native_unit_of_measurement=UnitOfPower.WATT,
254  state_class=SensorStateClass.MEASUREMENT,
255  ),
257  key="electricity_phase_two_consumed",
258  translation_key="electricity_phase_two_consumed",
259  device_class=SensorDeviceClass.POWER,
260  native_unit_of_measurement=UnitOfPower.WATT,
261  state_class=SensorStateClass.MEASUREMENT,
262  ),
264  key="electricity_phase_three_consumed",
265  translation_key="electricity_phase_three_consumed",
266  device_class=SensorDeviceClass.POWER,
267  native_unit_of_measurement=UnitOfPower.WATT,
268  state_class=SensorStateClass.MEASUREMENT,
269  ),
271  key="electricity_phase_one_produced",
272  translation_key="electricity_phase_one_produced",
273  device_class=SensorDeviceClass.POWER,
274  native_unit_of_measurement=UnitOfPower.WATT,
275  state_class=SensorStateClass.MEASUREMENT,
276  ),
278  key="electricity_phase_two_produced",
279  translation_key="electricity_phase_two_produced",
280  device_class=SensorDeviceClass.POWER,
281  native_unit_of_measurement=UnitOfPower.WATT,
282  state_class=SensorStateClass.MEASUREMENT,
283  ),
285  key="electricity_phase_three_produced",
286  translation_key="electricity_phase_three_produced",
287  device_class=SensorDeviceClass.POWER,
288  native_unit_of_measurement=UnitOfPower.WATT,
289  state_class=SensorStateClass.MEASUREMENT,
290  ),
292  key="voltage_phase_one",
293  translation_key="voltage_phase_one",
294  device_class=SensorDeviceClass.VOLTAGE,
295  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
296  state_class=SensorStateClass.MEASUREMENT,
297  entity_registry_enabled_default=False,
298  ),
300  key="voltage_phase_two",
301  translation_key="voltage_phase_two",
302  device_class=SensorDeviceClass.VOLTAGE,
303  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
304  state_class=SensorStateClass.MEASUREMENT,
305  entity_registry_enabled_default=False,
306  ),
308  key="voltage_phase_three",
309  translation_key="voltage_phase_three",
310  device_class=SensorDeviceClass.VOLTAGE,
311  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
312  state_class=SensorStateClass.MEASUREMENT,
313  entity_registry_enabled_default=False,
314  ),
316  key="gas_consumed_interval",
317  translation_key="gas_consumed_interval",
318  native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
319  state_class=SensorStateClass.MEASUREMENT,
320  ),
322  key="gas_consumed_cumulative",
323  translation_key="gas_consumed_cumulative",
324  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
325  device_class=SensorDeviceClass.GAS,
326  state_class=SensorStateClass.TOTAL,
327  ),
329  key="net_electricity_point",
330  translation_key="net_electricity_point",
331  native_unit_of_measurement=UnitOfPower.WATT,
332  device_class=SensorDeviceClass.POWER,
333  state_class=SensorStateClass.MEASUREMENT,
334  ),
336  key="net_electricity_cumulative",
337  translation_key="net_electricity_cumulative",
338  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
339  device_class=SensorDeviceClass.ENERGY,
340  state_class=SensorStateClass.TOTAL,
341  ),
343  key="battery",
344  native_unit_of_measurement=PERCENTAGE,
345  device_class=SensorDeviceClass.BATTERY,
346  entity_category=EntityCategory.DIAGNOSTIC,
347  state_class=SensorStateClass.MEASUREMENT,
348  ),
350  key="illuminance",
351  native_unit_of_measurement=LIGHT_LUX,
352  device_class=SensorDeviceClass.ILLUMINANCE,
353  state_class=SensorStateClass.MEASUREMENT,
354  entity_category=EntityCategory.DIAGNOSTIC,
355  ),
357  key="modulation_level",
358  translation_key="modulation_level",
359  native_unit_of_measurement=PERCENTAGE,
360  entity_category=EntityCategory.DIAGNOSTIC,
361  state_class=SensorStateClass.MEASUREMENT,
362  ),
364  key="valve_position",
365  translation_key="valve_position",
366  entity_category=EntityCategory.DIAGNOSTIC,
367  native_unit_of_measurement=PERCENTAGE,
368  state_class=SensorStateClass.MEASUREMENT,
369  ),
371  key="water_pressure",
372  translation_key="water_pressure",
373  native_unit_of_measurement=UnitOfPressure.BAR,
374  device_class=SensorDeviceClass.PRESSURE,
375  entity_category=EntityCategory.DIAGNOSTIC,
376  state_class=SensorStateClass.MEASUREMENT,
377  ),
379  key="humidity",
380  native_unit_of_measurement=PERCENTAGE,
381  device_class=SensorDeviceClass.HUMIDITY,
382  state_class=SensorStateClass.MEASUREMENT,
383  ),
385  key="dhw_temperature",
386  translation_key="dhw_temperature",
387  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
388  device_class=SensorDeviceClass.TEMPERATURE,
389  entity_category=EntityCategory.DIAGNOSTIC,
390  state_class=SensorStateClass.MEASUREMENT,
391  ),
393  key="domestic_hot_water_setpoint",
394  translation_key="domestic_hot_water_setpoint",
395  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
396  device_class=SensorDeviceClass.TEMPERATURE,
397  entity_category=EntityCategory.DIAGNOSTIC,
398  state_class=SensorStateClass.MEASUREMENT,
399  ),
400 )
401 
402 
404  hass: HomeAssistant,
405  entry: PlugwiseConfigEntry,
406  async_add_entities: AddEntitiesCallback,
407 ) -> None:
408  """Set up the Smile sensors from a config entry."""
409  coordinator = entry.runtime_data
410 
411  @callback
412  def _add_entities() -> None:
413  """Add Entities."""
414  if not coordinator.new_devices:
415  return
416 
418  PlugwiseSensorEntity(coordinator, device_id, description)
419  for device_id in coordinator.new_devices
420  if (sensors := coordinator.data.devices[device_id].get("sensors"))
421  for description in SENSORS
422  if description.key in sensors
423  )
424 
425  _add_entities()
426  entry.async_on_unload(coordinator.async_add_listener(_add_entities))
427 
428 
430  """Represent Plugwise Sensors."""
431 
432  entity_description: PlugwiseSensorEntityDescription
433 
434  def __init__(
435  self,
436  coordinator: PlugwiseDataUpdateCoordinator,
437  device_id: str,
438  description: PlugwiseSensorEntityDescription,
439  ) -> None:
440  """Initialise the sensor."""
441  super().__init__(coordinator, device_id)
442  self._attr_unique_id_attr_unique_id = f"{device_id}-{description.key}"
443  self.entity_descriptionentity_description = description
444 
445  @property
446  def native_value(self) -> int | float:
447  """Return the value reported by the sensor."""
448  return self.devicedevice["sensors"][self.entity_descriptionentity_description.key]
None __init__(self, PlugwiseDataUpdateCoordinator coordinator, str device_id, PlugwiseSensorEntityDescription description)
Definition: sensor.py:439
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, PlugwiseConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:407