Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Platform for solarlog sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from datetime import datetime
8 
9 from solarlog_cli.solarlog_models import InverterData, SolarlogData
10 
12  SensorDeviceClass,
13  SensorEntity,
14  SensorEntityDescription,
15  SensorStateClass,
16 )
17 from homeassistant.const import (
18  PERCENTAGE,
19  UnitOfElectricPotential,
20  UnitOfEnergy,
21  UnitOfPower,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 from homeassistant.helpers.typing import StateType
26 
27 from . import SolarlogConfigEntry
28 from .entity import SolarLogCoordinatorEntity, SolarLogInverterEntity
29 
30 
31 @dataclass(frozen=True, kw_only=True)
33  """Describes Solarlog coordinator sensor entity."""
34 
35  value_fn: Callable[[SolarlogData], StateType | datetime | None]
36 
37 
38 @dataclass(frozen=True, kw_only=True)
40  """Describes Solarlog inverter sensor entity."""
41 
42  value_fn: Callable[[InverterData], float | None]
43 
44 
45 SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] = (
47  key="last_updated",
48  translation_key="last_update",
49  device_class=SensorDeviceClass.TIMESTAMP,
50  value_fn=lambda data: data.last_updated,
51  ),
53  key="power_ac",
54  translation_key="power_ac",
55  native_unit_of_measurement=UnitOfPower.WATT,
56  device_class=SensorDeviceClass.POWER,
57  state_class=SensorStateClass.MEASUREMENT,
58  value_fn=lambda data: data.power_ac,
59  ),
61  key="power_dc",
62  translation_key="power_dc",
63  native_unit_of_measurement=UnitOfPower.WATT,
64  device_class=SensorDeviceClass.POWER,
65  state_class=SensorStateClass.MEASUREMENT,
66  value_fn=lambda data: data.power_dc,
67  ),
69  key="voltage_ac",
70  translation_key="voltage_ac",
71  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
72  device_class=SensorDeviceClass.VOLTAGE,
73  state_class=SensorStateClass.MEASUREMENT,
74  value_fn=lambda data: data.voltage_ac,
75  ),
77  key="voltage_dc",
78  translation_key="voltage_dc",
79  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
80  device_class=SensorDeviceClass.VOLTAGE,
81  state_class=SensorStateClass.MEASUREMENT,
82  value_fn=lambda data: data.voltage_dc,
83  ),
85  key="yield_day",
86  translation_key="yield_day",
87  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
88  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
89  device_class=SensorDeviceClass.ENERGY,
90  state_class=SensorStateClass.TOTAL_INCREASING,
91  suggested_display_precision=3,
92  value_fn=lambda data: data.yield_day,
93  ),
95  key="yield_yesterday",
96  translation_key="yield_yesterday",
97  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
98  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
99  device_class=SensorDeviceClass.ENERGY,
100  suggested_display_precision=3,
101  value_fn=lambda data: data.yield_yesterday,
102  ),
104  key="yield_month",
105  translation_key="yield_month",
106  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
107  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
108  device_class=SensorDeviceClass.ENERGY,
109  state_class=SensorStateClass.TOTAL_INCREASING,
110  suggested_display_precision=3,
111  value_fn=lambda data: data.yield_month,
112  ),
114  key="yield_year",
115  translation_key="yield_year",
116  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
117  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
118  device_class=SensorDeviceClass.ENERGY,
119  state_class=SensorStateClass.TOTAL_INCREASING,
120  value_fn=lambda data: data.yield_year,
121  ),
123  key="yield_total",
124  translation_key="yield_total",
125  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
126  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
127  device_class=SensorDeviceClass.ENERGY,
128  state_class=SensorStateClass.TOTAL,
129  suggested_display_precision=3,
130  value_fn=lambda data: data.yield_total,
131  ),
133  key="consumption_ac",
134  translation_key="consumption_ac",
135  native_unit_of_measurement=UnitOfPower.WATT,
136  device_class=SensorDeviceClass.POWER,
137  state_class=SensorStateClass.MEASUREMENT,
138  value_fn=lambda data: data.consumption_ac,
139  ),
141  key="consumption_day",
142  translation_key="consumption_day",
143  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
144  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
145  device_class=SensorDeviceClass.ENERGY,
146  state_class=SensorStateClass.TOTAL_INCREASING,
147  suggested_display_precision=3,
148  value_fn=lambda data: data.consumption_day,
149  ),
151  key="consumption_yesterday",
152  translation_key="consumption_yesterday",
153  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
154  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
155  device_class=SensorDeviceClass.ENERGY,
156  suggested_display_precision=3,
157  value_fn=lambda data: data.consumption_yesterday,
158  ),
160  key="consumption_month",
161  translation_key="consumption_month",
162  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
163  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
164  device_class=SensorDeviceClass.ENERGY,
165  state_class=SensorStateClass.TOTAL_INCREASING,
166  suggested_display_precision=3,
167  value_fn=lambda data: data.consumption_month,
168  ),
170  key="consumption_year",
171  translation_key="consumption_year",
172  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
173  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
174  device_class=SensorDeviceClass.ENERGY,
175  state_class=SensorStateClass.TOTAL_INCREASING,
176  suggested_display_precision=3,
177  value_fn=lambda data: data.consumption_year,
178  ),
180  key="consumption_total",
181  translation_key="consumption_total",
182  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
183  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
184  device_class=SensorDeviceClass.ENERGY,
185  state_class=SensorStateClass.TOTAL,
186  suggested_display_precision=3,
187  value_fn=lambda data: data.consumption_total,
188  ),
190  key="self_consumption_year",
191  translation_key="self_consumption_year",
192  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
193  device_class=SensorDeviceClass.ENERGY,
194  state_class=SensorStateClass.TOTAL_INCREASING,
195  value_fn=lambda data: data.self_consumption_year,
196  ),
198  key="total_power",
199  translation_key="total_power",
200  native_unit_of_measurement=UnitOfPower.WATT,
201  device_class=SensorDeviceClass.POWER,
202  state_class=SensorStateClass.MEASUREMENT,
203  value_fn=lambda data: data.total_power,
204  ),
206  key="alternator_loss",
207  translation_key="alternator_loss",
208  native_unit_of_measurement=UnitOfPower.WATT,
209  device_class=SensorDeviceClass.POWER,
210  state_class=SensorStateClass.MEASUREMENT,
211  value_fn=lambda data: data.alternator_loss,
212  ),
214  key="capacity",
215  translation_key="capacity",
216  native_unit_of_measurement=PERCENTAGE,
217  device_class=SensorDeviceClass.POWER_FACTOR,
218  state_class=SensorStateClass.MEASUREMENT,
219  suggested_display_precision=1,
220  value_fn=lambda data: data.capacity,
221  ),
223  key="efficiency",
224  translation_key="efficiency",
225  native_unit_of_measurement=PERCENTAGE,
226  device_class=SensorDeviceClass.POWER_FACTOR,
227  state_class=SensorStateClass.MEASUREMENT,
228  suggested_display_precision=1,
229  value_fn=lambda data: data.efficiency,
230  ),
232  key="power_available",
233  translation_key="power_available",
234  native_unit_of_measurement=UnitOfPower.WATT,
235  device_class=SensorDeviceClass.POWER,
236  state_class=SensorStateClass.MEASUREMENT,
237  value_fn=lambda data: data.power_available,
238  ),
240  key="usage",
241  translation_key="usage",
242  native_unit_of_measurement=PERCENTAGE,
243  device_class=SensorDeviceClass.POWER_FACTOR,
244  state_class=SensorStateClass.MEASUREMENT,
245  suggested_display_precision=1,
246  value_fn=lambda data: data.usage,
247  ),
248 )
249 
250 INVERTER_SENSOR_TYPES: tuple[SolarLogInverterSensorEntityDescription, ...] = (
252  key="current_power",
253  translation_key="current_power",
254  native_unit_of_measurement=UnitOfPower.WATT,
255  device_class=SensorDeviceClass.POWER,
256  state_class=SensorStateClass.MEASUREMENT,
257  value_fn=(
258  lambda inverter: None if inverter is None else inverter.current_power
259  ),
260  ),
262  key="consumption_year",
263  translation_key="consumption_year",
264  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
265  suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
266  device_class=SensorDeviceClass.ENERGY,
267  state_class=SensorStateClass.TOTAL_INCREASING,
268  suggested_display_precision=3,
269  value_fn=(
270  lambda inverter: None if inverter is None else inverter.consumption_year
271  ),
272  ),
273 )
274 
275 
277  hass: HomeAssistant,
278  entry: SolarlogConfigEntry,
279  async_add_entities: AddEntitiesCallback,
280 ) -> None:
281  """Add solarlog entry."""
282  coordinator = entry.runtime_data
283 
284  entities: list[SensorEntity] = [
285  SolarLogCoordinatorSensor(coordinator, sensor)
286  for sensor in SOLARLOG_SENSOR_TYPES
287  ]
288 
289  device_data = coordinator.data.inverter_data
290 
291  if device_data:
292  entities.extend(
293  SolarLogInverterSensor(coordinator, sensor, device_id)
294  for device_id in device_data
295  for sensor in INVERTER_SENSOR_TYPES
296  )
297 
298  async_add_entities(entities)
299 
300  def _async_add_new_device(device_id: int) -> None:
302  SolarLogInverterSensor(coordinator, sensor, device_id)
303  for sensor in INVERTER_SENSOR_TYPES
304  )
305 
306  coordinator.new_device_callbacks.append(_async_add_new_device)
307 
308 
310  """Represents a SolarLog sensor."""
311 
312  entity_description: SolarLogCoordinatorSensorEntityDescription
313 
314  @property
315  def native_value(self) -> StateType | datetime:
316  """Return the state for this sensor."""
317 
318  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
319 
320 
322  """Represents a SolarLog inverter sensor."""
323 
324  entity_description: SolarLogInverterSensorEntityDescription
325 
326  @property
327  def native_value(self) -> StateType:
328  """Return the state for this sensor."""
329 
330  return self.entity_descriptionentity_description.value_fn(
331  self.coordinator.data.inverter_data[self.device_iddevice_id]
332  )
None async_setup_entry(HomeAssistant hass, SolarlogConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:280