Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Platform for sensor integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from technove import Station as TechnoVEStation, Status
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.const import (
17  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
18  EntityCategory,
19  UnitOfElectricCurrent,
20  UnitOfElectricPotential,
21  UnitOfEnergy,
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 TechnoVEConfigEntry
28 from .coordinator import TechnoVEDataUpdateCoordinator
29 from .entity import TechnoVEEntity
30 
31 STATUS_TYPE = [s.value for s in Status if s != Status.UNKNOWN]
32 
33 
34 @dataclass(frozen=True, kw_only=True)
36  """Describes TechnoVE sensor entity."""
37 
38  value_fn: Callable[[TechnoVEStation], StateType]
39 
40 
41 SENSORS: tuple[TechnoVESensorEntityDescription, ...] = (
43  key="voltage_in",
44  translation_key="voltage_in",
45  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
46  device_class=SensorDeviceClass.VOLTAGE,
47  state_class=SensorStateClass.MEASUREMENT,
48  entity_category=EntityCategory.DIAGNOSTIC,
49  value_fn=lambda station: station.info.voltage_in,
50  ),
52  key="voltage_out",
53  translation_key="voltage_out",
54  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
55  device_class=SensorDeviceClass.VOLTAGE,
56  state_class=SensorStateClass.MEASUREMENT,
57  entity_category=EntityCategory.DIAGNOSTIC,
58  value_fn=lambda station: station.info.voltage_out,
59  ),
61  key="max_station_current",
62  translation_key="max_station_current",
63  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
64  device_class=SensorDeviceClass.CURRENT,
65  state_class=SensorStateClass.MEASUREMENT,
66  entity_category=EntityCategory.DIAGNOSTIC,
67  value_fn=lambda station: station.info.max_station_current,
68  ),
70  key="current",
71  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
72  device_class=SensorDeviceClass.CURRENT,
73  state_class=SensorStateClass.MEASUREMENT,
74  entity_category=EntityCategory.DIAGNOSTIC,
75  value_fn=lambda station: station.info.current,
76  ),
78  key="energy_total",
79  translation_key="energy_total",
80  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
81  device_class=SensorDeviceClass.ENERGY,
82  state_class=SensorStateClass.TOTAL,
83  entity_category=EntityCategory.DIAGNOSTIC,
84  value_fn=lambda station: station.info.energy_total,
85  ),
87  key="energy_session",
88  translation_key="energy_session",
89  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
90  device_class=SensorDeviceClass.ENERGY,
91  state_class=SensorStateClass.TOTAL_INCREASING,
92  entity_category=EntityCategory.DIAGNOSTIC,
93  value_fn=lambda station: station.info.energy_session,
94  ),
96  key="rssi",
97  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
98  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
99  state_class=SensorStateClass.MEASUREMENT,
100  entity_category=EntityCategory.DIAGNOSTIC,
101  entity_registry_enabled_default=False,
102  value_fn=lambda station: station.info.rssi,
103  ),
105  key="ssid",
106  translation_key="ssid",
107  entity_category=EntityCategory.DIAGNOSTIC,
108  entity_registry_enabled_default=False,
109  value_fn=lambda station: station.info.network_ssid,
110  ),
112  key="status",
113  translation_key="status",
114  device_class=SensorDeviceClass.ENUM,
115  options=STATUS_TYPE,
116  entity_category=EntityCategory.DIAGNOSTIC,
117  value_fn=lambda station: station.info.status.value,
118  ),
119 )
120 
121 
123  hass: HomeAssistant,
124  entry: TechnoVEConfigEntry,
125  async_add_entities: AddEntitiesCallback,
126 ) -> None:
127  """Set up the sensor platform."""
129  TechnoVESensorEntity(entry.runtime_data, description) for description in SENSORS
130  )
131 
132 
134  """Defines a TechnoVE sensor entity."""
135 
136  entity_description: TechnoVESensorEntityDescription
137 
138  def __init__(
139  self,
140  coordinator: TechnoVEDataUpdateCoordinator,
141  description: TechnoVESensorEntityDescription,
142  ) -> None:
143  """Initialize a TechnoVE sensor entity."""
144  super().__init__(coordinator, description.key)
145  self.entity_descriptionentity_description = description
146 
147  @property
148  def native_value(self) -> StateType:
149  """Return the state of the sensor."""
150  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
None __init__(self, TechnoVEDataUpdateCoordinator coordinator, TechnoVESensorEntityDescription description)
Definition: sensor.py:142
None async_setup_entry(HomeAssistant hass, TechnoVEConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:126