Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Pure Energie sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
9  DOMAIN as SENSOR_DOMAIN,
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.const import CONF_HOST, UnitOfEnergy, UnitOfPower
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.device_registry import DeviceInfo
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.update_coordinator import CoordinatorEntity
20 
21 from . import PureEnergieConfigEntry
22 from .const import DOMAIN
23 from .coordinator import PureEnergieData, PureEnergieDataUpdateCoordinator
24 
25 
26 @dataclass(frozen=True, kw_only=True)
28  """Describes a Pure Energie sensor entity."""
29 
30  value_fn: Callable[[PureEnergieData], int | float]
31 
32 
33 SENSORS: tuple[PureEnergieSensorEntityDescription, ...] = (
35  key="power_flow",
36  translation_key="power_flow",
37  native_unit_of_measurement=UnitOfPower.WATT,
38  device_class=SensorDeviceClass.POWER,
39  state_class=SensorStateClass.MEASUREMENT,
40  value_fn=lambda data: data.smartbridge.power_flow,
41  ),
43  key="energy_consumption_total",
44  translation_key="energy_consumption_total",
45  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
46  device_class=SensorDeviceClass.ENERGY,
47  state_class=SensorStateClass.TOTAL_INCREASING,
48  value_fn=lambda data: data.smartbridge.energy_consumption_total,
49  ),
51  key="energy_production_total",
52  translation_key="energy_production_total",
53  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
54  device_class=SensorDeviceClass.ENERGY,
55  state_class=SensorStateClass.TOTAL_INCREASING,
56  value_fn=lambda data: data.smartbridge.energy_production_total,
57  ),
58 )
59 
60 
62  hass: HomeAssistant,
63  entry: PureEnergieConfigEntry,
64  async_add_entities: AddEntitiesCallback,
65 ) -> None:
66  """Set up Pure Energie Sensors based on a config entry."""
69  description=description,
70  entry=entry,
71  )
72  for description in SENSORS
73  )
74 
75 
77  CoordinatorEntity[PureEnergieDataUpdateCoordinator], SensorEntity
78 ):
79  """Defines an Pure Energie sensor."""
80 
81  _attr_has_entity_name = True
82  entity_description: PureEnergieSensorEntityDescription
83 
84  def __init__(
85  self,
86  *,
87  description: PureEnergieSensorEntityDescription,
88  entry: PureEnergieConfigEntry,
89  ) -> None:
90  """Initialize Pure Energie sensor."""
91  super().__init__(coordinator=entry.runtime_data)
92  self.entity_identity_identity_id = f"{SENSOR_DOMAIN}.pem_{description.key}"
93  self.entity_descriptionentity_description = description
94  self._attr_unique_id_attr_unique_id = (
95  f"{entry.runtime_data.data.device.n2g_id}_{description.key}"
96  )
97  self._attr_device_info_attr_device_info = DeviceInfo(
98  identifiers={(DOMAIN, entry.runtime_data.data.device.n2g_id)},
99  configuration_url=f"http://{entry.runtime_data.config_entry.data[CONF_HOST]}",
100  sw_version=entry.runtime_data.data.device.firmware,
101  manufacturer=entry.runtime_data.data.device.manufacturer,
102  model=entry.runtime_data.data.device.model,
103  name=entry.title,
104  )
105 
106  @property
107  def native_value(self) -> int | float:
108  """Return the state of the sensor."""
109  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
None __init__(self, *PureEnergieSensorEntityDescription description, PureEnergieConfigEntry entry)
Definition: sensor.py:89
None async_setup_entry(HomeAssistant hass, PureEnergieConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:65