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 dataclasses import dataclass
6 import logging
7 from typing import Any
8 
9 from energyflip.const import (
10  SOURCE_TYPE_ELECTRICITY,
11  SOURCE_TYPE_ELECTRICITY_IN,
12  SOURCE_TYPE_ELECTRICITY_IN_LOW,
13  SOURCE_TYPE_ELECTRICITY_OUT,
14  SOURCE_TYPE_ELECTRICITY_OUT_LOW,
15  SOURCE_TYPE_GAS,
16 )
17 
19  SensorDeviceClass,
20  SensorEntity,
21  SensorEntityDescription,
22  SensorStateClass,
23 )
24 from homeassistant.config_entries import ConfigEntry
25 from homeassistant.const import (
26  CONF_ID,
27  UnitOfEnergy,
28  UnitOfPower,
29  UnitOfVolume,
30  UnitOfVolumeFlowRate,
31 )
32 from homeassistant.core import HomeAssistant
33 from homeassistant.helpers.entity_platform import AddEntitiesCallback
35  CoordinatorEntity,
36  DataUpdateCoordinator,
37 )
38 
39 from .const import (
40  DATA_COORDINATOR,
41  DOMAIN,
42  SENSOR_TYPE_RATE,
43  SENSOR_TYPE_THIS_DAY,
44  SENSOR_TYPE_THIS_MONTH,
45  SENSOR_TYPE_THIS_WEEK,
46  SENSOR_TYPE_THIS_YEAR,
47 )
48 
49 _LOGGER = logging.getLogger(__name__)
50 
51 
52 @dataclass(frozen=True)
54  """Class describing Airly sensor entities."""
55 
56  sensor_type: str = SENSOR_TYPE_RATE
57 
58 
59 SENSORS_INFO = [
61  translation_key="current_power",
62  sensor_type=SENSOR_TYPE_RATE,
63  device_class=SensorDeviceClass.POWER,
64  native_unit_of_measurement=UnitOfPower.WATT,
65  key=SOURCE_TYPE_ELECTRICITY,
66  state_class=SensorStateClass.MEASUREMENT,
67  ),
69  translation_key="current_power_peak",
70  sensor_type=SENSOR_TYPE_RATE,
71  device_class=SensorDeviceClass.POWER,
72  native_unit_of_measurement=UnitOfPower.WATT,
73  key=SOURCE_TYPE_ELECTRICITY_IN,
74  state_class=SensorStateClass.MEASUREMENT,
75  ),
77  translation_key="current_power_off_peak",
78  sensor_type=SENSOR_TYPE_RATE,
79  device_class=SensorDeviceClass.POWER,
80  native_unit_of_measurement=UnitOfPower.WATT,
81  key=SOURCE_TYPE_ELECTRICITY_IN_LOW,
82  state_class=SensorStateClass.MEASUREMENT,
83  ),
85  translation_key="current_power_out_peak",
86  sensor_type=SENSOR_TYPE_RATE,
87  device_class=SensorDeviceClass.POWER,
88  native_unit_of_measurement=UnitOfPower.WATT,
89  key=SOURCE_TYPE_ELECTRICITY_OUT,
90  state_class=SensorStateClass.MEASUREMENT,
91  ),
93  translation_key="current_power_out_off_peak",
94  sensor_type=SENSOR_TYPE_RATE,
95  device_class=SensorDeviceClass.POWER,
96  native_unit_of_measurement=UnitOfPower.WATT,
97  key=SOURCE_TYPE_ELECTRICITY_OUT_LOW,
98  state_class=SensorStateClass.MEASUREMENT,
99  ),
101  translation_key="energy_consumption_peak_today",
102  device_class=SensorDeviceClass.ENERGY,
103  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
104  key=SOURCE_TYPE_ELECTRICITY_IN,
105  sensor_type=SENSOR_TYPE_THIS_DAY,
106  state_class=SensorStateClass.TOTAL_INCREASING,
107  suggested_display_precision=3,
108  ),
110  translation_key="energy_consumption_off_peak_today",
111  device_class=SensorDeviceClass.ENERGY,
112  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
113  key=SOURCE_TYPE_ELECTRICITY_IN_LOW,
114  sensor_type=SENSOR_TYPE_THIS_DAY,
115  state_class=SensorStateClass.TOTAL_INCREASING,
116  suggested_display_precision=3,
117  ),
119  translation_key="energy_production_peak_today",
120  device_class=SensorDeviceClass.ENERGY,
121  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
122  key=SOURCE_TYPE_ELECTRICITY_OUT,
123  sensor_type=SENSOR_TYPE_THIS_DAY,
124  state_class=SensorStateClass.TOTAL_INCREASING,
125  suggested_display_precision=3,
126  ),
128  translation_key="energy_production_off_peak_today",
129  device_class=SensorDeviceClass.ENERGY,
130  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
131  key=SOURCE_TYPE_ELECTRICITY_OUT_LOW,
132  sensor_type=SENSOR_TYPE_THIS_DAY,
133  state_class=SensorStateClass.TOTAL_INCREASING,
134  suggested_display_precision=3,
135  ),
137  translation_key="energy_today",
138  device_class=SensorDeviceClass.ENERGY,
139  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
140  state_class=SensorStateClass.TOTAL,
141  key=SOURCE_TYPE_ELECTRICITY,
142  sensor_type=SENSOR_TYPE_THIS_DAY,
143  suggested_display_precision=1,
144  ),
146  translation_key="energy_week",
147  device_class=SensorDeviceClass.ENERGY,
148  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
149  state_class=SensorStateClass.TOTAL,
150  key=SOURCE_TYPE_ELECTRICITY,
151  sensor_type=SENSOR_TYPE_THIS_WEEK,
152  suggested_display_precision=1,
153  ),
155  translation_key="energy_month",
156  device_class=SensorDeviceClass.ENERGY,
157  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
158  state_class=SensorStateClass.TOTAL,
159  key=SOURCE_TYPE_ELECTRICITY,
160  sensor_type=SENSOR_TYPE_THIS_MONTH,
161  suggested_display_precision=1,
162  ),
164  translation_key="energy_year",
165  device_class=SensorDeviceClass.ENERGY,
166  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
167  state_class=SensorStateClass.TOTAL,
168  key=SOURCE_TYPE_ELECTRICITY,
169  sensor_type=SENSOR_TYPE_THIS_YEAR,
170  suggested_display_precision=1,
171  ),
173  translation_key="current_gas",
174  native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
175  sensor_type=SENSOR_TYPE_RATE,
176  state_class=SensorStateClass.MEASUREMENT,
177  key=SOURCE_TYPE_GAS,
178  suggested_display_precision=2,
179  ),
181  translation_key="gas_today",
182  device_class=SensorDeviceClass.GAS,
183  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
184  key=SOURCE_TYPE_GAS,
185  sensor_type=SENSOR_TYPE_THIS_DAY,
186  state_class=SensorStateClass.TOTAL_INCREASING,
187  suggested_display_precision=2,
188  ),
190  translation_key="gas_week",
191  device_class=SensorDeviceClass.GAS,
192  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
193  key=SOURCE_TYPE_GAS,
194  sensor_type=SENSOR_TYPE_THIS_WEEK,
195  state_class=SensorStateClass.TOTAL_INCREASING,
196  suggested_display_precision=2,
197  ),
199  translation_key="gas_month",
200  device_class=SensorDeviceClass.GAS,
201  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
202  key=SOURCE_TYPE_GAS,
203  sensor_type=SENSOR_TYPE_THIS_MONTH,
204  state_class=SensorStateClass.TOTAL_INCREASING,
205  suggested_display_precision=2,
206  ),
208  translation_key="gas_year",
209  device_class=SensorDeviceClass.GAS,
210  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
211  key=SOURCE_TYPE_GAS,
212  sensor_type=SENSOR_TYPE_THIS_YEAR,
213  state_class=SensorStateClass.TOTAL_INCREASING,
214  suggested_display_precision=2,
215  ),
216 ]
217 
218 
220  hass: HomeAssistant,
221  config_entry: ConfigEntry,
222  async_add_entities: AddEntitiesCallback,
223 ) -> None:
224  """Set up the sensor platform."""
225  coordinator: DataUpdateCoordinator[dict[str, dict[str, Any]]] = hass.data[DOMAIN][
226  config_entry.entry_id
227  ][DATA_COORDINATOR]
228  user_id = config_entry.data[CONF_ID]
229 
231  EnergyFlipSensor(coordinator, user_id, description)
232  for description in SENSORS_INFO
233  )
234 
235 
237  CoordinatorEntity[DataUpdateCoordinator[dict[str, dict[str, Any]]]], SensorEntity
238 ):
239  """Defines a EnergyFlip sensor."""
240 
241  entity_description: EnergyFlipSensorEntityDescription
242  _attr_has_entity_name = True
243 
244  def __init__(
245  self,
246  coordinator: DataUpdateCoordinator[dict[str, dict[str, Any]]],
247  user_id: str,
248  description: EnergyFlipSensorEntityDescription,
249  ) -> None:
250  """Initialize the sensor."""
251  super().__init__(coordinator)
252  self.entity_descriptionentity_description = description
253  self._source_type_source_type = description.key
254  self._sensor_type_sensor_type = description.sensor_type
255  self._attr_unique_id_attr_unique_id = (
256  f"{DOMAIN}_{user_id}_{description.key}_{description.sensor_type}"
257  )
258 
259  @property
260  def native_value(self) -> int | float | None:
261  """Return the state of the sensor."""
262  if (
263  data := self.coordinator.data[self.entity_descriptionentity_description.key][
264  self.entity_descriptionentity_description.sensor_type
265  ]
266  ) is not None:
267  return data
268  return None
269 
270  @property
271  def available(self) -> bool:
272  """Return if entity is available."""
273  return bool(
274  super().available
275  and self.coordinator.data
276  and self._source_type_source_type in self.coordinator.data
277  and self.coordinator.data[self._source_type_source_type]
278  )
None __init__(self, DataUpdateCoordinator[dict[str, dict[str, Any]]] coordinator, str user_id, EnergyFlipSensorEntityDescription description)
Definition: sensor.py:249
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:223