Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Platform for Kostal Plenticore sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from datetime import timedelta
8 import logging
9 from typing import Any
10 
12  SensorDeviceClass,
13  SensorEntity,
14  SensorEntityDescription,
15  SensorStateClass,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.const import (
19  PERCENTAGE,
20  EntityCategory,
21  UnitOfElectricCurrent,
22  UnitOfElectricPotential,
23  UnitOfEnergy,
24  UnitOfPower,
25 )
26 from homeassistant.core import HomeAssistant
27 from homeassistant.helpers.device_registry import DeviceInfo
28 from homeassistant.helpers.entity_platform import AddEntitiesCallback
29 from homeassistant.helpers.typing import StateType
30 from homeassistant.helpers.update_coordinator import CoordinatorEntity
31 
32 from .const import DOMAIN
33 from .coordinator import ProcessDataUpdateCoordinator
34 from .helper import PlenticoreDataFormatter
35 
36 _LOGGER = logging.getLogger(__name__)
37 
38 
39 @dataclass(frozen=True, kw_only=True)
41  """A class that describes plenticore sensor entities."""
42 
43  module_id: str
44  formatter: str
45 
46 
47 SENSOR_PROCESS_DATA = [
49  module_id="devices:local",
50  key="Inverter:State",
51  name="Inverter State",
52  icon="mdi:state-machine",
53  formatter="format_inverter_state",
54  ),
56  module_id="devices:local",
57  key="Dc_P",
58  name="Solar Power",
59  native_unit_of_measurement=UnitOfPower.WATT,
60  device_class=SensorDeviceClass.POWER,
61  entity_registry_enabled_default=True,
62  state_class=SensorStateClass.MEASUREMENT,
63  formatter="format_round",
64  ),
66  module_id="devices:local",
67  key="Grid_P",
68  name="Grid Power",
69  native_unit_of_measurement=UnitOfPower.WATT,
70  device_class=SensorDeviceClass.POWER,
71  entity_registry_enabled_default=True,
72  state_class=SensorStateClass.MEASUREMENT,
73  formatter="format_round",
74  ),
76  module_id="devices:local",
77  key="HomeBat_P",
78  name="Home Power from Battery",
79  native_unit_of_measurement=UnitOfPower.WATT,
80  device_class=SensorDeviceClass.POWER,
81  state_class=SensorStateClass.MEASUREMENT,
82  formatter="format_round",
83  ),
85  module_id="devices:local",
86  key="HomeGrid_P",
87  name="Home Power from Grid",
88  native_unit_of_measurement=UnitOfPower.WATT,
89  device_class=SensorDeviceClass.POWER,
90  state_class=SensorStateClass.MEASUREMENT,
91  formatter="format_round",
92  ),
94  module_id="devices:local",
95  key="HomeOwn_P",
96  name="Home Power from Own",
97  native_unit_of_measurement=UnitOfPower.WATT,
98  device_class=SensorDeviceClass.POWER,
99  state_class=SensorStateClass.MEASUREMENT,
100  formatter="format_round",
101  ),
103  module_id="devices:local",
104  key="HomePv_P",
105  name="Home Power from PV",
106  native_unit_of_measurement=UnitOfPower.WATT,
107  device_class=SensorDeviceClass.POWER,
108  state_class=SensorStateClass.MEASUREMENT,
109  formatter="format_round",
110  ),
112  module_id="devices:local",
113  key="Home_P",
114  name="Home Power",
115  native_unit_of_measurement=UnitOfPower.WATT,
116  device_class=SensorDeviceClass.POWER,
117  state_class=SensorStateClass.MEASUREMENT,
118  formatter="format_round",
119  ),
121  module_id="devices:local:ac",
122  key="P",
123  name="AC Power",
124  native_unit_of_measurement=UnitOfPower.WATT,
125  device_class=SensorDeviceClass.POWER,
126  entity_registry_enabled_default=True,
127  state_class=SensorStateClass.MEASUREMENT,
128  formatter="format_round",
129  ),
131  module_id="devices:local:pv1",
132  key="P",
133  name="DC1 Power",
134  native_unit_of_measurement=UnitOfPower.WATT,
135  device_class=SensorDeviceClass.POWER,
136  state_class=SensorStateClass.MEASUREMENT,
137  formatter="format_round",
138  ),
140  module_id="devices:local:pv1",
141  key="U",
142  name="DC1 Voltage",
143  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
144  device_class=SensorDeviceClass.VOLTAGE,
145  state_class=SensorStateClass.MEASUREMENT,
146  formatter="format_round",
147  ),
149  module_id="devices:local:pv1",
150  key="I",
151  name="DC1 Current",
152  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
153  device_class=SensorDeviceClass.CURRENT,
154  state_class=SensorStateClass.MEASUREMENT,
155  formatter="format_float",
156  ),
158  module_id="devices:local:pv2",
159  key="P",
160  name="DC2 Power",
161  native_unit_of_measurement=UnitOfPower.WATT,
162  device_class=SensorDeviceClass.POWER,
163  state_class=SensorStateClass.MEASUREMENT,
164  formatter="format_round",
165  ),
167  module_id="devices:local:pv2",
168  key="U",
169  name="DC2 Voltage",
170  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
171  device_class=SensorDeviceClass.VOLTAGE,
172  state_class=SensorStateClass.MEASUREMENT,
173  formatter="format_round",
174  ),
176  module_id="devices:local:pv2",
177  key="I",
178  name="DC2 Current",
179  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
180  device_class=SensorDeviceClass.CURRENT,
181  state_class=SensorStateClass.MEASUREMENT,
182  formatter="format_float",
183  ),
185  module_id="devices:local:pv3",
186  key="P",
187  name="DC3 Power",
188  native_unit_of_measurement=UnitOfPower.WATT,
189  device_class=SensorDeviceClass.POWER,
190  state_class=SensorStateClass.MEASUREMENT,
191  formatter="format_round",
192  ),
194  module_id="devices:local:pv3",
195  key="U",
196  name="DC3 Voltage",
197  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
198  device_class=SensorDeviceClass.VOLTAGE,
199  state_class=SensorStateClass.MEASUREMENT,
200  formatter="format_round",
201  ),
203  module_id="devices:local:pv3",
204  key="I",
205  name="DC3 Current",
206  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
207  device_class=SensorDeviceClass.CURRENT,
208  state_class=SensorStateClass.MEASUREMENT,
209  formatter="format_float",
210  ),
212  module_id="devices:local",
213  key="PV2Bat_P",
214  name="PV to Battery Power",
215  native_unit_of_measurement=UnitOfPower.WATT,
216  device_class=SensorDeviceClass.POWER,
217  state_class=SensorStateClass.MEASUREMENT,
218  formatter="format_round",
219  ),
221  module_id="devices:local",
222  key="EM_State",
223  name="Energy Manager State",
224  icon="mdi:state-machine",
225  formatter="format_em_manager_state",
226  ),
228  module_id="devices:local:battery",
229  key="Cycles",
230  name="Battery Cycles",
231  icon="mdi:recycle",
232  state_class=SensorStateClass.TOTAL_INCREASING,
233  formatter="format_round",
234  ),
236  module_id="devices:local:battery",
237  key="P",
238  name="Battery Power",
239  native_unit_of_measurement=UnitOfPower.WATT,
240  device_class=SensorDeviceClass.POWER,
241  state_class=SensorStateClass.MEASUREMENT,
242  formatter="format_round",
243  ),
245  module_id="devices:local:battery",
246  key="SoC",
247  name="Battery SoC",
248  native_unit_of_measurement=PERCENTAGE,
249  device_class=SensorDeviceClass.BATTERY,
250  formatter="format_round",
251  ),
253  module_id="scb:statistic:EnergyFlow",
254  key="Statistic:Autarky:Day",
255  name="Autarky Day",
256  native_unit_of_measurement=PERCENTAGE,
257  icon="mdi:chart-donut",
258  formatter="format_round",
259  ),
261  module_id="scb:statistic:EnergyFlow",
262  key="Statistic:Autarky:Month",
263  name="Autarky Month",
264  native_unit_of_measurement=PERCENTAGE,
265  icon="mdi:chart-donut",
266  formatter="format_round",
267  ),
269  module_id="scb:statistic:EnergyFlow",
270  key="Statistic:Autarky:Total",
271  name="Autarky Total",
272  native_unit_of_measurement=PERCENTAGE,
273  icon="mdi:chart-donut",
274  state_class=SensorStateClass.MEASUREMENT,
275  formatter="format_round",
276  ),
278  module_id="scb:statistic:EnergyFlow",
279  key="Statistic:Autarky:Year",
280  name="Autarky Year",
281  native_unit_of_measurement=PERCENTAGE,
282  icon="mdi:chart-donut",
283  formatter="format_round",
284  ),
286  module_id="scb:statistic:EnergyFlow",
287  key="Statistic:OwnConsumptionRate:Day",
288  name="Own Consumption Rate Day",
289  native_unit_of_measurement=PERCENTAGE,
290  icon="mdi:chart-donut",
291  formatter="format_round",
292  ),
294  module_id="scb:statistic:EnergyFlow",
295  key="Statistic:OwnConsumptionRate:Month",
296  name="Own Consumption Rate Month",
297  native_unit_of_measurement=PERCENTAGE,
298  icon="mdi:chart-donut",
299  formatter="format_round",
300  ),
302  module_id="scb:statistic:EnergyFlow",
303  key="Statistic:OwnConsumptionRate:Total",
304  name="Own Consumption Rate Total",
305  native_unit_of_measurement=PERCENTAGE,
306  icon="mdi:chart-donut",
307  state_class=SensorStateClass.MEASUREMENT,
308  formatter="format_round",
309  ),
311  module_id="scb:statistic:EnergyFlow",
312  key="Statistic:OwnConsumptionRate:Year",
313  name="Own Consumption Rate Year",
314  native_unit_of_measurement=PERCENTAGE,
315  icon="mdi:chart-donut",
316  formatter="format_round",
317  ),
319  module_id="scb:statistic:EnergyFlow",
320  key="Statistic:EnergyHome:Day",
321  name="Home Consumption Day",
322  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
323  device_class=SensorDeviceClass.ENERGY,
324  state_class=SensorStateClass.TOTAL_INCREASING,
325  formatter="format_energy",
326  ),
328  module_id="scb:statistic:EnergyFlow",
329  key="Statistic:EnergyHome:Month",
330  name="Home Consumption Month",
331  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
332  device_class=SensorDeviceClass.ENERGY,
333  state_class=SensorStateClass.TOTAL_INCREASING,
334  formatter="format_energy",
335  ),
337  module_id="scb:statistic:EnergyFlow",
338  key="Statistic:EnergyHome:Year",
339  name="Home Consumption Year",
340  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
341  device_class=SensorDeviceClass.ENERGY,
342  state_class=SensorStateClass.TOTAL_INCREASING,
343  formatter="format_energy",
344  ),
346  module_id="scb:statistic:EnergyFlow",
347  key="Statistic:EnergyHome:Total",
348  name="Home Consumption Total",
349  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
350  device_class=SensorDeviceClass.ENERGY,
351  state_class=SensorStateClass.TOTAL_INCREASING,
352  formatter="format_energy",
353  ),
355  module_id="scb:statistic:EnergyFlow",
356  key="Statistic:EnergyHomeBat:Day",
357  name="Home Consumption from Battery Day",
358  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
359  device_class=SensorDeviceClass.ENERGY,
360  state_class=SensorStateClass.TOTAL_INCREASING,
361  formatter="format_energy",
362  ),
364  module_id="scb:statistic:EnergyFlow",
365  key="Statistic:EnergyHomeBat:Month",
366  name="Home Consumption from Battery Month",
367  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
368  device_class=SensorDeviceClass.ENERGY,
369  state_class=SensorStateClass.TOTAL_INCREASING,
370  formatter="format_energy",
371  ),
373  module_id="scb:statistic:EnergyFlow",
374  key="Statistic:EnergyHomeBat:Year",
375  name="Home Consumption from Battery Year",
376  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
377  device_class=SensorDeviceClass.ENERGY,
378  state_class=SensorStateClass.TOTAL_INCREASING,
379  formatter="format_energy",
380  ),
382  module_id="scb:statistic:EnergyFlow",
383  key="Statistic:EnergyHomeBat:Total",
384  name="Home Consumption from Battery Total",
385  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
386  device_class=SensorDeviceClass.ENERGY,
387  state_class=SensorStateClass.TOTAL_INCREASING,
388  formatter="format_energy",
389  ),
391  module_id="scb:statistic:EnergyFlow",
392  key="Statistic:EnergyHomeGrid:Day",
393  name="Home Consumption from Grid Day",
394  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
395  device_class=SensorDeviceClass.ENERGY,
396  state_class=SensorStateClass.TOTAL_INCREASING,
397  formatter="format_energy",
398  ),
400  module_id="scb:statistic:EnergyFlow",
401  key="Statistic:EnergyHomeGrid:Month",
402  name="Home Consumption from Grid Month",
403  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
404  device_class=SensorDeviceClass.ENERGY,
405  state_class=SensorStateClass.TOTAL_INCREASING,
406  formatter="format_energy",
407  ),
409  module_id="scb:statistic:EnergyFlow",
410  key="Statistic:EnergyHomeGrid:Year",
411  name="Home Consumption from Grid Year",
412  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
413  device_class=SensorDeviceClass.ENERGY,
414  state_class=SensorStateClass.TOTAL_INCREASING,
415  formatter="format_energy",
416  ),
418  module_id="scb:statistic:EnergyFlow",
419  key="Statistic:EnergyHomeGrid:Total",
420  name="Home Consumption from Grid Total",
421  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
422  device_class=SensorDeviceClass.ENERGY,
423  state_class=SensorStateClass.TOTAL_INCREASING,
424  formatter="format_energy",
425  ),
427  module_id="scb:statistic:EnergyFlow",
428  key="Statistic:EnergyHomePv:Day",
429  name="Home Consumption from PV Day",
430  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
431  device_class=SensorDeviceClass.ENERGY,
432  state_class=SensorStateClass.TOTAL_INCREASING,
433  formatter="format_energy",
434  ),
436  module_id="scb:statistic:EnergyFlow",
437  key="Statistic:EnergyHomePv:Month",
438  name="Home Consumption from PV Month",
439  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
440  device_class=SensorDeviceClass.ENERGY,
441  state_class=SensorStateClass.TOTAL_INCREASING,
442  formatter="format_energy",
443  ),
445  module_id="scb:statistic:EnergyFlow",
446  key="Statistic:EnergyHomePv:Year",
447  name="Home Consumption from PV Year",
448  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
449  device_class=SensorDeviceClass.ENERGY,
450  state_class=SensorStateClass.TOTAL_INCREASING,
451  formatter="format_energy",
452  ),
454  module_id="scb:statistic:EnergyFlow",
455  key="Statistic:EnergyHomePv:Total",
456  name="Home Consumption from PV Total",
457  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
458  device_class=SensorDeviceClass.ENERGY,
459  state_class=SensorStateClass.TOTAL_INCREASING,
460  formatter="format_energy",
461  ),
463  module_id="scb:statistic:EnergyFlow",
464  key="Statistic:EnergyPv1:Day",
465  name="Energy PV1 Day",
466  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
467  device_class=SensorDeviceClass.ENERGY,
468  state_class=SensorStateClass.TOTAL_INCREASING,
469  formatter="format_energy",
470  ),
472  module_id="scb:statistic:EnergyFlow",
473  key="Statistic:EnergyPv1:Month",
474  name="Energy PV1 Month",
475  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
476  device_class=SensorDeviceClass.ENERGY,
477  state_class=SensorStateClass.TOTAL_INCREASING,
478  formatter="format_energy",
479  ),
481  module_id="scb:statistic:EnergyFlow",
482  key="Statistic:EnergyPv1:Year",
483  name="Energy PV1 Year",
484  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
485  device_class=SensorDeviceClass.ENERGY,
486  state_class=SensorStateClass.TOTAL_INCREASING,
487  formatter="format_energy",
488  ),
490  module_id="scb:statistic:EnergyFlow",
491  key="Statistic:EnergyPv1:Total",
492  name="Energy PV1 Total",
493  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
494  device_class=SensorDeviceClass.ENERGY,
495  state_class=SensorStateClass.TOTAL_INCREASING,
496  formatter="format_energy",
497  ),
499  module_id="scb:statistic:EnergyFlow",
500  key="Statistic:EnergyPv2:Day",
501  name="Energy PV2 Day",
502  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
503  device_class=SensorDeviceClass.ENERGY,
504  state_class=SensorStateClass.TOTAL_INCREASING,
505  formatter="format_energy",
506  ),
508  module_id="scb:statistic:EnergyFlow",
509  key="Statistic:EnergyPv2:Month",
510  name="Energy PV2 Month",
511  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
512  device_class=SensorDeviceClass.ENERGY,
513  state_class=SensorStateClass.TOTAL_INCREASING,
514  formatter="format_energy",
515  ),
517  module_id="scb:statistic:EnergyFlow",
518  key="Statistic:EnergyPv2:Year",
519  name="Energy PV2 Year",
520  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
521  device_class=SensorDeviceClass.ENERGY,
522  state_class=SensorStateClass.TOTAL_INCREASING,
523  formatter="format_energy",
524  ),
526  module_id="scb:statistic:EnergyFlow",
527  key="Statistic:EnergyPv2:Total",
528  name="Energy PV2 Total",
529  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
530  device_class=SensorDeviceClass.ENERGY,
531  state_class=SensorStateClass.TOTAL_INCREASING,
532  formatter="format_energy",
533  ),
535  module_id="scb:statistic:EnergyFlow",
536  key="Statistic:EnergyPv3:Day",
537  name="Energy PV3 Day",
538  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
539  device_class=SensorDeviceClass.ENERGY,
540  state_class=SensorStateClass.TOTAL_INCREASING,
541  formatter="format_energy",
542  ),
544  module_id="scb:statistic:EnergyFlow",
545  key="Statistic:EnergyPv3:Month",
546  name="Energy PV3 Month",
547  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
548  device_class=SensorDeviceClass.ENERGY,
549  state_class=SensorStateClass.TOTAL_INCREASING,
550  formatter="format_energy",
551  ),
553  module_id="scb:statistic:EnergyFlow",
554  key="Statistic:EnergyPv3:Year",
555  name="Energy PV3 Year",
556  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
557  device_class=SensorDeviceClass.ENERGY,
558  state_class=SensorStateClass.TOTAL_INCREASING,
559  formatter="format_energy",
560  ),
562  module_id="scb:statistic:EnergyFlow",
563  key="Statistic:EnergyPv3:Total",
564  name="Energy PV3 Total",
565  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
566  device_class=SensorDeviceClass.ENERGY,
567  state_class=SensorStateClass.TOTAL_INCREASING,
568  formatter="format_energy",
569  ),
571  module_id="scb:statistic:EnergyFlow",
572  key="Statistic:Yield:Day",
573  name="Energy Yield Day",
574  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
575  device_class=SensorDeviceClass.ENERGY,
576  entity_registry_enabled_default=True,
577  state_class=SensorStateClass.TOTAL_INCREASING,
578  formatter="format_energy",
579  ),
581  module_id="scb:statistic:EnergyFlow",
582  key="Statistic:Yield:Month",
583  name="Energy Yield Month",
584  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
585  device_class=SensorDeviceClass.ENERGY,
586  state_class=SensorStateClass.TOTAL_INCREASING,
587  formatter="format_energy",
588  ),
590  module_id="scb:statistic:EnergyFlow",
591  key="Statistic:Yield:Year",
592  name="Energy Yield Year",
593  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
594  device_class=SensorDeviceClass.ENERGY,
595  state_class=SensorStateClass.TOTAL_INCREASING,
596  formatter="format_energy",
597  ),
599  module_id="scb:statistic:EnergyFlow",
600  key="Statistic:Yield:Total",
601  name="Energy Yield Total",
602  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
603  device_class=SensorDeviceClass.ENERGY,
604  state_class=SensorStateClass.TOTAL_INCREASING,
605  formatter="format_energy",
606  ),
608  module_id="scb:statistic:EnergyFlow",
609  key="Statistic:EnergyChargeGrid:Day",
610  name="Battery Charge from Grid Day",
611  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
612  device_class=SensorDeviceClass.ENERGY,
613  state_class=SensorStateClass.TOTAL_INCREASING,
614  formatter="format_energy",
615  ),
617  module_id="scb:statistic:EnergyFlow",
618  key="Statistic:EnergyChargeGrid:Month",
619  name="Battery Charge from Grid Month",
620  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
621  device_class=SensorDeviceClass.ENERGY,
622  state_class=SensorStateClass.TOTAL_INCREASING,
623  formatter="format_energy",
624  ),
626  module_id="scb:statistic:EnergyFlow",
627  key="Statistic:EnergyChargeGrid:Year",
628  name="Battery Charge from Grid Year",
629  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
630  device_class=SensorDeviceClass.ENERGY,
631  state_class=SensorStateClass.TOTAL_INCREASING,
632  formatter="format_energy",
633  ),
635  module_id="scb:statistic:EnergyFlow",
636  key="Statistic:EnergyChargeGrid:Total",
637  name="Battery Charge from Grid Total",
638  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
639  device_class=SensorDeviceClass.ENERGY,
640  state_class=SensorStateClass.TOTAL_INCREASING,
641  formatter="format_energy",
642  ),
644  module_id="scb:statistic:EnergyFlow",
645  key="Statistic:EnergyChargePv:Day",
646  name="Battery Charge from PV Day",
647  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
648  device_class=SensorDeviceClass.ENERGY,
649  state_class=SensorStateClass.TOTAL_INCREASING,
650  formatter="format_energy",
651  ),
653  module_id="scb:statistic:EnergyFlow",
654  key="Statistic:EnergyChargePv:Month",
655  name="Battery Charge from PV Month",
656  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
657  device_class=SensorDeviceClass.ENERGY,
658  state_class=SensorStateClass.TOTAL_INCREASING,
659  formatter="format_energy",
660  ),
662  module_id="scb:statistic:EnergyFlow",
663  key="Statistic:EnergyChargePv:Year",
664  name="Battery Charge from PV Year",
665  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
666  device_class=SensorDeviceClass.ENERGY,
667  state_class=SensorStateClass.TOTAL_INCREASING,
668  formatter="format_energy",
669  ),
671  module_id="scb:statistic:EnergyFlow",
672  key="Statistic:EnergyChargePv:Total",
673  name="Battery Charge from PV Total",
674  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
675  device_class=SensorDeviceClass.ENERGY,
676  state_class=SensorStateClass.TOTAL_INCREASING,
677  formatter="format_energy",
678  ),
680  module_id="scb:statistic:EnergyFlow",
681  key="Statistic:EnergyDischarge:Day",
682  name="Battery Discharge Day",
683  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
684  device_class=SensorDeviceClass.ENERGY,
685  state_class=SensorStateClass.TOTAL_INCREASING,
686  formatter="format_energy",
687  ),
689  module_id="scb:statistic:EnergyFlow",
690  key="Statistic:EnergyDischarge:Month",
691  name="Battery Discharge Month",
692  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
693  device_class=SensorDeviceClass.ENERGY,
694  state_class=SensorStateClass.TOTAL_INCREASING,
695  formatter="format_energy",
696  ),
698  module_id="scb:statistic:EnergyFlow",
699  key="Statistic:EnergyDischarge:Year",
700  name="Battery Discharge Year",
701  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
702  device_class=SensorDeviceClass.ENERGY,
703  state_class=SensorStateClass.TOTAL_INCREASING,
704  formatter="format_energy",
705  ),
707  module_id="scb:statistic:EnergyFlow",
708  key="Statistic:EnergyDischarge:Total",
709  name="Battery Discharge Total",
710  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
711  device_class=SensorDeviceClass.ENERGY,
712  state_class=SensorStateClass.TOTAL_INCREASING,
713  formatter="format_energy",
714  ),
716  module_id="scb:statistic:EnergyFlow",
717  key="Statistic:EnergyDischargeGrid:Day",
718  name="Energy Discharge to Grid Day",
719  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
720  device_class=SensorDeviceClass.ENERGY,
721  state_class=SensorStateClass.TOTAL_INCREASING,
722  formatter="format_energy",
723  ),
725  module_id="scb:statistic:EnergyFlow",
726  key="Statistic:EnergyDischargeGrid:Month",
727  name="Energy Discharge to Grid Month",
728  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
729  device_class=SensorDeviceClass.ENERGY,
730  state_class=SensorStateClass.TOTAL_INCREASING,
731  formatter="format_energy",
732  ),
734  module_id="scb:statistic:EnergyFlow",
735  key="Statistic:EnergyDischargeGrid:Year",
736  name="Energy Discharge to Grid Year",
737  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
738  device_class=SensorDeviceClass.ENERGY,
739  state_class=SensorStateClass.TOTAL_INCREASING,
740  formatter="format_energy",
741  ),
743  module_id="scb:statistic:EnergyFlow",
744  key="Statistic:EnergyDischargeGrid:Total",
745  name="Energy Discharge to Grid Total",
746  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
747  device_class=SensorDeviceClass.ENERGY,
748  state_class=SensorStateClass.TOTAL_INCREASING,
749  formatter="format_energy",
750  ),
752  module_id="scb:event",
753  key="Event:ActiveErrorCnt",
754  name="Active Alarms",
755  entity_category=EntityCategory.DIAGNOSTIC,
756  entity_registry_enabled_default=False,
757  icon="mdi:alert",
758  formatter="format_round",
759  ),
761  module_id="_virt_",
762  key="pv_P",
763  name="Sum power of all PV DC inputs",
764  native_unit_of_measurement=UnitOfPower.WATT,
765  device_class=SensorDeviceClass.POWER,
766  entity_registry_enabled_default=True,
767  state_class=SensorStateClass.MEASUREMENT,
768  formatter="format_round",
769  ),
771  module_id="_virt_",
772  key="Statistic:EnergyGrid:Total",
773  name="Energy to Grid Total",
774  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
775  device_class=SensorDeviceClass.ENERGY,
776  state_class=SensorStateClass.TOTAL_INCREASING,
777  formatter="format_energy",
778  ),
780  module_id="_virt_",
781  key="Statistic:EnergyGrid:Year",
782  name="Energy to Grid Year",
783  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
784  device_class=SensorDeviceClass.ENERGY,
785  state_class=SensorStateClass.TOTAL_INCREASING,
786  formatter="format_energy",
787  ),
789  module_id="_virt_",
790  key="Statistic:EnergyGrid:Month",
791  name="Energy to Grid Month",
792  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
793  device_class=SensorDeviceClass.ENERGY,
794  state_class=SensorStateClass.TOTAL_INCREASING,
795  formatter="format_energy",
796  ),
798  module_id="_virt_",
799  key="Statistic:EnergyGrid:Day",
800  name="Energy to Grid Day",
801  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
802  device_class=SensorDeviceClass.ENERGY,
803  state_class=SensorStateClass.TOTAL_INCREASING,
804  formatter="format_energy",
805  ),
806 ]
807 
808 
810  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
811 ) -> None:
812  """Add kostal plenticore Sensors."""
813  plenticore = hass.data[DOMAIN][entry.entry_id]
814 
815  entities = []
816 
817  available_process_data = await plenticore.client.get_process_data()
818  process_data_update_coordinator = ProcessDataUpdateCoordinator(
819  hass,
820  _LOGGER,
821  "Process Data",
822  timedelta(seconds=10),
823  plenticore,
824  )
825  for description in SENSOR_PROCESS_DATA:
826  module_id = description.module_id
827  data_id = description.key
828  if (
829  module_id not in available_process_data
830  or data_id not in available_process_data[module_id]
831  ):
832  _LOGGER.debug(
833  "Skipping non existing process data %s/%s", module_id, data_id
834  )
835  continue
836 
837  entities.append(
839  process_data_update_coordinator,
840  description,
841  entry.entry_id,
842  entry.title,
843  plenticore.device_info,
844  )
845  )
846 
847  async_add_entities(entities)
848 
849 
851  CoordinatorEntity[ProcessDataUpdateCoordinator], SensorEntity
852 ):
853  """Representation of a Plenticore data Sensor."""
854 
855  entity_description: PlenticoreSensorEntityDescription
856 
857  def __init__(
858  self,
859  coordinator: ProcessDataUpdateCoordinator,
860  description: PlenticoreSensorEntityDescription,
861  entry_id: str,
862  platform_name: str,
863  device_info: DeviceInfo,
864  ) -> None:
865  """Create a new Sensor Entity for Plenticore process data."""
866  super().__init__(coordinator)
867  self.entity_descriptionentity_description = description
868  self.entry_identry_id = entry_id
869  self.module_idmodule_id = description.module_id
870  self.data_iddata_id = description.key
871 
872  self._formatter: Callable[[str], Any] = PlenticoreDataFormatter.get_method(
873  description.formatter
874  )
875 
876  self._attr_device_info_attr_device_info = device_info
877  self._attr_unique_id_attr_unique_id = f"{entry_id}_{self.module_id}_{self.data_id}"
878  self._attr_name_attr_name = f"{platform_name} {description.name}"
879 
880  @property
881  def available(self) -> bool:
882  """Return if entity is available."""
883  return (
884  super().available
885  and self.coordinator.data is not None
886  and self.module_idmodule_id in self.coordinator.data
887  and self.data_iddata_id in self.coordinator.data[self.module_idmodule_id]
888  )
889 
890  async def async_added_to_hass(self) -> None:
891  """Register this entity on the Update Coordinator."""
892  await super().async_added_to_hass()
893  self.async_on_removeasync_on_remove(
894  self.coordinator.start_fetch_data(self.module_idmodule_id, self.data_iddata_id)
895  )
896 
897  async def async_will_remove_from_hass(self) -> None:
898  """Unregister this entity from the Update Coordinator."""
899  self.coordinator.stop_fetch_data(self.module_idmodule_id, self.data_iddata_id)
900  await super().async_will_remove_from_hass()
901 
902  @property
903  def native_value(self) -> StateType:
904  """Return the state of the sensor."""
905  if self.coordinator.data is None:
906  # None is translated to STATE_UNKNOWN
907  return None
908 
909  raw_value = self.coordinator.data[self.module_idmodule_id][self.data_iddata_id]
910 
911  return self._formatter(raw_value)
None __init__(self, ProcessDataUpdateCoordinator coordinator, PlenticoreSensorEntityDescription description, str entry_id, str platform_name, DeviceInfo device_info)
Definition: sensor.py:864
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:811