Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for the Airzone Cloud switch."""
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 API_POWER, API_VALUE, AZD_POWER, AZD_ZONES
9 
11  SwitchDeviceClass,
12  SwitchEntity,
13  SwitchEntityDescription,
14 )
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import AirzoneCloudConfigEntry
19 from .coordinator import AirzoneUpdateCoordinator
20 from .entity import AirzoneEntity, AirzoneZoneEntity
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  """Class to describe an Airzone switch entity."""
26 
27  api_param: str
28 
29 
30 ZONE_SWITCH_TYPES: Final[tuple[AirzoneSwitchDescription, ...]] = (
32  api_param=API_POWER,
33  device_class=SwitchDeviceClass.SWITCH,
34  key=AZD_POWER,
35  ),
36 )
37 
38 
40  hass: HomeAssistant,
41  entry: AirzoneCloudConfigEntry,
42  async_add_entities: AddEntitiesCallback,
43 ) -> None:
44  """Add Airzone Cloud switch from a config_entry."""
45  coordinator = entry.runtime_data
46 
47  # Zones
50  coordinator,
51  description,
52  zone_id,
53  zone_data,
54  )
55  for description in ZONE_SWITCH_TYPES
56  for zone_id, zone_data in coordinator.data.get(AZD_ZONES, {}).items()
57  if description.key in zone_data
58  )
59 
60 
62  """Define an Airzone Cloud switch."""
63 
64  entity_description: AirzoneSwitchDescription
65 
66  @callback
67  def _handle_coordinator_update(self) -> None:
68  """Update attributes when the coordinator updates."""
69  self._async_update_attrs_async_update_attrs()
71 
72  @callback
73  def _async_update_attrs(self) -> None:
74  """Update switch attributes."""
75  self._attr_is_on_attr_is_on = self.get_airzone_valueget_airzone_value(self.entity_description.key)
76 
77 
79  """Define an Airzone Cloud Zone switch."""
80 
81  def __init__(
82  self,
83  coordinator: AirzoneUpdateCoordinator,
84  description: AirzoneSwitchDescription,
85  zone_id: str,
86  zone_data: dict[str, Any],
87  ) -> None:
88  """Initialize."""
89  super().__init__(coordinator, zone_id, zone_data)
90 
91  self._attr_name_attr_name = None
92  self._attr_unique_id_attr_unique_id = f"{zone_id}_{description.key}"
93  self.entity_descriptionentity_description = description
94 
95  self._async_update_attrs_async_update_attrs()
96 
97  async def async_turn_on(self, **kwargs: Any) -> None:
98  """Turn the entity on."""
99  param = self.entity_descriptionentity_description.api_param
100  params: dict[str, Any] = {
101  param: {
102  API_VALUE: True,
103  }
104  }
105  await self._async_update_params_async_update_params_async_update_params(params)
106 
107  async def async_turn_off(self, **kwargs: Any) -> None:
108  """Turn the entity off."""
109  param = self.entity_descriptionentity_description.api_param
110  params: dict[str, Any] = {
111  param: {
112  API_VALUE: False,
113  }
114  }
115  await self._async_update_params_async_update_params_async_update_params(params)
None _async_update_params(self, dict[str, Any] params)
Definition: entity.py:53
None _async_update_params(self, dict[str, Any] params)
Definition: entity.py:327
None __init__(self, AirzoneUpdateCoordinator coordinator, AirzoneSwitchDescription description, str zone_id, dict[str, Any] zone_data)
Definition: switch.py:87
None async_setup_entry(HomeAssistant hass, AirzoneCloudConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:43