Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor data of the Renson ventilation unit."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from renson_endura_delta.field_enum import (
8  AIR_QUALITY_FIELD,
9  BREEZE_LEVEL_FIELD,
10  BREEZE_TEMPERATURE_FIELD,
11  BYPASS_LEVEL_FIELD,
12  BYPASS_TEMPERATURE_FIELD,
13  CO2_FIELD,
14  CO2_HYSTERESIS_FIELD,
15  CO2_QUALITY_FIELD,
16  CO2_THRESHOLD_FIELD,
17  CURRENT_AIRFLOW_EXTRACT_FIELD,
18  CURRENT_AIRFLOW_INGOING_FIELD,
19  CURRENT_LEVEL_FIELD,
20  DAY_POLLUTION_FIELD,
21  FILTER_REMAIN_FIELD,
22  HUMIDITY_FIELD,
23  INDOOR_TEMP_FIELD,
24  MANUAL_LEVEL_FIELD,
25  NIGHT_POLLUTION_FIELD,
26  OUTDOOR_TEMP_FIELD,
27  FieldEnum,
28 )
29 from renson_endura_delta.renson import RensonVentilation
30 
32  SensorDeviceClass,
33  SensorEntity,
34  SensorEntityDescription,
35  SensorStateClass,
36 )
37 from homeassistant.config_entries import ConfigEntry
38 from homeassistant.const import (
39  CONCENTRATION_PARTS_PER_MILLION,
40  PERCENTAGE,
41  UnitOfTemperature,
42  UnitOfTime,
43  UnitOfVolumeFlowRate,
44 )
45 from homeassistant.core import HomeAssistant, callback
46 from homeassistant.helpers.entity_platform import AddEntitiesCallback
47 
48 from . import RensonData
49 from .const import DOMAIN
50 from .coordinator import RensonCoordinator
51 from .entity import RensonEntity
52 
53 
54 @dataclass(frozen=True, kw_only=True)
56  """Description of a Renson sensor."""
57 
58  field: FieldEnum
59  raw_format: bool
60 
61 
62 SENSORS: tuple[RensonSensorEntityDescription, ...] = (
64  key="CO2_QUALITY_FIELD",
65  translation_key="co2_quality_category",
66  field=CO2_QUALITY_FIELD,
67  raw_format=False,
68  device_class=SensorDeviceClass.ENUM,
69  options=["good", "bad", "poor"],
70  ),
72  key="AIR_QUALITY_FIELD",
73  translation_key="air_quality_category",
74  field=AIR_QUALITY_FIELD,
75  raw_format=False,
76  device_class=SensorDeviceClass.ENUM,
77  options=["good", "bad", "poor"],
78  ),
80  key="CO2_FIELD",
81  field=CO2_FIELD,
82  raw_format=True,
83  state_class=SensorStateClass.MEASUREMENT,
84  device_class=SensorDeviceClass.CO2,
85  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
86  ),
88  key="AIR_FIELD",
89  translation_key="air_quality",
90  field=AIR_QUALITY_FIELD,
91  state_class=SensorStateClass.MEASUREMENT,
92  raw_format=True,
93  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
94  ),
96  key="CURRENT_LEVEL_FIELD",
97  translation_key="ventilation_level",
98  field=CURRENT_LEVEL_FIELD,
99  raw_format=False,
100  device_class=SensorDeviceClass.ENUM,
101  options=["off", "level1", "level2", "level3", "level4", "breeze", "holiday"],
102  ),
104  key="CURRENT_AIRFLOW_EXTRACT_FIELD",
105  translation_key="total_airflow_out",
106  field=CURRENT_AIRFLOW_EXTRACT_FIELD,
107  raw_format=False,
108  state_class=SensorStateClass.MEASUREMENT,
109  native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
110  ),
112  key="CURRENT_AIRFLOW_INGOING_FIELD",
113  translation_key="total_airflow_in",
114  field=CURRENT_AIRFLOW_INGOING_FIELD,
115  raw_format=False,
116  state_class=SensorStateClass.MEASUREMENT,
117  native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
118  ),
120  key="OUTDOOR_TEMP_FIELD",
121  translation_key="outdoor_air_temperature",
122  field=OUTDOOR_TEMP_FIELD,
123  raw_format=False,
124  device_class=SensorDeviceClass.TEMPERATURE,
125  state_class=SensorStateClass.MEASUREMENT,
126  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
127  ),
129  key="INDOOR_TEMP_FIELD",
130  translation_key="extract_air_temperature",
131  field=INDOOR_TEMP_FIELD,
132  raw_format=False,
133  device_class=SensorDeviceClass.TEMPERATURE,
134  state_class=SensorStateClass.MEASUREMENT,
135  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
136  ),
138  key="FILTER_REMAIN_FIELD",
139  translation_key="filter_change",
140  field=FILTER_REMAIN_FIELD,
141  raw_format=False,
142  device_class=SensorDeviceClass.DURATION,
143  state_class=SensorStateClass.MEASUREMENT,
144  native_unit_of_measurement=UnitOfTime.DAYS,
145  ),
147  key="HUMIDITY_FIELD",
148  field=HUMIDITY_FIELD,
149  raw_format=False,
150  device_class=SensorDeviceClass.HUMIDITY,
151  state_class=SensorStateClass.MEASUREMENT,
152  native_unit_of_measurement=PERCENTAGE,
153  ),
155  key="MANUAL_LEVEL_FIELD",
156  translation_key="manual_level",
157  field=MANUAL_LEVEL_FIELD,
158  raw_format=False,
159  device_class=SensorDeviceClass.ENUM,
160  options=["off", "level1", "level2", "level3", "level4", "breeze", "holiday"],
161  ),
163  key="BREEZE_TEMPERATURE_FIELD",
164  translation_key="breeze_temperature",
165  field=BREEZE_TEMPERATURE_FIELD,
166  raw_format=False,
167  device_class=SensorDeviceClass.TEMPERATURE,
168  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
169  ),
171  key="BREEZE_LEVEL_FIELD",
172  translation_key="breeze_level",
173  field=BREEZE_LEVEL_FIELD,
174  raw_format=False,
175  device_class=SensorDeviceClass.ENUM,
176  options=["off", "level1", "level2", "level3", "level4", "breeze"],
177  ),
179  key="DAY_POLLUTION_FIELD",
180  translation_key="day_pollution_level",
181  field=DAY_POLLUTION_FIELD,
182  raw_format=False,
183  entity_registry_enabled_default=False,
184  device_class=SensorDeviceClass.ENUM,
185  options=["level1", "level2", "level3", "level4"],
186  ),
188  key="NIGHT_POLLUTION_FIELD",
189  translation_key="co2_quality_category",
190  field=NIGHT_POLLUTION_FIELD,
191  raw_format=False,
192  entity_registry_enabled_default=False,
193  device_class=SensorDeviceClass.ENUM,
194  options=["level1", "level2", "level3", "level4"],
195  ),
197  key="CO2_THRESHOLD_FIELD",
198  translation_key="co2_threshold",
199  field=CO2_THRESHOLD_FIELD,
200  raw_format=False,
201  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
202  entity_registry_enabled_default=False,
203  ),
205  key="CO2_HYSTERESIS_FIELD",
206  translation_key="co2_hysteresis",
207  field=CO2_HYSTERESIS_FIELD,
208  raw_format=False,
209  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
210  entity_registry_enabled_default=False,
211  ),
213  key="BYPASS_TEMPERATURE_FIELD",
214  translation_key="bypass_activation_temperature",
215  field=BYPASS_TEMPERATURE_FIELD,
216  raw_format=False,
217  device_class=SensorDeviceClass.TEMPERATURE,
218  state_class=SensorStateClass.MEASUREMENT,
219  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
220  ),
222  key="BYPASS_LEVEL_FIELD",
223  translation_key="bypass_level",
224  field=BYPASS_LEVEL_FIELD,
225  raw_format=False,
226  device_class=SensorDeviceClass.POWER_FACTOR,
227  state_class=SensorStateClass.MEASUREMENT,
228  native_unit_of_measurement=PERCENTAGE,
229  ),
230 )
231 
232 
234  """Get a sensor data from the Renson API and store it in the state of the class."""
235 
236  _attr_has_entity_name = True
237 
238  def __init__(
239  self,
240  description: RensonSensorEntityDescription,
241  api: RensonVentilation,
242  coordinator: RensonCoordinator,
243  ) -> None:
244  """Initialize class."""
245  super().__init__(description.key, api, coordinator)
246 
247  self.fieldfield = description.field
248  self.entity_descriptionentity_description = description
249 
250  self.data_typedata_type = description.field.field_type
251  self.raw_formatraw_format = description.raw_format
252 
253  @callback
254  def _handle_coordinator_update(self) -> None:
255  """Handle updated data from the coordinator."""
256  all_data = self.coordinator.data
257 
258  value = self.apiapiapi.get_field_value(all_data, self.fieldfield.name)
259 
260  if self.raw_formatraw_format:
261  self._attr_native_value_attr_native_value = value
262  elif self.entity_descriptionentity_description.device_class == SensorDeviceClass.ENUM:
263  self._attr_native_value_attr_native_value = self.apiapiapi.parse_value(
264  value, self.data_typedata_type
265  ).lower()
266  else:
267  self._attr_native_value_attr_native_value = self.apiapiapi.parse_value(value, self.data_typedata_type)
268 
269  self.async_write_ha_stateasync_write_ha_state()
270 
271 
273  hass: HomeAssistant,
274  config_entry: ConfigEntry,
275  async_add_entities: AddEntitiesCallback,
276 ) -> None:
277  """Set up the Renson sensor platform."""
278 
279  data: RensonData = hass.data[DOMAIN][config_entry.entry_id]
280 
281  entities = [
282  RensonSensor(description, data.api, data.coordinator) for description in SENSORS
283  ]
284 
285  async_add_entities(entities)
None __init__(self, RensonSensorEntityDescription description, RensonVentilation api, RensonCoordinator coordinator)
Definition: sensor.py:243
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:276