Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for the Zeversolar platform."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 import zeversolar
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import EntityCategory, UnitOfEnergy, UnitOfPower
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import DOMAIN
22 from .coordinator import ZeversolarCoordinator
23 from .entity import ZeversolarEntity
24 
25 
26 @dataclass(frozen=True, kw_only=True)
28  """Describes Zeversolar sensor entity."""
29 
30  value_fn: Callable[[zeversolar.ZeverSolarData], zeversolar.kWh | zeversolar.Watt]
31 
32 
33 SENSOR_TYPES = (
35  key="pac",
36  translation_key="pac",
37  native_unit_of_measurement=UnitOfPower.WATT,
38  state_class=SensorStateClass.MEASUREMENT,
39  entity_category=EntityCategory.DIAGNOSTIC,
40  device_class=SensorDeviceClass.POWER,
41  value_fn=lambda data: data.pac,
42  ),
44  key="energy_today",
45  translation_key="energy_today",
46  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
47  state_class=SensorStateClass.TOTAL_INCREASING,
48  device_class=SensorDeviceClass.ENERGY,
49  value_fn=lambda data: data.energy_today,
50  ),
51 )
52 
53 
55  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
56 ) -> None:
57  """Set up the Zeversolar sensor."""
58  coordinator: ZeversolarCoordinator = hass.data[DOMAIN][entry.entry_id]
61  description=description,
62  coordinator=coordinator,
63  )
64  for description in SENSOR_TYPES
65  )
66 
67 
69  """Implementation of the Zeversolar sensor."""
70 
71  entity_description: ZeversolarEntityDescription
72 
73  def __init__(
74  self,
75  *,
76  description: ZeversolarEntityDescription,
77  coordinator: ZeversolarCoordinator,
78  ) -> None:
79  """Initialize the sensor."""
80  self.entity_descriptionentity_description = description
81  super().__init__(coordinator=coordinator)
82  self._attr_unique_id_attr_unique_id = f"{coordinator.data.serial_number}_{description.key}"
83 
84  @property
85  def native_value(self) -> int | float:
86  """Return sensor state."""
87  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
None __init__(self, *ZeversolarEntityDescription description, ZeversolarCoordinator coordinator)
Definition: sensor.py:78
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:56