Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """The read-only sensors for APsystems local API integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from APsystemsEZ1 import ReturnOutputData
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.const import UnitOfEnergy, UnitOfPower
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.typing import DiscoveryInfoType, StateType
20 from homeassistant.helpers.update_coordinator import CoordinatorEntity
21 
22 from . import ApSystemsConfigEntry, ApSystemsData
23 from .coordinator import ApSystemsDataCoordinator
24 from .entity import ApSystemsEntity
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  """Describes Apsystens Inverter sensor entity."""
30 
31  value_fn: Callable[[ReturnOutputData], float | None]
32 
33 
34 SENSORS: tuple[ApsystemsLocalApiSensorDescription, ...] = (
36  key="total_power",
37  translation_key="total_power",
38  native_unit_of_measurement=UnitOfPower.WATT,
39  device_class=SensorDeviceClass.POWER,
40  state_class=SensorStateClass.MEASUREMENT,
41  value_fn=lambda c: c.p1 + c.p2,
42  ),
44  key="total_power_p1",
45  translation_key="total_power_p1",
46  native_unit_of_measurement=UnitOfPower.WATT,
47  device_class=SensorDeviceClass.POWER,
48  state_class=SensorStateClass.MEASUREMENT,
49  value_fn=lambda c: c.p1,
50  ),
52  key="total_power_p2",
53  translation_key="total_power_p2",
54  native_unit_of_measurement=UnitOfPower.WATT,
55  device_class=SensorDeviceClass.POWER,
56  state_class=SensorStateClass.MEASUREMENT,
57  value_fn=lambda c: c.p2,
58  ),
60  key="lifetime_production",
61  translation_key="lifetime_production",
62  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
63  device_class=SensorDeviceClass.ENERGY,
64  state_class=SensorStateClass.TOTAL_INCREASING,
65  value_fn=lambda c: c.te1 + c.te2,
66  ),
68  key="lifetime_production_p1",
69  translation_key="lifetime_production_p1",
70  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
71  device_class=SensorDeviceClass.ENERGY,
72  state_class=SensorStateClass.TOTAL_INCREASING,
73  value_fn=lambda c: c.te1,
74  ),
76  key="lifetime_production_p2",
77  translation_key="lifetime_production_p2",
78  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
79  device_class=SensorDeviceClass.ENERGY,
80  state_class=SensorStateClass.TOTAL_INCREASING,
81  value_fn=lambda c: c.te2,
82  ),
84  key="today_production",
85  translation_key="today_production",
86  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
87  device_class=SensorDeviceClass.ENERGY,
88  state_class=SensorStateClass.TOTAL_INCREASING,
89  value_fn=lambda c: c.e1 + c.e2,
90  ),
92  key="today_production_p1",
93  translation_key="today_production_p1",
94  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
95  device_class=SensorDeviceClass.ENERGY,
96  state_class=SensorStateClass.TOTAL_INCREASING,
97  value_fn=lambda c: c.e1,
98  ),
100  key="today_production_p2",
101  translation_key="today_production_p2",
102  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
103  device_class=SensorDeviceClass.ENERGY,
104  state_class=SensorStateClass.TOTAL_INCREASING,
105  value_fn=lambda c: c.e2,
106  ),
107 )
108 
109 
111  hass: HomeAssistant,
112  config_entry: ApSystemsConfigEntry,
113  add_entities: AddEntitiesCallback,
114  discovery_info: DiscoveryInfoType | None = None,
115 ) -> None:
116  """Set up the sensor platform."""
117  config = config_entry.runtime_data
118 
119  add_entities(
121  data=config,
122  entity_description=desc,
123  )
124  for desc in SENSORS
125  )
126 
127 
129  CoordinatorEntity[ApSystemsDataCoordinator], ApSystemsEntity, SensorEntity
130 ):
131  """Base sensor to be used with description."""
132 
133  entity_description: ApsystemsLocalApiSensorDescription
134  _attr_has_entity_name = True
135 
136  def __init__(
137  self,
138  data: ApSystemsData,
139  entity_description: ApsystemsLocalApiSensorDescription,
140  ) -> None:
141  """Initialize the sensor."""
142  super().__init__(data.coordinator)
143  ApSystemsEntity.__init__(self, data)
144  self.entity_descriptionentity_description = entity_description
145  self._attr_unique_id_attr_unique_id = f"{data.device_id}_{entity_description.key}"
146 
147  @property
148  def native_value(self) -> StateType:
149  """Return value of sensor."""
150  return self.entity_descriptionentity_description.value_fn(self.coordinator.data.output_data)
None __init__(self, ApSystemsData data, ApsystemsLocalApiSensorDescription entity_description)
Definition: sensor.py:140
None async_setup_entry(HomeAssistant hass, ApSystemsConfigEntry config_entry, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:115
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40