Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Viessmann ViCare sensor device."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from contextlib import suppress
7 from dataclasses import dataclass
8 import logging
9 
10 from PyViCare.PyViCareDevice import Device as PyViCareDevice
11 from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
12 from PyViCare.PyViCareHeatingDevice import (
13  HeatingDeviceWithComponent as PyViCareHeatingDeviceComponent,
14 )
15 from PyViCare.PyViCareUtils import (
16  PyViCareInvalidDataError,
17  PyViCareNotSupportedFeatureError,
18  PyViCareRateLimitError,
19 )
20 import requests
21 
23  SensorDeviceClass,
24  SensorEntity,
25  SensorEntityDescription,
26  SensorStateClass,
27 )
28 from homeassistant.config_entries import ConfigEntry
29 from homeassistant.const import (
30  PERCENTAGE,
31  EntityCategory,
32  UnitOfEnergy,
33  UnitOfPower,
34  UnitOfTemperature,
35  UnitOfTime,
36  UnitOfVolume,
37  UnitOfVolumeFlowRate,
38 )
39 from homeassistant.core import HomeAssistant
40 from homeassistant.helpers.entity_platform import AddEntitiesCallback
41 
42 from .const import (
43  DEVICE_LIST,
44  DOMAIN,
45  VICARE_CUBIC_METER,
46  VICARE_KW,
47  VICARE_KWH,
48  VICARE_PERCENT,
49  VICARE_W,
50  VICARE_WH,
51 )
52 from .entity import ViCareEntity
53 from .types import ViCareDevice, ViCareRequiredKeysMixin
54 from .utils import (
55  get_burners,
56  get_circuits,
57  get_compressors,
58  get_device_serial,
59  is_supported,
60 )
61 
62 _LOGGER = logging.getLogger(__name__)
63 
64 VICARE_UNIT_TO_DEVICE_CLASS = {
65  VICARE_WH: SensorDeviceClass.ENERGY,
66  VICARE_KWH: SensorDeviceClass.ENERGY,
67  VICARE_W: SensorDeviceClass.POWER,
68  VICARE_KW: SensorDeviceClass.POWER,
69  VICARE_CUBIC_METER: SensorDeviceClass.GAS,
70 }
71 
72 VICARE_UNIT_TO_HA_UNIT = {
73  VICARE_PERCENT: PERCENTAGE,
74  VICARE_W: UnitOfPower.WATT,
75  VICARE_KW: UnitOfPower.KILO_WATT,
76  VICARE_WH: UnitOfEnergy.WATT_HOUR,
77  VICARE_KWH: UnitOfEnergy.KILO_WATT_HOUR,
78  VICARE_CUBIC_METER: UnitOfVolume.CUBIC_METERS,
79 }
80 
81 
82 @dataclass(frozen=True)
84  """Describes ViCare sensor entity."""
85 
86  unit_getter: Callable[[PyViCareDevice], str | None] | None = None
87 
88 
89 GLOBAL_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
91  key="outside_temperature",
92  translation_key="outside_temperature",
93  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
94  value_getter=lambda api: api.getOutsideTemperature(),
95  device_class=SensorDeviceClass.TEMPERATURE,
96  state_class=SensorStateClass.MEASUREMENT,
97  ),
99  key="return_temperature",
100  translation_key="return_temperature",
101  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
102  value_getter=lambda api: api.getReturnTemperature(),
103  device_class=SensorDeviceClass.TEMPERATURE,
104  state_class=SensorStateClass.MEASUREMENT,
105  ),
107  key="boiler_temperature",
108  translation_key="boiler_temperature",
109  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
110  value_getter=lambda api: api.getBoilerTemperature(),
111  device_class=SensorDeviceClass.TEMPERATURE,
112  state_class=SensorStateClass.MEASUREMENT,
113  ),
115  key="boiler_supply_temperature",
116  translation_key="boiler_supply_temperature",
117  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
118  value_getter=lambda api: api.getBoilerCommonSupplyTemperature(),
119  device_class=SensorDeviceClass.TEMPERATURE,
120  state_class=SensorStateClass.MEASUREMENT,
121  ),
123  key="primary_circuit_supply_temperature",
124  translation_key="primary_circuit_supply_temperature",
125  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
126  value_getter=lambda api: api.getSupplyTemperaturePrimaryCircuit(),
127  device_class=SensorDeviceClass.TEMPERATURE,
128  state_class=SensorStateClass.MEASUREMENT,
129  ),
131  key="primary_circuit_return_temperature",
132  translation_key="primary_circuit_return_temperature",
133  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
134  value_getter=lambda api: api.getReturnTemperaturePrimaryCircuit(),
135  device_class=SensorDeviceClass.TEMPERATURE,
136  state_class=SensorStateClass.MEASUREMENT,
137  ),
139  key="secondary_circuit_supply_temperature",
140  translation_key="secondary_circuit_supply_temperature",
141  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
142  value_getter=lambda api: api.getSupplyTemperatureSecondaryCircuit(),
143  device_class=SensorDeviceClass.TEMPERATURE,
144  state_class=SensorStateClass.MEASUREMENT,
145  ),
147  key="secondary_circuit_return_temperature",
148  translation_key="secondary_circuit_return_temperature",
149  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
150  value_getter=lambda api: api.getReturnTemperatureSecondaryCircuit(),
151  device_class=SensorDeviceClass.TEMPERATURE,
152  state_class=SensorStateClass.MEASUREMENT,
153  ),
155  key="hotwater_out_temperature",
156  translation_key="hotwater_out_temperature",
157  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
158  value_getter=lambda api: api.getDomesticHotWaterOutletTemperature(),
159  device_class=SensorDeviceClass.TEMPERATURE,
160  state_class=SensorStateClass.MEASUREMENT,
161  ),
163  key="hotwater_max_temperature",
164  translation_key="hotwater_max_temperature",
165  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
166  value_getter=lambda api: api.getDomesticHotWaterMaxTemperature(),
167  device_class=SensorDeviceClass.TEMPERATURE,
168  state_class=SensorStateClass.MEASUREMENT,
169  entity_registry_enabled_default=False,
170  ),
172  key="hotwater_min_temperature",
173  translation_key="hotwater_min_temperature",
174  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
175  value_getter=lambda api: api.getDomesticHotWaterMinTemperature(),
176  device_class=SensorDeviceClass.TEMPERATURE,
177  state_class=SensorStateClass.MEASUREMENT,
178  entity_registry_enabled_default=False,
179  ),
181  key="dhw_storage_temperature",
182  translation_key="dhw_storage_temperature",
183  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
184  value_getter=lambda api: api.getDomesticHotWaterStorageTemperature(),
185  device_class=SensorDeviceClass.TEMPERATURE,
186  state_class=SensorStateClass.MEASUREMENT,
187  ),
189  key="dhw_storage_top_temperature",
190  translation_key="dhw_storage_top_temperature",
191  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
192  value_getter=lambda api: api.getHotWaterStorageTemperatureTop(),
193  device_class=SensorDeviceClass.TEMPERATURE,
194  state_class=SensorStateClass.MEASUREMENT,
195  ),
197  key="dhw_storage_bottom_temperature",
198  translation_key="dhw_storage_bottom_temperature",
199  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
200  value_getter=lambda api: api.getHotWaterStorageTemperatureBottom(),
201  device_class=SensorDeviceClass.TEMPERATURE,
202  state_class=SensorStateClass.MEASUREMENT,
203  ),
205  key="hotwater_gas_consumption_today",
206  translation_key="hotwater_gas_consumption_today",
207  value_getter=lambda api: api.getGasConsumptionDomesticHotWaterToday(),
208  unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
209  state_class=SensorStateClass.TOTAL_INCREASING,
210  ),
212  key="hotwater_gas_consumption_heating_this_week",
213  translation_key="hotwater_gas_consumption_heating_this_week",
214  value_getter=lambda api: api.getGasConsumptionDomesticHotWaterThisWeek(),
215  unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
216  state_class=SensorStateClass.TOTAL_INCREASING,
217  entity_registry_enabled_default=False,
218  ),
220  key="hotwater_gas_consumption_heating_this_month",
221  translation_key="hotwater_gas_consumption_heating_this_month",
222  value_getter=lambda api: api.getGasConsumptionDomesticHotWaterThisMonth(),
223  unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
224  state_class=SensorStateClass.TOTAL_INCREASING,
225  entity_registry_enabled_default=False,
226  ),
228  key="hotwater_gas_consumption_heating_this_year",
229  translation_key="hotwater_gas_consumption_heating_this_year",
230  value_getter=lambda api: api.getGasConsumptionDomesticHotWaterThisYear(),
231  unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
232  state_class=SensorStateClass.TOTAL_INCREASING,
233  entity_registry_enabled_default=False,
234  ),
236  key="gas_consumption_heating_today",
237  translation_key="gas_consumption_heating_today",
238  value_getter=lambda api: api.getGasConsumptionHeatingToday(),
239  unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
240  state_class=SensorStateClass.TOTAL_INCREASING,
241  ),
243  key="gas_consumption_heating_this_week",
244  translation_key="gas_consumption_heating_this_week",
245  value_getter=lambda api: api.getGasConsumptionHeatingThisWeek(),
246  unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
247  state_class=SensorStateClass.TOTAL_INCREASING,
248  entity_registry_enabled_default=False,
249  ),
251  key="gas_consumption_heating_this_month",
252  translation_key="gas_consumption_heating_this_month",
253  value_getter=lambda api: api.getGasConsumptionHeatingThisMonth(),
254  unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
255  state_class=SensorStateClass.TOTAL_INCREASING,
256  entity_registry_enabled_default=False,
257  ),
259  key="gas_consumption_heating_this_year",
260  translation_key="gas_consumption_heating_this_year",
261  value_getter=lambda api: api.getGasConsumptionHeatingThisYear(),
262  unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
263  state_class=SensorStateClass.TOTAL_INCREASING,
264  entity_registry_enabled_default=False,
265  ),
267  key="gas_consumption_fuelcell_today",
268  translation_key="gas_consumption_fuelcell_today",
269  value_getter=lambda api: api.getFuelCellGasConsumptionToday(),
270  unit_getter=lambda api: api.getFuelCellGasConsumptionUnit(),
271  state_class=SensorStateClass.TOTAL_INCREASING,
272  ),
274  key="gas_consumption_fuelcell_this_week",
275  translation_key="gas_consumption_fuelcell_this_week",
276  value_getter=lambda api: api.getFuelCellGasConsumptionThisWeek(),
277  unit_getter=lambda api: api.getFuelCellGasConsumptionUnit(),
278  state_class=SensorStateClass.TOTAL_INCREASING,
279  entity_registry_enabled_default=False,
280  ),
282  key="gas_consumption_fuelcell_this_month",
283  translation_key="gas_consumption_fuelcell_this_month",
284  value_getter=lambda api: api.getFuelCellGasConsumptionThisMonth(),
285  unit_getter=lambda api: api.getFuelCellGasConsumptionUnit(),
286  state_class=SensorStateClass.TOTAL_INCREASING,
287  entity_registry_enabled_default=False,
288  ),
290  key="gas_consumption_fuelcell_this_year",
291  translation_key="gas_consumption_fuelcell_this_year",
292  value_getter=lambda api: api.getFuelCellGasConsumptionThisYear(),
293  unit_getter=lambda api: api.getFuelCellGasConsumptionUnit(),
294  state_class=SensorStateClass.TOTAL_INCREASING,
295  entity_registry_enabled_default=False,
296  ),
298  key="gas_consumption_total_today",
299  translation_key="gas_consumption_total_today",
300  value_getter=lambda api: api.getGasConsumptionTotalToday(),
301  unit_getter=lambda api: api.getGasConsumptionUnit(),
302  state_class=SensorStateClass.TOTAL_INCREASING,
303  ),
305  key="gas_consumption_total_this_week",
306  translation_key="gas_consumption_total_this_week",
307  value_getter=lambda api: api.getGasConsumptionTotalThisWeek(),
308  unit_getter=lambda api: api.getGasConsumptionUnit(),
309  state_class=SensorStateClass.TOTAL_INCREASING,
310  entity_registry_enabled_default=False,
311  ),
313  key="gas_consumption_total_this_month",
314  translation_key="gas_consumption_total_this_month",
315  value_getter=lambda api: api.getGasConsumptionTotalThisMonth(),
316  unit_getter=lambda api: api.getGasConsumptionUnit(),
317  state_class=SensorStateClass.TOTAL_INCREASING,
318  entity_registry_enabled_default=False,
319  ),
321  key="gas_consumption_total_this_year",
322  translation_key="gas_consumption_total_this_year",
323  value_getter=lambda api: api.getGasConsumptionTotalThisYear(),
324  unit_getter=lambda api: api.getGasConsumptionUnit(),
325  state_class=SensorStateClass.TOTAL_INCREASING,
326  entity_registry_enabled_default=False,
327  ),
329  key="gas_summary_consumption_heating_currentday",
330  translation_key="gas_summary_consumption_heating_currentday",
331  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
332  value_getter=lambda api: api.getGasSummaryConsumptionHeatingCurrentDay(),
333  unit_getter=lambda api: api.getGasSummaryConsumptionHeatingUnit(),
334  state_class=SensorStateClass.TOTAL_INCREASING,
335  ),
337  key="gas_summary_consumption_heating_currentmonth",
338  translation_key="gas_summary_consumption_heating_currentmonth",
339  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
340  value_getter=lambda api: api.getGasSummaryConsumptionHeatingCurrentMonth(),
341  unit_getter=lambda api: api.getGasSummaryConsumptionHeatingUnit(),
342  state_class=SensorStateClass.TOTAL_INCREASING,
343  entity_registry_enabled_default=False,
344  ),
346  key="gas_summary_consumption_heating_currentyear",
347  translation_key="gas_summary_consumption_heating_currentyear",
348  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
349  value_getter=lambda api: api.getGasSummaryConsumptionHeatingCurrentYear(),
350  unit_getter=lambda api: api.getGasSummaryConsumptionHeatingUnit(),
351  state_class=SensorStateClass.TOTAL_INCREASING,
352  entity_registry_enabled_default=False,
353  ),
355  key="gas_summary_consumption_heating_lastsevendays",
356  translation_key="gas_summary_consumption_heating_lastsevendays",
357  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
358  value_getter=lambda api: api.getGasSummaryConsumptionHeatingLastSevenDays(),
359  unit_getter=lambda api: api.getGasSummaryConsumptionHeatingUnit(),
360  state_class=SensorStateClass.TOTAL_INCREASING,
361  entity_registry_enabled_default=False,
362  ),
364  key="hotwater_gas_summary_consumption_heating_currentday",
365  translation_key="hotwater_gas_summary_consumption_heating_currentday",
366  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
367  value_getter=lambda api: api.getGasSummaryConsumptionDomesticHotWaterCurrentDay(),
368  unit_getter=lambda api: api.getGasSummaryConsumptionDomesticHotWaterUnit(),
369  state_class=SensorStateClass.TOTAL_INCREASING,
370  ),
372  key="hotwater_gas_summary_consumption_heating_currentmonth",
373  translation_key="hotwater_gas_summary_consumption_heating_currentmonth",
374  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
375  value_getter=lambda api: api.getGasSummaryConsumptionDomesticHotWaterCurrentMonth(),
376  unit_getter=lambda api: api.getGasSummaryConsumptionDomesticHotWaterUnit(),
377  state_class=SensorStateClass.TOTAL_INCREASING,
378  entity_registry_enabled_default=False,
379  ),
381  key="hotwater_gas_summary_consumption_heating_currentyear",
382  translation_key="hotwater_gas_summary_consumption_heating_currentyear",
383  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
384  value_getter=lambda api: api.getGasSummaryConsumptionDomesticHotWaterCurrentYear(),
385  unit_getter=lambda api: api.getGasSummaryConsumptionDomesticHotWaterUnit(),
386  state_class=SensorStateClass.TOTAL_INCREASING,
387  entity_registry_enabled_default=False,
388  ),
390  key="hotwater_gas_summary_consumption_heating_lastsevendays",
391  translation_key="hotwater_gas_summary_consumption_heating_lastsevendays",
392  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
393  value_getter=lambda api: api.getGasSummaryConsumptionDomesticHotWaterLastSevenDays(),
394  unit_getter=lambda api: api.getGasSummaryConsumptionDomesticHotWaterUnit(),
395  state_class=SensorStateClass.TOTAL_INCREASING,
396  entity_registry_enabled_default=False,
397  ),
399  key="energy_summary_consumption_heating_currentday",
400  translation_key="energy_summary_consumption_heating_currentday",
401  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
402  value_getter=lambda api: api.getPowerSummaryConsumptionHeatingCurrentDay(),
403  unit_getter=lambda api: api.getPowerSummaryConsumptionHeatingUnit(),
404  state_class=SensorStateClass.TOTAL_INCREASING,
405  ),
407  key="energy_summary_consumption_heating_currentmonth",
408  translation_key="energy_summary_consumption_heating_currentmonth",
409  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
410  value_getter=lambda api: api.getPowerSummaryConsumptionHeatingCurrentMonth(),
411  unit_getter=lambda api: api.getPowerSummaryConsumptionHeatingUnit(),
412  state_class=SensorStateClass.TOTAL_INCREASING,
413  entity_registry_enabled_default=False,
414  ),
416  key="energy_summary_consumption_heating_currentyear",
417  translation_key="energy_summary_consumption_heating_currentyear",
418  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
419  value_getter=lambda api: api.getPowerSummaryConsumptionHeatingCurrentYear(),
420  unit_getter=lambda api: api.getPowerSummaryConsumptionHeatingUnit(),
421  state_class=SensorStateClass.TOTAL_INCREASING,
422  entity_registry_enabled_default=False,
423  ),
425  key="energy_summary_consumption_heating_lastsevendays",
426  translation_key="energy_summary_consumption_heating_lastsevendays",
427  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
428  value_getter=lambda api: api.getPowerSummaryConsumptionHeatingLastSevenDays(),
429  unit_getter=lambda api: api.getPowerSummaryConsumptionHeatingUnit(),
430  state_class=SensorStateClass.TOTAL_INCREASING,
431  entity_registry_enabled_default=False,
432  ),
434  key="energy_consumption_cooling_today",
435  translation_key="energy_consumption_cooling_today",
436  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
437  value_getter=lambda api: api.getPowerConsumptionCoolingToday(),
438  unit_getter=lambda api: api.getPowerConsumptionCoolingUnit(),
439  state_class=SensorStateClass.TOTAL_INCREASING,
440  ),
442  key="energy_consumption_cooling_this_month",
443  translation_key="energy_consumption_cooling_this_month",
444  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
445  value_getter=lambda api: api.getPowerConsumptionCoolingThisMonth(),
446  unit_getter=lambda api: api.getPowerConsumptionCoolingUnit(),
447  state_class=SensorStateClass.TOTAL_INCREASING,
448  entity_registry_enabled_default=False,
449  ),
451  key="energy_consumption_cooling_this_year",
452  translation_key="energy_consumption_cooling_this_year",
453  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
454  value_getter=lambda api: api.getPowerConsumptionCoolingThisYear(),
455  unit_getter=lambda api: api.getPowerConsumptionCoolingUnit(),
456  state_class=SensorStateClass.TOTAL_INCREASING,
457  entity_registry_enabled_default=False,
458  ),
460  key="energy_dhw_summary_consumption_heating_currentday",
461  translation_key="energy_dhw_summary_consumption_heating_currentday",
462  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
463  value_getter=lambda api: api.getPowerSummaryConsumptionDomesticHotWaterCurrentDay(),
464  unit_getter=lambda api: api.getPowerSummaryConsumptionDomesticHotWaterUnit(),
465  state_class=SensorStateClass.TOTAL_INCREASING,
466  ),
468  key="energy_dhw_summary_consumption_heating_currentmonth",
469  translation_key="energy_dhw_summary_consumption_heating_currentmonth",
470  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
471  value_getter=lambda api: api.getPowerSummaryConsumptionDomesticHotWaterCurrentMonth(),
472  unit_getter=lambda api: api.getPowerSummaryConsumptionDomesticHotWaterUnit(),
473  state_class=SensorStateClass.TOTAL_INCREASING,
474  entity_registry_enabled_default=False,
475  ),
477  key="energy_dhw_summary_consumption_heating_currentyear",
478  translation_key="energy_dhw_summary_consumption_heating_currentyear",
479  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
480  value_getter=lambda api: api.getPowerSummaryConsumptionDomesticHotWaterCurrentYear(),
481  unit_getter=lambda api: api.getPowerSummaryConsumptionDomesticHotWaterUnit(),
482  state_class=SensorStateClass.TOTAL_INCREASING,
483  entity_registry_enabled_default=False,
484  ),
486  key="energy_summary_dhw_consumption_heating_lastsevendays",
487  translation_key="energy_summary_dhw_consumption_heating_lastsevendays",
488  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
489  value_getter=lambda api: api.getPowerSummaryConsumptionDomesticHotWaterLastSevenDays(),
490  unit_getter=lambda api: api.getPowerSummaryConsumptionDomesticHotWaterUnit(),
491  state_class=SensorStateClass.TOTAL_INCREASING,
492  entity_registry_enabled_default=False,
493  ),
495  key="power_production_current",
496  translation_key="power_production_current",
497  native_unit_of_measurement=UnitOfPower.WATT,
498  value_getter=lambda api: api.getPowerProductionCurrent(),
499  device_class=SensorDeviceClass.POWER,
500  state_class=SensorStateClass.MEASUREMENT,
501  ),
503  key="power_production_today",
504  translation_key="power_production_today",
505  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
506  value_getter=lambda api: api.getPowerProductionToday(),
507  device_class=SensorDeviceClass.ENERGY,
508  state_class=SensorStateClass.TOTAL_INCREASING,
509  ),
511  key="power_production_this_week",
512  translation_key="power_production_this_week",
513  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
514  value_getter=lambda api: api.getPowerProductionThisWeek(),
515  device_class=SensorDeviceClass.ENERGY,
516  state_class=SensorStateClass.TOTAL_INCREASING,
517  entity_registry_enabled_default=False,
518  ),
520  key="power_production_this_month",
521  translation_key="power_production_this_month",
522  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
523  value_getter=lambda api: api.getPowerProductionThisMonth(),
524  device_class=SensorDeviceClass.ENERGY,
525  state_class=SensorStateClass.TOTAL_INCREASING,
526  entity_registry_enabled_default=False,
527  ),
529  key="power_production_this_year",
530  translation_key="power_production_this_year",
531  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
532  value_getter=lambda api: api.getPowerProductionThisYear(),
533  device_class=SensorDeviceClass.ENERGY,
534  state_class=SensorStateClass.TOTAL_INCREASING,
535  entity_registry_enabled_default=False,
536  ),
538  key="solar storage temperature",
539  translation_key="solar_storage_temperature",
540  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
541  value_getter=lambda api: api.getSolarStorageTemperature(),
542  device_class=SensorDeviceClass.TEMPERATURE,
543  state_class=SensorStateClass.MEASUREMENT,
544  ),
546  key="collector temperature",
547  translation_key="collector_temperature",
548  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
549  value_getter=lambda api: api.getSolarCollectorTemperature(),
550  device_class=SensorDeviceClass.TEMPERATURE,
551  state_class=SensorStateClass.MEASUREMENT,
552  ),
554  key="solar power production today",
555  translation_key="solar_power_production_today",
556  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
557  value_getter=lambda api: api.getSolarPowerProductionToday(),
558  unit_getter=lambda api: api.getSolarPowerProductionUnit(),
559  device_class=SensorDeviceClass.ENERGY,
560  state_class=SensorStateClass.TOTAL_INCREASING,
561  ),
563  key="solar power production this week",
564  translation_key="solar_power_production_this_week",
565  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
566  value_getter=lambda api: api.getSolarPowerProductionThisWeek(),
567  unit_getter=lambda api: api.getSolarPowerProductionUnit(),
568  device_class=SensorDeviceClass.ENERGY,
569  state_class=SensorStateClass.TOTAL_INCREASING,
570  entity_registry_enabled_default=False,
571  ),
573  key="solar power production this month",
574  translation_key="solar_power_production_this_month",
575  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
576  value_getter=lambda api: api.getSolarPowerProductionThisMonth(),
577  unit_getter=lambda api: api.getSolarPowerProductionUnit(),
578  device_class=SensorDeviceClass.ENERGY,
579  state_class=SensorStateClass.TOTAL_INCREASING,
580  entity_registry_enabled_default=False,
581  ),
583  key="solar power production this year",
584  translation_key="solar_power_production_this_year",
585  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
586  value_getter=lambda api: api.getSolarPowerProductionThisYear(),
587  unit_getter=lambda api: api.getSolarPowerProductionUnit(),
588  device_class=SensorDeviceClass.ENERGY,
589  state_class=SensorStateClass.TOTAL_INCREASING,
590  entity_registry_enabled_default=False,
591  ),
593  key="power consumption today",
594  translation_key="power_consumption_today",
595  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
596  value_getter=lambda api: api.getPowerConsumptionToday(),
597  unit_getter=lambda api: api.getPowerConsumptionUnit(),
598  device_class=SensorDeviceClass.ENERGY,
599  state_class=SensorStateClass.TOTAL_INCREASING,
600  ),
602  key="power consumption this week",
603  translation_key="power_consumption_this_week",
604  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
605  value_getter=lambda api: api.getPowerConsumptionThisWeek(),
606  unit_getter=lambda api: api.getPowerConsumptionUnit(),
607  device_class=SensorDeviceClass.ENERGY,
608  state_class=SensorStateClass.TOTAL_INCREASING,
609  entity_registry_enabled_default=False,
610  ),
612  key="power consumption this month",
613  translation_key="power consumption this month",
614  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
615  value_getter=lambda api: api.getPowerConsumptionThisMonth(),
616  unit_getter=lambda api: api.getPowerConsumptionUnit(),
617  device_class=SensorDeviceClass.ENERGY,
618  state_class=SensorStateClass.TOTAL_INCREASING,
619  entity_registry_enabled_default=False,
620  ),
622  key="power consumption this year",
623  translation_key="power_consumption_this_year",
624  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
625  value_getter=lambda api: api.getPowerConsumptionThisYear(),
626  unit_getter=lambda api: api.getPowerConsumptionUnit(),
627  device_class=SensorDeviceClass.ENERGY,
628  state_class=SensorStateClass.TOTAL_INCREASING,
629  entity_registry_enabled_default=False,
630  ),
632  key="buffer top temperature",
633  translation_key="buffer_top_temperature",
634  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
635  value_getter=lambda api: api.getBufferTopTemperature(),
636  device_class=SensorDeviceClass.TEMPERATURE,
637  state_class=SensorStateClass.MEASUREMENT,
638  ),
640  key="buffer main temperature",
641  translation_key="buffer_main_temperature",
642  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
643  value_getter=lambda api: api.getBufferMainTemperature(),
644  device_class=SensorDeviceClass.TEMPERATURE,
645  state_class=SensorStateClass.MEASUREMENT,
646  ),
648  key="volumetric_flow",
649  translation_key="volumetric_flow",
650  native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
651  value_getter=lambda api: api.getVolumetricFlowReturn() / 1000,
652  entity_category=EntityCategory.DIAGNOSTIC,
653  state_class=SensorStateClass.MEASUREMENT,
654  ),
656  key="ess_state_of_charge",
657  translation_key="ess_state_of_charge",
658  native_unit_of_measurement=PERCENTAGE,
659  device_class=SensorDeviceClass.BATTERY,
660  state_class=SensorStateClass.MEASUREMENT,
661  value_getter=lambda api: api.getElectricalEnergySystemSOC(),
662  unit_getter=lambda api: api.getElectricalEnergySystemSOCUnit(),
663  ),
665  key="ess_power_current",
666  translation_key="ess_power_current",
667  native_unit_of_measurement=UnitOfPower.WATT,
668  state_class=SensorStateClass.MEASUREMENT,
669  value_getter=lambda api: api.getElectricalEnergySystemPower(),
670  unit_getter=lambda api: api.getElectricalEnergySystemPowerUnit(),
671  ),
673  key="ess_state",
674  translation_key="ess_state",
675  device_class=SensorDeviceClass.ENUM,
676  options=["charge", "discharge", "standby"],
677  value_getter=lambda api: api.getElectricalEnergySystemOperationState(),
678  ),
680  key="ess_discharge_today",
681  translation_key="ess_discharge_today",
682  state_class=SensorStateClass.TOTAL_INCREASING,
683  value_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedCurrentDay(),
684  unit_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedUnit(),
685  ),
687  key="ess_discharge_this_week",
688  translation_key="ess_discharge_this_week",
689  state_class=SensorStateClass.TOTAL_INCREASING,
690  value_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedCurrentWeek(),
691  unit_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedUnit(),
692  entity_registry_enabled_default=False,
693  ),
695  key="ess_discharge_this_month",
696  translation_key="ess_discharge_this_month",
697  state_class=SensorStateClass.TOTAL_INCREASING,
698  value_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedCurrentMonth(),
699  unit_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedUnit(),
700  entity_registry_enabled_default=False,
701  ),
703  key="ess_discharge_this_year",
704  translation_key="ess_discharge_this_year",
705  state_class=SensorStateClass.TOTAL_INCREASING,
706  value_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedCurrentYear(),
707  unit_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedUnit(),
708  entity_registry_enabled_default=False,
709  ),
711  key="ess_discharge_total",
712  translation_key="ess_discharge_total",
713  state_class=SensorStateClass.TOTAL_INCREASING,
714  value_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedLifeCycle(),
715  unit_getter=lambda api: api.getElectricalEnergySystemTransferDischargeCumulatedUnit(),
716  entity_registry_enabled_default=False,
717  ),
719  key="pcc_transfer_power_exchange",
720  translation_key="pcc_transfer_power_exchange",
721  native_unit_of_measurement=UnitOfPower.WATT,
722  state_class=SensorStateClass.MEASUREMENT,
723  value_getter=lambda api: api.getPointOfCommonCouplingTransferPowerExchange(),
724  ),
726  key="pcc_energy_consumption",
727  translation_key="pcc_energy_consumption",
728  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
729  state_class=SensorStateClass.TOTAL_INCREASING,
730  value_getter=lambda api: api.getPointOfCommonCouplingTransferConsumptionTotal(),
731  unit_getter=lambda api: api.getPointOfCommonCouplingTransferConsumptionTotalUnit(),
732  ),
734  key="pcc_energy_feed_in",
735  translation_key="pcc_energy_feed_in",
736  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
737  state_class=SensorStateClass.TOTAL_INCREASING,
738  value_getter=lambda api: api.getPointOfCommonCouplingTransferFeedInTotal(),
739  unit_getter=lambda api: api.getPointOfCommonCouplingTransferFeedInTotalUnit(),
740  ),
742  key="photovoltaic_power_production_current",
743  translation_key="photovoltaic_power_production_current",
744  native_unit_of_measurement=UnitOfPower.KILO_WATT,
745  state_class=SensorStateClass.MEASUREMENT,
746  value_getter=lambda api: api.getPhotovoltaicProductionCurrent(),
747  unit_getter=lambda api: api.getPhotovoltaicProductionCurrentUnit(),
748  ),
750  key="photovoltaic_energy_production_today",
751  translation_key="photovoltaic_energy_production_today",
752  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
753  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
754  state_class=SensorStateClass.TOTAL_INCREASING,
755  value_getter=lambda api: api.getPhotovoltaicProductionCumulatedCurrentDay(),
756  unit_getter=lambda api: api.getPhotovoltaicProductionCumulatedUnit(),
757  ),
759  key="photovoltaic_energy_production_this_week",
760  translation_key="photovoltaic_energy_production_this_week",
761  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
762  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
763  state_class=SensorStateClass.TOTAL_INCREASING,
764  value_getter=lambda api: api.getPhotovoltaicProductionCumulatedCurrentWeek(),
765  unit_getter=lambda api: api.getPhotovoltaicProductionCumulatedUnit(),
766  entity_registry_enabled_default=False,
767  ),
769  key="photovoltaic_energy_production_this_month",
770  translation_key="photovoltaic_energy_production_this_month",
771  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
772  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
773  state_class=SensorStateClass.TOTAL_INCREASING,
774  value_getter=lambda api: api.getPhotovoltaicProductionCumulatedCurrentMonth(),
775  unit_getter=lambda api: api.getPhotovoltaicProductionCumulatedUnit(),
776  entity_registry_enabled_default=False,
777  ),
779  key="photovoltaic_energy_production_this_year",
780  translation_key="photovoltaic_energy_production_this_year",
781  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
782  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
783  state_class=SensorStateClass.TOTAL_INCREASING,
784  value_getter=lambda api: api.getPhotovoltaicProductionCumulatedCurrentYear(),
785  unit_getter=lambda api: api.getPhotovoltaicProductionCumulatedUnit(),
786  entity_registry_enabled_default=False,
787  ),
789  key="photovoltaic_energy_production_total",
790  translation_key="photovoltaic_energy_production_total",
791  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
792  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
793  state_class=SensorStateClass.TOTAL_INCREASING,
794  value_getter=lambda api: api.getPhotovoltaicProductionCumulatedLifeCycle(),
795  unit_getter=lambda api: api.getPhotovoltaicProductionCumulatedUnit(),
796  ),
798  key="photovoltaic_status",
799  translation_key="photovoltaic_status",
800  device_class=SensorDeviceClass.ENUM,
801  options=["ready", "production"],
802  value_getter=lambda api: _filter_pv_states(api.getPhotovoltaicStatus()),
803  ),
805  key="room_temperature",
806  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
807  device_class=SensorDeviceClass.TEMPERATURE,
808  state_class=SensorStateClass.MEASUREMENT,
809  value_getter=lambda api: api.getTemperature(),
810  ),
812  key="room_humidity",
813  device_class=SensorDeviceClass.HUMIDITY,
814  native_unit_of_measurement=PERCENTAGE,
815  state_class=SensorStateClass.MEASUREMENT,
816  value_getter=lambda api: api.getHumidity(),
817  ),
818 )
819 
820 CIRCUIT_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
822  key="supply_temperature",
823  translation_key="supply_temperature",
824  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
825  value_getter=lambda api: api.getSupplyTemperature(),
826  device_class=SensorDeviceClass.TEMPERATURE,
827  state_class=SensorStateClass.MEASUREMENT,
828  ),
829 )
830 
831 BURNER_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
833  key="burner_starts",
834  translation_key="burner_starts",
835  value_getter=lambda api: api.getStarts(),
836  entity_category=EntityCategory.DIAGNOSTIC,
837  state_class=SensorStateClass.TOTAL_INCREASING,
838  ),
840  key="burner_hours",
841  translation_key="burner_hours",
842  native_unit_of_measurement=UnitOfTime.HOURS,
843  value_getter=lambda api: api.getHours(),
844  entity_category=EntityCategory.DIAGNOSTIC,
845  state_class=SensorStateClass.TOTAL_INCREASING,
846  ),
848  key="burner_modulation",
849  translation_key="burner_modulation",
850  native_unit_of_measurement=PERCENTAGE,
851  value_getter=lambda api: api.getModulation(),
852  state_class=SensorStateClass.MEASUREMENT,
853  ),
854 )
855 
856 COMPRESSOR_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
858  key="compressor_starts",
859  translation_key="compressor_starts",
860  value_getter=lambda api: api.getStarts(),
861  entity_category=EntityCategory.DIAGNOSTIC,
862  state_class=SensorStateClass.TOTAL_INCREASING,
863  ),
865  key="compressor_hours",
866  translation_key="compressor_hours",
867  native_unit_of_measurement=UnitOfTime.HOURS,
868  value_getter=lambda api: api.getHours(),
869  entity_category=EntityCategory.DIAGNOSTIC,
870  state_class=SensorStateClass.TOTAL_INCREASING,
871  ),
873  key="compressor_hours_loadclass1",
874  translation_key="compressor_hours_loadclass1",
875  native_unit_of_measurement=UnitOfTime.HOURS,
876  value_getter=lambda api: api.getHoursLoadClass1(),
877  entity_category=EntityCategory.DIAGNOSTIC,
878  state_class=SensorStateClass.TOTAL_INCREASING,
879  entity_registry_enabled_default=False,
880  ),
882  key="compressor_hours_loadclass2",
883  translation_key="compressor_hours_loadclass2",
884  native_unit_of_measurement=UnitOfTime.HOURS,
885  value_getter=lambda api: api.getHoursLoadClass2(),
886  entity_category=EntityCategory.DIAGNOSTIC,
887  state_class=SensorStateClass.TOTAL_INCREASING,
888  entity_registry_enabled_default=False,
889  ),
891  key="compressor_hours_loadclass3",
892  translation_key="compressor_hours_loadclass3",
893  native_unit_of_measurement=UnitOfTime.HOURS,
894  value_getter=lambda api: api.getHoursLoadClass3(),
895  entity_category=EntityCategory.DIAGNOSTIC,
896  state_class=SensorStateClass.TOTAL_INCREASING,
897  entity_registry_enabled_default=False,
898  ),
900  key="compressor_hours_loadclass4",
901  translation_key="compressor_hours_loadclass4",
902  native_unit_of_measurement=UnitOfTime.HOURS,
903  value_getter=lambda api: api.getHoursLoadClass4(),
904  entity_category=EntityCategory.DIAGNOSTIC,
905  state_class=SensorStateClass.TOTAL_INCREASING,
906  entity_registry_enabled_default=False,
907  ),
909  key="compressor_hours_loadclass5",
910  translation_key="compressor_hours_loadclass5",
911  native_unit_of_measurement=UnitOfTime.HOURS,
912  value_getter=lambda api: api.getHoursLoadClass5(),
913  entity_category=EntityCategory.DIAGNOSTIC,
914  state_class=SensorStateClass.TOTAL_INCREASING,
915  entity_registry_enabled_default=False,
916  ),
918  key="compressor_phase",
919  translation_key="compressor_phase",
920  value_getter=lambda api: api.getPhase(),
921  entity_category=EntityCategory.DIAGNOSTIC,
922  ),
923 )
924 
925 
926 def _filter_pv_states(state: str) -> str | None:
927  return None if state in ("nothing", "unknown") else state
928 
929 
931  device_list: list[ViCareDevice],
932 ) -> list[ViCareSensor]:
933  """Create ViCare sensor entities for a device."""
934 
935  entities: list[ViCareSensor] = []
936  for device in device_list:
937  # add device entities
938  entities.extend(
939  ViCareSensor(
940  description,
941  get_device_serial(device.api),
942  device.config,
943  device.api,
944  )
945  for description in GLOBAL_SENSORS
946  if is_supported(description.key, description, device.api)
947  )
948  # add component entities
949  for component_list, entity_description_list in (
950  (get_circuits(device.api), CIRCUIT_SENSORS),
951  (get_burners(device.api), BURNER_SENSORS),
952  (get_compressors(device.api), COMPRESSOR_SENSORS),
953  ):
954  entities.extend(
955  ViCareSensor(
956  description,
957  get_device_serial(device.api),
958  device.config,
959  device.api,
960  component,
961  )
962  for component in component_list
963  for description in entity_description_list
964  if is_supported(description.key, description, component)
965  )
966  return entities
967 
968 
970  hass: HomeAssistant,
971  config_entry: ConfigEntry,
972  async_add_entities: AddEntitiesCallback,
973 ) -> None:
974  """Create the ViCare sensor devices."""
975  device_list = hass.data[DOMAIN][config_entry.entry_id][DEVICE_LIST]
976 
978  await hass.async_add_executor_job(
979  _build_entities,
980  device_list,
981  ),
982  # run update to have device_class set depending on unit_of_measurement
983  True,
984  )
985 
986 
988  """Representation of a ViCare sensor."""
989 
990  entity_description: ViCareSensorEntityDescription
991 
992  def __init__(
993  self,
994  description: ViCareSensorEntityDescription,
995  device_serial: str | None,
996  device_config: PyViCareDeviceConfig,
997  device: PyViCareDevice,
998  component: PyViCareHeatingDeviceComponent | None = None,
999  ) -> None:
1000  """Initialize the sensor."""
1001  super().__init__(
1002  description.key, device_serial, device_config, device, component
1003  )
1004  self.entity_descriptionentity_description = description
1005 
1006  @property
1007  def available(self) -> bool:
1008  """Return True if entity is available."""
1009  return self._attr_native_value_attr_native_value is not None
1010 
1011  def update(self) -> None:
1012  """Update state of sensor."""
1013  vicare_unit = None
1014  try:
1015  with suppress(PyViCareNotSupportedFeatureError):
1016  self._attr_native_value_attr_native_value = self.entity_descriptionentity_description.value_getter(
1017  self._api
1018  )
1019 
1020  if self.entity_descriptionentity_description.unit_getter:
1021  vicare_unit = self.entity_descriptionentity_description.unit_getter(self._api)
1022  except requests.exceptions.ConnectionError:
1023  _LOGGER.error("Unable to retrieve data from ViCare server")
1024  except ValueError:
1025  _LOGGER.error("Unable to decode data from ViCare server")
1026  except PyViCareRateLimitError as limit_exception:
1027  _LOGGER.error("Vicare API rate limit exceeded: %s", limit_exception)
1028  except PyViCareInvalidDataError as invalid_data_exception:
1029  _LOGGER.error("Invalid data from Vicare server: %s", invalid_data_exception)
1030 
1031  if vicare_unit is not None:
1032  if (
1033  device_class := VICARE_UNIT_TO_DEVICE_CLASS.get(vicare_unit)
1034  ) is not None:
1035  self._attr_device_class_attr_device_class = device_class
1036  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = VICARE_UNIT_TO_HA_UNIT.get(
1037  vicare_unit
1038  )
None __init__(self, ViCareSensorEntityDescription description, str|None device_serial, PyViCareDeviceConfig device_config, PyViCareDevice device, PyViCareHeatingDeviceComponent|None component=None)
Definition: sensor.py:999
list[ViCareSensor] _build_entities(list[ViCareDevice] device_list)
Definition: sensor.py:932
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:973
str|None _filter_pv_states(str state)
Definition: sensor.py:926
str|None get_device_serial(PyViCareDevice device)
Definition: utils.py:35
list[PyViCareHeatingDeviceComponent] get_compressors(PyViCareDevice device)
Definition: utils.py:92
bool is_supported(str name, ViCareRequiredKeysMixin entity_description, vicare_device)
Definition: utils.py:56
list[PyViCareHeatingDeviceComponent] get_circuits(PyViCareDevice device)
Definition: utils.py:81
list[PyViCareHeatingDeviceComponent] get_burners(PyViCareDevice device)
Definition: utils.py:70