Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """PEGELONLINE sensor entities."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from aiopegelonline.models import CurrentMeasurement
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import PegelOnlineConfigEntry
20 from .coordinator import PegelOnlineDataUpdateCoordinator
21 from .entity import PegelOnlineEntity
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """PEGELONLINE sensor entity description."""
27 
28  measurement_key: str
29 
30 
31 SENSORS: tuple[PegelOnlineSensorEntityDescription, ...] = (
33  key="air_temperature",
34  translation_key="air_temperature",
35  measurement_key="air_temperature",
36  state_class=SensorStateClass.MEASUREMENT,
37  device_class=SensorDeviceClass.TEMPERATURE,
38  entity_registry_enabled_default=False,
39  ),
41  key="clearance_height",
42  translation_key="clearance_height",
43  measurement_key="clearance_height",
44  state_class=SensorStateClass.MEASUREMENT,
45  device_class=SensorDeviceClass.DISTANCE,
46  ),
48  key="oxygen_level",
49  translation_key="oxygen_level",
50  measurement_key="oxygen_level",
51  state_class=SensorStateClass.MEASUREMENT,
52  entity_registry_enabled_default=False,
53  ),
55  key="ph_value",
56  measurement_key="ph_value",
57  state_class=SensorStateClass.MEASUREMENT,
58  device_class=SensorDeviceClass.PH,
59  entity_registry_enabled_default=False,
60  ),
62  key="water_speed",
63  translation_key="water_speed",
64  measurement_key="water_speed",
65  state_class=SensorStateClass.MEASUREMENT,
66  device_class=SensorDeviceClass.SPEED,
67  entity_registry_enabled_default=False,
68  ),
70  key="water_flow",
71  translation_key="water_flow",
72  measurement_key="water_flow",
73  state_class=SensorStateClass.MEASUREMENT,
74  entity_registry_enabled_default=False,
75  ),
77  key="water_level",
78  translation_key="water_level",
79  measurement_key="water_level",
80  state_class=SensorStateClass.MEASUREMENT,
81  ),
83  key="water_temperature",
84  translation_key="water_temperature",
85  measurement_key="water_temperature",
86  state_class=SensorStateClass.MEASUREMENT,
87  device_class=SensorDeviceClass.TEMPERATURE,
88  entity_registry_enabled_default=False,
89  ),
90 )
91 
92 
94  hass: HomeAssistant,
95  entry: PegelOnlineConfigEntry,
96  async_add_entities: AddEntitiesCallback,
97 ) -> None:
98  """Set up the PEGELONLINE sensor."""
99  coordinator = entry.runtime_data
100 
102  [
103  PegelOnlineSensor(coordinator, description)
104  for description in SENSORS
105  if getattr(coordinator.data, description.measurement_key) is not None
106  ]
107  )
108 
109 
111  """Representation of a PEGELONLINE sensor."""
112 
113  entity_description: PegelOnlineSensorEntityDescription
114 
115  def __init__(
116  self,
117  coordinator: PegelOnlineDataUpdateCoordinator,
118  description: PegelOnlineSensorEntityDescription,
119  ) -> None:
120  """Initialize a PEGELONLINE sensor."""
121  super().__init__(coordinator)
122  self.entity_descriptionentity_description = description
123  self._attr_unique_id_attr_unique_id = f"{self.station.uuid}_{description.key}"
124 
125  if description.device_class != SensorDeviceClass.PH:
126  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = self.measurementmeasurement.uom
127 
128  if self.stationstationstation.latitude and self.stationstationstation.longitude:
129  self._attr_extra_state_attributes_attr_extra_state_attributes.update(
130  {
131  ATTR_LATITUDE: self.stationstationstation.latitude,
132  ATTR_LONGITUDE: self.stationstationstation.longitude,
133  }
134  )
135 
136  @property
137  def measurement(self) -> CurrentMeasurement:
138  """Return the measurement data of the entity."""
139  return getattr(self.coordinator.data, self.entity_descriptionentity_description.measurement_key)
140 
141  @property
142  def native_value(self) -> float:
143  """Return the state of the device."""
144  return self.measurementmeasurement.value
None __init__(self, PegelOnlineDataUpdateCoordinator coordinator, PegelOnlineSensorEntityDescription description)
Definition: sensor.py:119
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
None async_setup_entry(HomeAssistant hass, PegelOnlineConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:97