Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for the Airzone Cloud sensors."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, Final
6 
7 from aioairzone_cloud.const import (
8  AZD_AIDOOS,
9  AZD_AQ_INDEX,
10  AZD_AQ_PM_1,
11  AZD_AQ_PM_2P5,
12  AZD_AQ_PM_10,
13  AZD_CPU_USAGE,
14  AZD_HUMIDITY,
15  AZD_INDOOR_EXCHANGER_TEMP,
16  AZD_INDOOR_RETURN_TEMP,
17  AZD_INDOOR_WORK_TEMP,
18  AZD_MEMORY_FREE,
19  AZD_OUTDOOR_CONDENSER_PRESS,
20  AZD_OUTDOOR_DISCHARGE_TEMP,
21  AZD_OUTDOOR_ELECTRIC_CURRENT,
22  AZD_OUTDOOR_EVAPORATOR_PRESS,
23  AZD_OUTDOOR_EXCHANGER_TEMP,
24  AZD_OUTDOOR_TEMP,
25  AZD_TEMP,
26  AZD_THERMOSTAT_BATTERY,
27  AZD_THERMOSTAT_COVERAGE,
28  AZD_WEBSERVERS,
29  AZD_WIFI_RSSI,
30  AZD_ZONES,
31 )
32 
34  SensorDeviceClass,
35  SensorEntity,
36  SensorEntityDescription,
37  SensorStateClass,
38 )
39 from homeassistant.const import (
40  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
41  PERCENTAGE,
42  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
43  EntityCategory,
44  UnitOfElectricCurrent,
45  UnitOfInformation,
46  UnitOfPressure,
47  UnitOfTemperature,
48 )
49 from homeassistant.core import HomeAssistant, callback
50 from homeassistant.helpers.entity_platform import AddEntitiesCallback
51 
52 from . import AirzoneCloudConfigEntry
53 from .coordinator import AirzoneUpdateCoordinator
54 from .entity import (
55  AirzoneAidooEntity,
56  AirzoneEntity,
57  AirzoneWebServerEntity,
58  AirzoneZoneEntity,
59 )
60 
61 AIDOO_SENSOR_TYPES: Final[tuple[SensorEntityDescription, ...]] = (
63  entity_category=EntityCategory.DIAGNOSTIC,
64  entity_registry_enabled_default=False,
65  key=AZD_INDOOR_EXCHANGER_TEMP,
66  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
67  state_class=SensorStateClass.MEASUREMENT,
68  translation_key="indoor_exchanger_temp",
69  ),
71  entity_category=EntityCategory.DIAGNOSTIC,
72  entity_registry_enabled_default=False,
73  key=AZD_INDOOR_RETURN_TEMP,
74  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
75  state_class=SensorStateClass.MEASUREMENT,
76  translation_key="indoor_return_temp",
77  ),
79  entity_category=EntityCategory.DIAGNOSTIC,
80  entity_registry_enabled_default=False,
81  key=AZD_INDOOR_WORK_TEMP,
82  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
83  state_class=SensorStateClass.MEASUREMENT,
84  translation_key="indoor_work_temp",
85  ),
87  entity_category=EntityCategory.DIAGNOSTIC,
88  entity_registry_enabled_default=False,
89  key=AZD_OUTDOOR_CONDENSER_PRESS,
90  native_unit_of_measurement=UnitOfPressure.KPA,
91  state_class=SensorStateClass.MEASUREMENT,
92  translation_key="outdoor_condenser_press",
93  ),
95  entity_category=EntityCategory.DIAGNOSTIC,
96  entity_registry_enabled_default=False,
97  key=AZD_OUTDOOR_DISCHARGE_TEMP,
98  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
99  state_class=SensorStateClass.MEASUREMENT,
100  translation_key="outdoor_discharge_temp",
101  ),
103  entity_category=EntityCategory.DIAGNOSTIC,
104  entity_registry_enabled_default=False,
105  key=AZD_OUTDOOR_ELECTRIC_CURRENT,
106  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
107  state_class=SensorStateClass.MEASUREMENT,
108  translation_key="outdoor_electric_current",
109  ),
111  entity_category=EntityCategory.DIAGNOSTIC,
112  entity_registry_enabled_default=False,
113  key=AZD_OUTDOOR_EVAPORATOR_PRESS,
114  native_unit_of_measurement=UnitOfPressure.KPA,
115  state_class=SensorStateClass.MEASUREMENT,
116  translation_key="outdoor_evaporator_press",
117  ),
119  entity_category=EntityCategory.DIAGNOSTIC,
120  entity_registry_enabled_default=False,
121  key=AZD_OUTDOOR_EXCHANGER_TEMP,
122  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
123  state_class=SensorStateClass.MEASUREMENT,
124  translation_key="outdoor_exchanger_temp",
125  ),
127  entity_category=EntityCategory.DIAGNOSTIC,
128  entity_registry_enabled_default=False,
129  key=AZD_OUTDOOR_TEMP,
130  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
131  state_class=SensorStateClass.MEASUREMENT,
132  translation_key="outdoor_temp",
133  ),
135  device_class=SensorDeviceClass.TEMPERATURE,
136  key=AZD_TEMP,
137  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
138  state_class=SensorStateClass.MEASUREMENT,
139  ),
140 )
141 
142 WEBSERVER_SENSOR_TYPES: Final[tuple[SensorEntityDescription, ...]] = (
144  entity_category=EntityCategory.DIAGNOSTIC,
145  entity_registry_enabled_default=False,
146  key=AZD_CPU_USAGE,
147  native_unit_of_measurement=PERCENTAGE,
148  state_class=SensorStateClass.MEASUREMENT,
149  translation_key="cpu_usage",
150  ),
152  entity_category=EntityCategory.DIAGNOSTIC,
153  entity_registry_enabled_default=False,
154  key=AZD_MEMORY_FREE,
155  native_unit_of_measurement=UnitOfInformation.BYTES,
156  state_class=SensorStateClass.MEASUREMENT,
157  translation_key="free_memory",
158  ),
160  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
161  entity_category=EntityCategory.DIAGNOSTIC,
162  entity_registry_enabled_default=False,
163  key=AZD_WIFI_RSSI,
164  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
165  state_class=SensorStateClass.MEASUREMENT,
166  ),
167 )
168 
169 ZONE_SENSOR_TYPES: Final[tuple[SensorEntityDescription, ...]] = (
171  device_class=SensorDeviceClass.AQI,
172  key=AZD_AQ_INDEX,
173  state_class=SensorStateClass.MEASUREMENT,
174  ),
176  device_class=SensorDeviceClass.PM1,
177  key=AZD_AQ_PM_1,
178  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
179  state_class=SensorStateClass.MEASUREMENT,
180  ),
182  device_class=SensorDeviceClass.PM25,
183  key=AZD_AQ_PM_2P5,
184  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
185  state_class=SensorStateClass.MEASUREMENT,
186  ),
188  device_class=SensorDeviceClass.PM10,
189  key=AZD_AQ_PM_10,
190  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
191  state_class=SensorStateClass.MEASUREMENT,
192  ),
194  device_class=SensorDeviceClass.TEMPERATURE,
195  key=AZD_TEMP,
196  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
197  state_class=SensorStateClass.MEASUREMENT,
198  ),
200  device_class=SensorDeviceClass.HUMIDITY,
201  key=AZD_HUMIDITY,
202  native_unit_of_measurement=PERCENTAGE,
203  state_class=SensorStateClass.MEASUREMENT,
204  ),
206  device_class=SensorDeviceClass.BATTERY,
207  key=AZD_THERMOSTAT_BATTERY,
208  native_unit_of_measurement=PERCENTAGE,
209  state_class=SensorStateClass.MEASUREMENT,
210  ),
212  entity_category=EntityCategory.DIAGNOSTIC,
213  entity_registry_enabled_default=False,
214  key=AZD_THERMOSTAT_COVERAGE,
215  native_unit_of_measurement=PERCENTAGE,
216  state_class=SensorStateClass.MEASUREMENT,
217  translation_key="thermostat_coverage",
218  ),
219 )
220 
221 
223  hass: HomeAssistant,
224  entry: AirzoneCloudConfigEntry,
225  async_add_entities: AddEntitiesCallback,
226 ) -> None:
227  """Add Airzone Cloud sensors from a config_entry."""
228  coordinator = entry.runtime_data
229 
230  # Aidoos
231  sensors: list[AirzoneSensor] = [
233  coordinator,
234  description,
235  aidoo_id,
236  aidoo_data,
237  )
238  for aidoo_id, aidoo_data in coordinator.data.get(AZD_AIDOOS, {}).items()
239  for description in AIDOO_SENSOR_TYPES
240  if description.key in aidoo_data
241  ]
242 
243  # WebServers
244  sensors.extend(
246  coordinator,
247  description,
248  ws_id,
249  ws_data,
250  )
251  for ws_id, ws_data in coordinator.data.get(AZD_WEBSERVERS, {}).items()
252  for description in WEBSERVER_SENSOR_TYPES
253  if description.key in ws_data
254  )
255 
256  # Zones
257  sensors.extend(
259  coordinator,
260  description,
261  zone_id,
262  zone_data,
263  )
264  for zone_id, zone_data in coordinator.data.get(AZD_ZONES, {}).items()
265  for description in ZONE_SENSOR_TYPES
266  if description.key in zone_data
267  )
268 
269  async_add_entities(sensors)
270 
271 
273  """Define an Airzone Cloud sensor."""
274 
275  @property
276  def available(self) -> bool:
277  """Return Airzone Cloud sensor availability."""
278  return super().available and self.native_valuenative_value is not None
279 
280  @callback
281  def _handle_coordinator_update(self) -> None:
282  """Update attributes when the coordinator updates."""
283  self._async_update_attrs_async_update_attrs()
285 
286  @callback
287  def _async_update_attrs(self) -> None:
288  """Update sensor attributes."""
289  self._attr_native_value_attr_native_value = self.get_airzone_valueget_airzone_value(self.entity_description.key)
290 
291 
293  """Define an Airzone Cloud Aidoo sensor."""
294 
295  def __init__(
296  self,
297  coordinator: AirzoneUpdateCoordinator,
298  description: SensorEntityDescription,
299  aidoo_id: str,
300  aidoo_data: dict[str, Any],
301  ) -> None:
302  """Initialize."""
303  super().__init__(coordinator, aidoo_id, aidoo_data)
304 
305  self._attr_unique_id_attr_unique_id = f"{aidoo_id}_{description.key}"
306  self.entity_descriptionentity_description = description
307 
308  self._async_update_attrs_async_update_attrs()
309 
310 
312  """Define an Airzone Cloud WebServer sensor."""
313 
314  def __init__(
315  self,
316  coordinator: AirzoneUpdateCoordinator,
317  description: SensorEntityDescription,
318  ws_id: str,
319  ws_data: dict[str, Any],
320  ) -> None:
321  """Initialize."""
322  super().__init__(coordinator, ws_id, ws_data)
323 
324  self._attr_unique_id_attr_unique_id = f"{ws_id}_{description.key}"
325  self.entity_descriptionentity_description = description
326 
327  self._async_update_attrs_async_update_attrs()
328 
329 
331  """Define an Airzone Cloud Zone sensor."""
332 
333  def __init__(
334  self,
335  coordinator: AirzoneUpdateCoordinator,
336  description: SensorEntityDescription,
337  zone_id: str,
338  zone_data: dict[str, Any],
339  ) -> None:
340  """Initialize."""
341  super().__init__(coordinator, zone_id, zone_data)
342 
343  self._attr_unique_id_attr_unique_id = f"{zone_id}_{description.key}"
344  self.entity_descriptionentity_description = description
345 
346  self._async_update_attrs_async_update_attrs()
None __init__(self, AirzoneUpdateCoordinator coordinator, SensorEntityDescription description, str aidoo_id, dict[str, Any] aidoo_data)
Definition: sensor.py:301
None __init__(self, AirzoneUpdateCoordinator coordinator, SensorEntityDescription description, str ws_id, dict[str, Any] ws_data)
Definition: sensor.py:320
None __init__(self, AirzoneUpdateCoordinator coordinator, SensorEntityDescription description, str zone_id, dict[str, Any] zone_data)
Definition: sensor.py:339
StateType|date|datetime|Decimal native_value(self)
Definition: __init__.py:460
None async_setup_entry(HomeAssistant hass, AirzoneCloudConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:226