Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for P1 Monitor sensors."""
2 
3 from __future__ import annotations
4 
5 from typing import Literal
6 
8  SensorDeviceClass,
9  SensorEntity,
10  SensorEntityDescription,
11  SensorStateClass,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import (
15  CONF_HOST,
16  CURRENCY_EURO,
17  UnitOfElectricCurrent,
18  UnitOfElectricPotential,
19  UnitOfEnergy,
20  UnitOfPower,
21  UnitOfVolume,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 from homeassistant.helpers.typing import StateType
27 from homeassistant.helpers.update_coordinator import CoordinatorEntity
28 
29 from .const import (
30  DOMAIN,
31  SERVICE_PHASES,
32  SERVICE_SETTINGS,
33  SERVICE_SMARTMETER,
34  SERVICE_WATERMETER,
35 )
36 from .coordinator import P1MonitorDataUpdateCoordinator
37 
38 SENSORS_SMARTMETER: tuple[SensorEntityDescription, ...] = (
40  key="gas_consumption",
41  translation_key="gas_consumption",
42  entity_registry_enabled_default=False,
43  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
44  device_class=SensorDeviceClass.GAS,
45  state_class=SensorStateClass.TOTAL_INCREASING,
46  ),
48  key="power_consumption",
49  translation_key="power_consumption",
50  native_unit_of_measurement=UnitOfPower.WATT,
51  device_class=SensorDeviceClass.POWER,
52  state_class=SensorStateClass.MEASUREMENT,
53  ),
55  key="energy_consumption_high",
56  translation_key="energy_consumption_high",
57  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
58  device_class=SensorDeviceClass.ENERGY,
59  state_class=SensorStateClass.TOTAL_INCREASING,
60  ),
62  key="energy_consumption_low",
63  translation_key="energy_consumption_low",
64  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
65  device_class=SensorDeviceClass.ENERGY,
66  state_class=SensorStateClass.TOTAL_INCREASING,
67  ),
69  key="power_production",
70  translation_key="power_production",
71  native_unit_of_measurement=UnitOfPower.WATT,
72  device_class=SensorDeviceClass.POWER,
73  state_class=SensorStateClass.MEASUREMENT,
74  ),
76  key="energy_production_high",
77  translation_key="energy_production_high",
78  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
79  device_class=SensorDeviceClass.ENERGY,
80  state_class=SensorStateClass.TOTAL_INCREASING,
81  ),
83  key="energy_production_low",
84  translation_key="energy_production_low",
85  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
86  device_class=SensorDeviceClass.ENERGY,
87  state_class=SensorStateClass.TOTAL_INCREASING,
88  ),
90  key="energy_tariff_period",
91  translation_key="energy_tariff_period",
92  ),
93 )
94 
95 SENSORS_PHASES: tuple[SensorEntityDescription, ...] = (
97  key="voltage_phase_l1",
98  translation_key="voltage_phase_l1",
99  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
100  device_class=SensorDeviceClass.VOLTAGE,
101  state_class=SensorStateClass.MEASUREMENT,
102  ),
104  key="voltage_phase_l2",
105  translation_key="voltage_phase_l2",
106  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
107  device_class=SensorDeviceClass.VOLTAGE,
108  state_class=SensorStateClass.MEASUREMENT,
109  ),
111  key="voltage_phase_l3",
112  translation_key="voltage_phase_l3",
113  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
114  device_class=SensorDeviceClass.VOLTAGE,
115  state_class=SensorStateClass.MEASUREMENT,
116  ),
118  key="current_phase_l1",
119  translation_key="current_phase_l1",
120  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
121  device_class=SensorDeviceClass.CURRENT,
122  state_class=SensorStateClass.MEASUREMENT,
123  ),
125  key="current_phase_l2",
126  translation_key="current_phase_l2",
127  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
128  device_class=SensorDeviceClass.CURRENT,
129  state_class=SensorStateClass.MEASUREMENT,
130  ),
132  key="current_phase_l3",
133  translation_key="current_phase_l3",
134  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
135  device_class=SensorDeviceClass.CURRENT,
136  state_class=SensorStateClass.MEASUREMENT,
137  ),
139  key="power_consumed_phase_l1",
140  translation_key="power_consumed_phase_l1",
141  native_unit_of_measurement=UnitOfPower.WATT,
142  device_class=SensorDeviceClass.POWER,
143  state_class=SensorStateClass.MEASUREMENT,
144  ),
146  key="power_consumed_phase_l2",
147  translation_key="power_consumed_phase_l2",
148  native_unit_of_measurement=UnitOfPower.WATT,
149  device_class=SensorDeviceClass.POWER,
150  state_class=SensorStateClass.MEASUREMENT,
151  ),
153  key="power_consumed_phase_l3",
154  translation_key="power_consumed_phase_l3",
155  native_unit_of_measurement=UnitOfPower.WATT,
156  device_class=SensorDeviceClass.POWER,
157  state_class=SensorStateClass.MEASUREMENT,
158  ),
160  key="power_produced_phase_l1",
161  translation_key="power_produced_phase_l1",
162  native_unit_of_measurement=UnitOfPower.WATT,
163  device_class=SensorDeviceClass.POWER,
164  state_class=SensorStateClass.MEASUREMENT,
165  ),
167  key="power_produced_phase_l2",
168  translation_key="power_produced_phase_l2",
169  native_unit_of_measurement=UnitOfPower.WATT,
170  device_class=SensorDeviceClass.POWER,
171  state_class=SensorStateClass.MEASUREMENT,
172  ),
174  key="power_produced_phase_l3",
175  translation_key="power_produced_phase_l3",
176  native_unit_of_measurement=UnitOfPower.WATT,
177  device_class=SensorDeviceClass.POWER,
178  state_class=SensorStateClass.MEASUREMENT,
179  ),
180 )
181 
182 SENSORS_SETTINGS: tuple[SensorEntityDescription, ...] = (
184  key="gas_consumption_price",
185  translation_key="gas_consumption_price",
186  entity_registry_enabled_default=False,
187  state_class=SensorStateClass.MEASUREMENT,
188  native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfVolume.CUBIC_METERS}",
189  ),
191  key="energy_consumption_price_low",
192  translation_key="energy_consumption_price_low",
193  state_class=SensorStateClass.MEASUREMENT,
194  native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
195  ),
197  key="energy_consumption_price_high",
198  translation_key="energy_consumption_price_high",
199  state_class=SensorStateClass.MEASUREMENT,
200  native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
201  ),
203  key="energy_production_price_low",
204  translation_key="energy_production_price_low",
205  state_class=SensorStateClass.MEASUREMENT,
206  native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
207  ),
209  key="energy_production_price_high",
210  translation_key="energy_production_price_high",
211  state_class=SensorStateClass.MEASUREMENT,
212  native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
213  ),
214 )
215 
216 SENSORS_WATERMETER: tuple[SensorEntityDescription, ...] = (
218  key="consumption_day",
219  translation_key="consumption_day",
220  state_class=SensorStateClass.TOTAL_INCREASING,
221  native_unit_of_measurement=UnitOfVolume.LITERS,
222  device_class=SensorDeviceClass.WATER,
223  ),
225  key="consumption_total",
226  translation_key="consumption_total",
227  state_class=SensorStateClass.TOTAL_INCREASING,
228  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
229  device_class=SensorDeviceClass.WATER,
230  ),
232  key="pulse_count",
233  translation_key="pulse_count",
234  ),
235 )
236 
237 
239  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
240 ) -> None:
241  """Set up P1 Monitor Sensors based on a config entry."""
242  entities: list[P1MonitorSensorEntity] = []
243  entities.extend(
245  entry=entry,
246  description=description,
247  name="SmartMeter",
248  service=SERVICE_SMARTMETER,
249  )
250  for description in SENSORS_SMARTMETER
251  )
252  entities.extend(
254  entry=entry,
255  description=description,
256  name="Phases",
257  service=SERVICE_PHASES,
258  )
259  for description in SENSORS_PHASES
260  )
261  entities.extend(
263  entry=entry,
264  description=description,
265  name="Settings",
266  service=SERVICE_SETTINGS,
267  )
268  for description in SENSORS_SETTINGS
269  )
270  if entry.runtime_data.has_water_meter:
271  entities.extend(
273  entry=entry,
274  description=description,
275  name="WaterMeter",
276  service=SERVICE_WATERMETER,
277  )
278  for description in SENSORS_WATERMETER
279  )
280  async_add_entities(entities)
281 
282 
284  CoordinatorEntity[P1MonitorDataUpdateCoordinator], SensorEntity
285 ):
286  """Defines an P1 Monitor sensor."""
287 
288  _attr_has_entity_name = True
289 
290  def __init__(
291  self,
292  *,
293  entry: ConfigEntry,
294  description: SensorEntityDescription,
295  name: str,
296  service: Literal["smartmeter", "watermeter", "phases", "settings"],
297  ) -> None:
298  """Initialize P1 Monitor sensor."""
299  super().__init__(coordinator=entry.runtime_data)
300  self._service_key_service_key = service
301 
302  self.entity_descriptionentity_description = description
303  self._attr_unique_id_attr_unique_id = (
304  f"{entry.runtime_data.config_entry.entry_id}_{service}_{description.key}"
305  )
306 
307  self._attr_device_info_attr_device_info = DeviceInfo(
308  entry_type=DeviceEntryType.SERVICE,
309  identifiers={
310  (DOMAIN, f"{entry.runtime_data.config_entry.entry_id}_{service}")
311  },
312  configuration_url=f"http://{entry.runtime_data.config_entry.data[CONF_HOST]}",
313  manufacturer="P1 Monitor",
314  name=name,
315  )
316 
317  @property
318  def native_value(self) -> StateType:
319  """Return the state of the sensor."""
320  value = getattr(
321  self.coordinator.data[self._service_key_service_key], self.entity_descriptionentity_description.key
322  )
323  if isinstance(value, str):
324  return value.lower()
325  return value # type: ignore[no-any-return]
None __init__(self, *ConfigEntry entry, SensorEntityDescription description, str name, Literal["smartmeter", "watermeter", "phases", "settings"] service)
Definition: sensor.py:297
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:240