Home Assistant Unofficial Reference 2024.12.1
water_heater.py
Go to the documentation of this file.
1 """Support for the Airzone Cloud water heater."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, Final
6 
7 from aioairzone_cloud.common import HotWaterOperation, TemperatureUnit
8 from aioairzone_cloud.const import (
9  API_OPTS,
10  API_POWER,
11  API_POWERFUL_MODE,
12  API_SETPOINT,
13  API_UNITS,
14  API_VALUE,
15  AZD_HOT_WATERS,
16  AZD_OPERATION,
17  AZD_OPERATIONS,
18  AZD_TEMP,
19  AZD_TEMP_SET,
20  AZD_TEMP_SET_MAX,
21  AZD_TEMP_SET_MIN,
22 )
23 
25  STATE_ECO,
26  STATE_PERFORMANCE,
27  WaterHeaterEntity,
28  WaterHeaterEntityFeature,
29 )
30 from homeassistant.const import ATTR_TEMPERATURE, STATE_OFF, UnitOfTemperature
31 from homeassistant.core import HomeAssistant, callback
32 from homeassistant.helpers.entity_platform import AddEntitiesCallback
33 
34 from . import AirzoneCloudConfigEntry
35 from .coordinator import AirzoneUpdateCoordinator
36 from .entity import AirzoneHotWaterEntity
37 
38 OPERATION_LIB_TO_HASS: Final[dict[HotWaterOperation, str]] = {
39  HotWaterOperation.Off: STATE_OFF,
40  HotWaterOperation.On: STATE_ECO,
41  HotWaterOperation.Powerful: STATE_PERFORMANCE,
42 }
43 
44 OPERATION_MODE_TO_DHW_PARAMS: Final[dict[str, dict[str, Any]]] = {
45  STATE_OFF: {
46  API_POWER: {
47  API_VALUE: False,
48  },
49  },
50  STATE_ECO: {
51  API_POWER: {
52  API_VALUE: True,
53  },
54  API_POWERFUL_MODE: {
55  API_VALUE: False,
56  },
57  },
58  STATE_PERFORMANCE: {
59  API_POWER: {
60  API_VALUE: True,
61  },
62  API_POWERFUL_MODE: {
63  API_VALUE: True,
64  },
65  },
66 }
67 
68 
70  hass: HomeAssistant,
71  entry: AirzoneCloudConfigEntry,
72  async_add_entities: AddEntitiesCallback,
73 ) -> None:
74  """Add Airzone Cloud Water Heater from a config_entry."""
75  coordinator = entry.runtime_data
76 
79  coordinator,
80  dhw_id,
81  dhw_data,
82  )
83  for dhw_id, dhw_data in coordinator.data.get(AZD_HOT_WATERS, {}).items()
84  )
85 
86 
88  """Define an Airzone Cloud Water Heater."""
89 
90  _attr_name = None
91  _attr_supported_features = (
92  WaterHeaterEntityFeature.TARGET_TEMPERATURE
93  | WaterHeaterEntityFeature.ON_OFF
94  | WaterHeaterEntityFeature.OPERATION_MODE
95  )
96  _attr_temperature_unit = UnitOfTemperature.CELSIUS
97 
98  def __init__(
99  self,
100  coordinator: AirzoneUpdateCoordinator,
101  dhw_id: str,
102  dhw_data: dict,
103  ) -> None:
104  """Initialize Airzone Cloud Water Heater."""
105  super().__init__(coordinator, dhw_id, dhw_data)
106 
107  self._attr_unique_id_attr_unique_id = dhw_id
108  self._attr_operation_list_attr_operation_list = [
109  OPERATION_LIB_TO_HASS[operation]
110  for operation in self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_OPERATIONS)
111  ]
112 
113  self._async_update_attrs_async_update_attrs()
114 
115  async def async_turn_off(self, **kwargs: Any) -> None:
116  """Turn the water heater off."""
117  params = {
118  API_POWER: {
119  API_VALUE: False,
120  },
121  }
122  await self._async_update_params_async_update_params_async_update_params(params)
123 
124  async def async_turn_on(self, **kwargs: Any) -> None:
125  """Turn the water heater off."""
126  params = {
127  API_POWER: {
128  API_VALUE: True,
129  },
130  }
131  await self._async_update_params_async_update_params_async_update_params(params)
132 
133  async def async_set_operation_mode(self, operation_mode: str) -> None:
134  """Set new target operation mode."""
135  params = OPERATION_MODE_TO_DHW_PARAMS.get(operation_mode, {})
136  await self._async_update_params_async_update_params_async_update_params(params)
137 
138  async def async_set_temperature(self, **kwargs: Any) -> None:
139  """Set new target temperature."""
140  params: dict[str, Any] = {}
141  if ATTR_TEMPERATURE in kwargs:
142  params[API_SETPOINT] = {
143  API_VALUE: kwargs[ATTR_TEMPERATURE],
144  API_OPTS: {
145  API_UNITS: TemperatureUnit.CELSIUS.value,
146  },
147  }
148  await self._async_update_params_async_update_params_async_update_params(params)
149 
150  @callback
151  def _handle_coordinator_update(self) -> None:
152  """Update attributes when the coordinator updates."""
153  self._async_update_attrs_async_update_attrs()
155 
156  @callback
157  def _async_update_attrs(self) -> None:
158  """Update water heater attributes."""
159  self._attr_current_temperature_attr_current_temperature = self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_TEMP)
160  self._attr_current_operation_attr_current_operation = OPERATION_LIB_TO_HASS[
161  self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_OPERATION)
162  ]
163  self._attr_max_temp_attr_max_temp = self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_TEMP_SET_MAX)
164  self._attr_min_temp_attr_min_temp = self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_TEMP_SET_MIN)
165  self._attr_target_temperature_attr_target_temperature = self.get_airzone_valueget_airzone_valueget_airzone_value(AZD_TEMP_SET)
None _async_update_params(self, dict[str, Any] params)
Definition: entity.py:53
None __init__(self, AirzoneUpdateCoordinator coordinator, str dhw_id, dict dhw_data)
None async_setup_entry(HomeAssistant hass, AirzoneCloudConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: water_heater.py:73