Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for the Airzone Cloud binary sensors."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any, Final
7 
8 from aioairzone_cloud.const import (
9  AZD_ACTIVE,
10  AZD_AIDOOS,
11  AZD_AIR_DEMAND,
12  AZD_AQ_ACTIVE,
13  AZD_ERRORS,
14  AZD_FLOOR_DEMAND,
15  AZD_PROBLEMS,
16  AZD_SYSTEMS,
17  AZD_THERMOSTAT_BATTERY_LOW,
18  AZD_WARNINGS,
19  AZD_ZONES,
20 )
21 
23  BinarySensorDeviceClass,
24  BinarySensorEntity,
25  BinarySensorEntityDescription,
26 )
27 from homeassistant.const import EntityCategory
28 from homeassistant.core import HomeAssistant, callback
29 from homeassistant.helpers.entity_platform import AddEntitiesCallback
30 
31 from . import AirzoneCloudConfigEntry
32 from .coordinator import AirzoneUpdateCoordinator
33 from .entity import (
34  AirzoneAidooEntity,
35  AirzoneEntity,
36  AirzoneSystemEntity,
37  AirzoneZoneEntity,
38 )
39 
40 
41 @dataclass(frozen=True)
43  """A class that describes Airzone Cloud binary sensor entities."""
44 
45  attributes: dict[str, str] | None = None
46 
47 
48 AIDOO_BINARY_SENSOR_TYPES: Final[tuple[AirzoneBinarySensorEntityDescription, ...]] = (
50  device_class=BinarySensorDeviceClass.RUNNING,
51  key=AZD_ACTIVE,
52  ),
54  attributes={
55  "errors": AZD_ERRORS,
56  "warnings": AZD_WARNINGS,
57  },
58  device_class=BinarySensorDeviceClass.PROBLEM,
59  entity_category=EntityCategory.DIAGNOSTIC,
60  key=AZD_PROBLEMS,
61  ),
62 )
63 
64 
65 SYSTEM_BINARY_SENSOR_TYPES: Final[tuple[AirzoneBinarySensorEntityDescription, ...]] = (
67  attributes={
68  "errors": AZD_ERRORS,
69  "warnings": AZD_WARNINGS,
70  },
71  device_class=BinarySensorDeviceClass.PROBLEM,
72  entity_category=EntityCategory.DIAGNOSTIC,
73  key=AZD_PROBLEMS,
74  ),
75 )
76 
77 
78 ZONE_BINARY_SENSOR_TYPES: Final[tuple[AirzoneBinarySensorEntityDescription, ...]] = (
80  device_class=BinarySensorDeviceClass.RUNNING,
81  key=AZD_ACTIVE,
82  ),
84  device_class=BinarySensorDeviceClass.RUNNING,
85  key=AZD_AIR_DEMAND,
86  translation_key="air_demand",
87  ),
89  key=AZD_AQ_ACTIVE,
90  translation_key="air_quality_active",
91  ),
93  device_class=BinarySensorDeviceClass.BATTERY,
94  key=AZD_THERMOSTAT_BATTERY_LOW,
95  ),
97  device_class=BinarySensorDeviceClass.RUNNING,
98  key=AZD_FLOOR_DEMAND,
99  translation_key="floor_demand",
100  ),
102  attributes={
103  "warnings": AZD_WARNINGS,
104  },
105  device_class=BinarySensorDeviceClass.PROBLEM,
106  entity_category=EntityCategory.DIAGNOSTIC,
107  key=AZD_PROBLEMS,
108  ),
109 )
110 
111 
113  hass: HomeAssistant,
114  entry: AirzoneCloudConfigEntry,
115  async_add_entities: AddEntitiesCallback,
116 ) -> None:
117  """Add Airzone Cloud binary sensors from a config_entry."""
118  coordinator = entry.runtime_data
119 
120  binary_sensors: list[AirzoneBinarySensor] = [
122  coordinator,
123  description,
124  aidoo_id,
125  aidoo_data,
126  )
127  for aidoo_id, aidoo_data in coordinator.data.get(AZD_AIDOOS, {}).items()
128  for description in AIDOO_BINARY_SENSOR_TYPES
129  if description.key in aidoo_data
130  ]
131 
132  binary_sensors.extend(
134  coordinator,
135  description,
136  system_id,
137  system_data,
138  )
139  for system_id, system_data in coordinator.data.get(AZD_SYSTEMS, {}).items()
140  for description in SYSTEM_BINARY_SENSOR_TYPES
141  if description.key in system_data
142  )
143 
144  binary_sensors.extend(
146  coordinator,
147  description,
148  zone_id,
149  zone_data,
150  )
151  for zone_id, zone_data in coordinator.data.get(AZD_ZONES, {}).items()
152  for description in ZONE_BINARY_SENSOR_TYPES
153  if description.key in zone_data
154  )
155 
156  async_add_entities(binary_sensors)
157 
158 
160  """Define an Airzone Cloud binary sensor."""
161 
162  entity_description: AirzoneBinarySensorEntityDescription
163 
164  @property
165  def available(self) -> bool:
166  """Return Airzone Cloud binary sensor availability."""
167  return super().available and self.is_on is not None
168 
169  @callback
170  def _handle_coordinator_update(self) -> None:
171  """Update attributes when the coordinator updates."""
172  self._async_update_attrs_async_update_attrs()
174 
175  @callback
176  def _async_update_attrs(self) -> None:
177  """Update binary sensor attributes."""
178  self._attr_is_on_attr_is_on = self.get_airzone_valueget_airzone_value(self.entity_description.key)
179  if self.entity_description.attributes:
180  self._attr_extra_state_attributes_attr_extra_state_attributes = {
181  key: self.get_airzone_valueget_airzone_value(val)
182  for key, val in self.entity_description.attributes.items()
183  }
184 
185 
187  """Define an Airzone Cloud Aidoo binary sensor."""
188 
189  def __init__(
190  self,
191  coordinator: AirzoneUpdateCoordinator,
192  description: AirzoneBinarySensorEntityDescription,
193  aidoo_id: str,
194  aidoo_data: dict[str, Any],
195  ) -> None:
196  """Initialize."""
197  super().__init__(coordinator, aidoo_id, aidoo_data)
198 
199  self._attr_unique_id_attr_unique_id = f"{aidoo_id}_{description.key}"
200  self.entity_descriptionentity_description = description
201 
202  self._async_update_attrs_async_update_attrs()
203 
204 
206  """Define an Airzone Cloud System binary sensor."""
207 
208  def __init__(
209  self,
210  coordinator: AirzoneUpdateCoordinator,
211  description: AirzoneBinarySensorEntityDescription,
212  system_id: str,
213  system_data: dict[str, Any],
214  ) -> None:
215  """Initialize."""
216  super().__init__(coordinator, system_id, system_data)
217 
218  self._attr_unique_id_attr_unique_id = f"{system_id}_{description.key}"
219  self.entity_descriptionentity_description = description
220 
221  self._async_update_attrs_async_update_attrs()
222 
223 
225  """Define an Airzone Cloud Zone binary sensor."""
226 
227  def __init__(
228  self,
229  coordinator: AirzoneUpdateCoordinator,
230  description: AirzoneBinarySensorEntityDescription,
231  zone_id: str,
232  zone_data: dict[str, Any],
233  ) -> None:
234  """Initialize."""
235  super().__init__(coordinator, zone_id, zone_data)
236 
237  self._attr_unique_id_attr_unique_id = f"{zone_id}_{description.key}"
238  self.entity_descriptionentity_description = description
239 
240  self._async_update_attrs_async_update_attrs()
None __init__(self, AirzoneUpdateCoordinator coordinator, AirzoneBinarySensorEntityDescription description, str aidoo_id, dict[str, Any] aidoo_data)
None __init__(self, AirzoneUpdateCoordinator coordinator, AirzoneBinarySensorEntityDescription description, str system_id, dict[str, Any] system_data)
None __init__(self, AirzoneUpdateCoordinator coordinator, AirzoneBinarySensorEntityDescription description, str zone_id, dict[str, Any] zone_data)
None async_setup_entry(HomeAssistant hass, AirzoneCloudConfigEntry entry, AddEntitiesCallback async_add_entities)