Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Entity classes for the Airzone integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from aioairzone.const import (
9  API_SYSTEM_ID,
10  API_ZONE_ID,
11  AZD_AVAILABLE,
12  AZD_FIRMWARE,
13  AZD_FULL_NAME,
14  AZD_HOT_WATER,
15  AZD_ID,
16  AZD_MAC,
17  AZD_MODEL,
18  AZD_NAME,
19  AZD_SYSTEM,
20  AZD_SYSTEMS,
21  AZD_THERMOSTAT_FW,
22  AZD_THERMOSTAT_MODEL,
23  AZD_WEBSERVER,
24  AZD_ZONES,
25 )
26 from aioairzone.exceptions import AirzoneError
27 
28 from homeassistant.config_entries import ConfigEntry
29 from homeassistant.exceptions import HomeAssistantError
30 from homeassistant.helpers import device_registry as dr
31 from homeassistant.helpers.device_registry import DeviceInfo
32 from homeassistant.helpers.update_coordinator import CoordinatorEntity
33 
34 from . import AirzoneConfigEntry
35 from .const import DOMAIN, MANUFACTURER
36 from .coordinator import AirzoneUpdateCoordinator
37 
38 _LOGGER = logging.getLogger(__name__)
39 
40 
41 class AirzoneEntity(CoordinatorEntity[AirzoneUpdateCoordinator]):
42  """Define an Airzone entity."""
43 
44  _attr_has_entity_name = True
45 
46  def get_airzone_value(self, key: str) -> Any:
47  """Return Airzone entity value by key."""
48  raise NotImplementedError
49 
50 
52  """Define an Airzone System entity."""
53 
54  def __init__(
55  self,
56  coordinator: AirzoneUpdateCoordinator,
57  entry: AirzoneConfigEntry,
58  system_data: dict[str, Any],
59  ) -> None:
60  """Initialize."""
61  super().__init__(coordinator)
62 
63  self.system_idsystem_id = system_data[AZD_ID]
64 
65  self._attr_device_info_attr_device_info = DeviceInfo(
66  identifiers={(DOMAIN, f"{entry.entry_id}_{self.system_id}")},
67  manufacturer=MANUFACTURER,
68  model=self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_MODEL),
69  name=f"System {self.system_id}",
70  sw_version=self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_FIRMWARE),
71  via_device=(DOMAIN, f"{entry.entry_id}_ws"),
72  )
73  self._attr_unique_id_attr_unique_id = entry.unique_id or entry.entry_id
74 
75  @property
76  def available(self) -> bool:
77  """Return system availability."""
78  return super().available and self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_AVAILABLE)
79 
80  def get_airzone_value(self, key: str) -> Any:
81  """Return system value by key."""
82  value = None
83  if system := self.coordinator.data[AZD_SYSTEMS].get(self.system_idsystem_id):
84  if key in system:
85  value = system[key]
86  return value
87 
88 
90  """Define an Airzone Hot Water entity."""
91 
92  def __init__(
93  self,
94  coordinator: AirzoneUpdateCoordinator,
95  entry: ConfigEntry,
96  ) -> None:
97  """Initialize."""
98  super().__init__(coordinator)
99 
100  self._attr_device_info_attr_device_info = DeviceInfo(
101  identifiers={(DOMAIN, f"{entry.entry_id}_dhw")},
102  manufacturer=MANUFACTURER,
103  model="DHW",
104  name=self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_NAME),
105  via_device=(DOMAIN, f"{entry.entry_id}_ws"),
106  )
107  self._attr_unique_id_attr_unique_id = entry.unique_id or entry.entry_id
108 
109  def get_airzone_value(self, key: str) -> Any:
110  """Return DHW value by key."""
111  return self.coordinator.data[AZD_HOT_WATER].get(key)
112 
113  async def _async_update_dhw_params(self, params: dict[str, Any]) -> None:
114  """Send DHW parameters to API."""
115  _params = {
116  API_SYSTEM_ID: 0,
117  **params,
118  }
119  _LOGGER.debug("update_dhw_params=%s", _params)
120  try:
121  await self.coordinator.airzone.set_dhw_parameters(_params)
122  except AirzoneError as error:
123  raise HomeAssistantError(f"Failed to set DHW: {error}") from error
124 
125  self.coordinator.async_set_updated_data(self.coordinator.airzone.data())
126 
127 
129  """Define an Airzone WebServer entity."""
130 
131  def __init__(
132  self,
133  coordinator: AirzoneUpdateCoordinator,
134  entry: ConfigEntry,
135  ) -> None:
136  """Initialize."""
137  super().__init__(coordinator)
138 
139  mac = self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_MAC)
140 
141  self._attr_device_info_attr_device_info = DeviceInfo(
142  connections={(dr.CONNECTION_NETWORK_MAC, mac)},
143  identifiers={(DOMAIN, f"{entry.entry_id}_ws")},
144  manufacturer=MANUFACTURER,
145  model=self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_MODEL),
146  name=self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_FULL_NAME),
147  sw_version=self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_FIRMWARE),
148  )
149  self._attr_unique_id_attr_unique_id = entry.unique_id or entry.entry_id
150 
151  def get_airzone_value(self, key: str) -> Any:
152  """Return system value by key."""
153  return self.coordinator.data[AZD_WEBSERVER].get(key)
154 
155 
157  """Define an Airzone Zone entity."""
158 
159  def __init__(
160  self,
161  coordinator: AirzoneUpdateCoordinator,
162  entry: ConfigEntry,
163  system_zone_id: str,
164  zone_data: dict[str, Any],
165  ) -> None:
166  """Initialize."""
167  super().__init__(coordinator)
168 
169  self.system_idsystem_id = zone_data[AZD_SYSTEM]
170  self.system_zone_idsystem_zone_id = system_zone_id
171  self.zone_idzone_id = zone_data[AZD_ID]
172 
173  self._attr_device_info_attr_device_info = DeviceInfo(
174  identifiers={(DOMAIN, f"{entry.entry_id}_{system_zone_id}")},
175  manufacturer=MANUFACTURER,
176  model=self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_THERMOSTAT_MODEL),
177  name=zone_data[AZD_NAME],
178  sw_version=self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_THERMOSTAT_FW),
179  via_device=(DOMAIN, f"{entry.entry_id}_{self.system_id}"),
180  )
181  self._attr_unique_id_attr_unique_id = entry.unique_id or entry.entry_id
182 
183  @property
184  def available(self) -> bool:
185  """Return zone availability."""
186  return super().available and self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_AVAILABLE)
187 
188  def get_airzone_value(self, key: str) -> Any:
189  """Return zone value by key."""
190  value = None
191  if zone := self.coordinator.data[AZD_ZONES].get(self.system_zone_idsystem_zone_id):
192  if key in zone:
193  value = zone[key]
194  return value
195 
196  async def _async_update_hvac_params(self, params: dict[str, Any]) -> None:
197  """Send HVAC parameters to API."""
198  _params = {
199  API_SYSTEM_ID: self.system_idsystem_id,
200  API_ZONE_ID: self.zone_idzone_id,
201  **params,
202  }
203  _LOGGER.debug("update_hvac_params=%s", _params)
204  try:
205  await self.coordinator.airzone.set_hvac_parameters(_params)
206  except AirzoneError as error:
207  raise HomeAssistantError(
208  f"Failed to set zone {self.entity_id}: {error}"
209  ) from error
210 
211  self.coordinator.async_set_updated_data(self.coordinator.airzone.data())
None _async_update_dhw_params(self, dict[str, Any] params)
Definition: entity.py:113
None __init__(self, AirzoneUpdateCoordinator coordinator, ConfigEntry entry)
Definition: entity.py:96
None __init__(self, AirzoneUpdateCoordinator coordinator, AirzoneConfigEntry entry, dict[str, Any] system_data)
Definition: entity.py:59
None __init__(self, AirzoneUpdateCoordinator coordinator, ConfigEntry entry)
Definition: entity.py:135
None __init__(self, AirzoneUpdateCoordinator coordinator, ConfigEntry entry, str system_zone_id, dict[str, Any] zone_data)
Definition: entity.py:165
None _async_update_hvac_params(self, dict[str, Any] params)
Definition: entity.py:196
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88