Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """IMGW-PIB sensor platform."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from imgw_pib.model import HydrologicalData
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.const import EntityCategory, UnitOfLength, UnitOfTemperature
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.typing import StateType
20 
21 from . import ImgwPibConfigEntry
22 from .coordinator import ImgwPibDataUpdateCoordinator
23 from .entity import ImgwPibEntity
24 
25 PARALLEL_UPDATES = 1
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """IMGW-PIB sensor entity description."""
31 
32  value: Callable[[HydrologicalData], StateType]
33 
34 
35 SENSOR_TYPES: tuple[ImgwPibSensorEntityDescription, ...] = (
37  key="flood_alarm_level",
38  translation_key="flood_alarm_level",
39  native_unit_of_measurement=UnitOfLength.CENTIMETERS,
40  device_class=SensorDeviceClass.DISTANCE,
41  entity_category=EntityCategory.DIAGNOSTIC,
42  suggested_display_precision=0,
43  entity_registry_enabled_default=False,
44  value=lambda data: data.flood_alarm_level.value,
45  ),
47  key="flood_warning_level",
48  translation_key="flood_warning_level",
49  native_unit_of_measurement=UnitOfLength.CENTIMETERS,
50  device_class=SensorDeviceClass.DISTANCE,
51  entity_category=EntityCategory.DIAGNOSTIC,
52  suggested_display_precision=0,
53  entity_registry_enabled_default=False,
54  value=lambda data: data.flood_warning_level.value,
55  ),
57  key="water_level",
58  translation_key="water_level",
59  native_unit_of_measurement=UnitOfLength.CENTIMETERS,
60  device_class=SensorDeviceClass.DISTANCE,
61  state_class=SensorStateClass.MEASUREMENT,
62  suggested_display_precision=0,
63  value=lambda data: data.water_level.value,
64  ),
66  key="water_temperature",
67  translation_key="water_temperature",
68  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
69  device_class=SensorDeviceClass.TEMPERATURE,
70  state_class=SensorStateClass.MEASUREMENT,
71  suggested_display_precision=1,
72  value=lambda data: data.water_temperature.value,
73  ),
74 )
75 
76 
78  hass: HomeAssistant,
79  entry: ImgwPibConfigEntry,
80  async_add_entities: AddEntitiesCallback,
81 ) -> None:
82  """Add a IMGW-PIB sensor entity from a config_entry."""
83  coordinator = entry.runtime_data.coordinator
84 
86  ImgwPibSensorEntity(coordinator, description)
87  for description in SENSOR_TYPES
88  if getattr(coordinator.data, description.key).value is not None
89  )
90 
91 
93  """Define IMGW-PIB sensor entity."""
94 
95  entity_description: ImgwPibSensorEntityDescription
96 
97  def __init__(
98  self,
99  coordinator: ImgwPibDataUpdateCoordinator,
100  description: ImgwPibSensorEntityDescription,
101  ) -> None:
102  """Initialize."""
103  super().__init__(coordinator)
104 
105  self._attr_unique_id_attr_unique_id = f"{coordinator.station_id}_{description.key}"
106  self.entity_descriptionentity_description = description
107 
108  @property
109  def native_value(self) -> StateType:
110  """Return the value reported by the sensor."""
111  return self.entity_descriptionentity_description.value(self.coordinator.data)
None __init__(self, ImgwPibDataUpdateCoordinator coordinator, ImgwPibSensorEntityDescription description)
Definition: sensor.py:101
None async_setup_entry(HomeAssistant hass, ImgwPibConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:81