Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for the Airzone switch."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any, Final
7 
8 from aioairzone.const import API_ON, AZD_ON, AZD_ZONES
9 
11  SwitchDeviceClass,
12  SwitchEntity,
13  SwitchEntityDescription,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import AirzoneConfigEntry
20 from .coordinator import AirzoneUpdateCoordinator
21 from .entity import AirzoneEntity, AirzoneZoneEntity
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """Class to describe an Airzone switch entity."""
27 
28  api_param: str
29 
30 
31 ZONE_SWITCH_TYPES: Final[tuple[AirzoneSwitchDescription, ...]] = (
33  api_param=API_ON,
34  device_class=SwitchDeviceClass.SWITCH,
35  key=AZD_ON,
36  ),
37 )
38 
39 
41  hass: HomeAssistant,
42  entry: AirzoneConfigEntry,
43  async_add_entities: AddEntitiesCallback,
44 ) -> None:
45  """Add Airzone switch from a config_entry."""
46  coordinator = entry.runtime_data
47 
48  added_zones: set[str] = set()
49 
50  def _async_entity_listener() -> None:
51  """Handle additions of switch."""
52 
53  zones_data = coordinator.data.get(AZD_ZONES, {})
54  received_zones = set(zones_data)
55  new_zones = received_zones - added_zones
56  if new_zones:
59  coordinator,
60  description,
61  entry,
62  system_zone_id,
63  zones_data.get(system_zone_id),
64  )
65  for system_zone_id in new_zones
66  for description in ZONE_SWITCH_TYPES
67  if description.key in zones_data.get(system_zone_id)
68  )
69  added_zones.update(new_zones)
70 
71  entry.async_on_unload(coordinator.async_add_listener(_async_entity_listener))
72  _async_entity_listener()
73 
74 
76  """Define an Airzone switch."""
77 
78  entity_description: AirzoneSwitchDescription
79 
80  @callback
81  def _handle_coordinator_update(self) -> None:
82  """Update attributes when the coordinator updates."""
83  self._async_update_attrs_async_update_attrs()
85 
86  @callback
87  def _async_update_attrs(self) -> None:
88  """Update switch attributes."""
89  self._attr_is_on_attr_is_on = self.get_airzone_valueget_airzone_value(self.entity_description.key)
90 
91 
93  """Define an Airzone Zone switch."""
94 
95  def __init__(
96  self,
97  coordinator: AirzoneUpdateCoordinator,
98  description: AirzoneSwitchDescription,
99  entry: ConfigEntry,
100  system_zone_id: str,
101  zone_data: dict[str, Any],
102  ) -> None:
103  """Initialize."""
104  super().__init__(coordinator, entry, system_zone_id, zone_data)
105 
106  self._attr_name_attr_name = None
107  self._attr_unique_id_attr_unique_id_attr_unique_id = (
108  f"{self._attr_unique_id}_{system_zone_id}_{description.key}"
109  )
110  self.entity_descriptionentity_description = description
111 
112  self._async_update_attrs_async_update_attrs()
113 
114  async def async_turn_on(self, **kwargs: Any) -> None:
115  """Turn the entity on."""
116  param = self.entity_descriptionentity_description.api_param
117  await self._async_update_hvac_params_async_update_hvac_params({param: True})
118 
119  async def async_turn_off(self, **kwargs: Any) -> None:
120  """Turn the entity off."""
121  param = self.entity_descriptionentity_description.api_param
122  await self._async_update_hvac_params_async_update_hvac_params({param: False})
None _async_update_hvac_params(self, dict[str, Any] params)
Definition: entity.py:196
None __init__(self, AirzoneUpdateCoordinator coordinator, AirzoneSwitchDescription description, ConfigEntry entry, str system_zone_id, dict[str, Any] zone_data)
Definition: switch.py:102
None async_setup_entry(HomeAssistant hass, AirzoneConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:44