Home Assistant Unofficial Reference 2024.12.1
water_heater.py
Go to the documentation of this file.
1 """The water heater platform for the A. O. Smith integration."""
2 
3 from typing import Any
4 
5 from py_aosmith.models import OperationMode as AOSmithOperationMode
6 
8  STATE_ECO,
9  STATE_ELECTRIC,
10  STATE_HEAT_PUMP,
11  STATE_OFF,
12  WaterHeaterEntity,
13  WaterHeaterEntityFeature,
14 )
15 from homeassistant.const import UnitOfTemperature
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import HomeAssistantError
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import AOSmithConfigEntry
21 from .coordinator import AOSmithStatusCoordinator
22 from .entity import AOSmithStatusEntity
23 
24 MODE_HA_TO_AOSMITH = {
25  STATE_ECO: AOSmithOperationMode.HYBRID,
26  STATE_ELECTRIC: AOSmithOperationMode.ELECTRIC,
27  STATE_HEAT_PUMP: AOSmithOperationMode.HEAT_PUMP,
28  STATE_OFF: AOSmithOperationMode.VACATION,
29 }
30 MODE_AOSMITH_TO_HA = {
31  AOSmithOperationMode.ELECTRIC: STATE_ELECTRIC,
32  AOSmithOperationMode.HEAT_PUMP: STATE_HEAT_PUMP,
33  AOSmithOperationMode.HYBRID: STATE_ECO,
34  AOSmithOperationMode.VACATION: STATE_OFF,
35 }
36 
37 # Priority list for operation mode to use when exiting away mode
38 # Will use the first mode that is supported by the device
39 DEFAULT_OPERATION_MODE_PRIORITY = [
40  AOSmithOperationMode.HYBRID,
41  AOSmithOperationMode.HEAT_PUMP,
42  AOSmithOperationMode.ELECTRIC,
43 ]
44 
45 
47  hass: HomeAssistant,
48  entry: AOSmithConfigEntry,
49  async_add_entities: AddEntitiesCallback,
50 ) -> None:
51  """Set up A. O. Smith water heater platform."""
52  data = entry.runtime_data
53 
55  AOSmithWaterHeaterEntity(data.status_coordinator, junction_id)
56  for junction_id in data.status_coordinator.data
57  )
58 
59 
61  """The water heater entity for the A. O. Smith integration."""
62 
63  _attr_name = None
64  _attr_temperature_unit = UnitOfTemperature.FAHRENHEIT
65  _attr_min_temp = 95
66 
67  def __init__(
68  self,
69  coordinator: AOSmithStatusCoordinator,
70  junction_id: str,
71  ) -> None:
72  """Initialize the entity."""
73  super().__init__(coordinator, junction_id)
74  self._attr_unique_id_attr_unique_id = junction_id
75 
76  @property
77  def operation_list(self) -> list[str]:
78  """Return the list of supported operation modes."""
79  ha_modes = []
80  for supported_mode in self.devicedevice.supported_modes:
81  ha_mode = MODE_AOSMITH_TO_HA.get(supported_mode.mode)
82 
83  # Filtering out STATE_OFF since it is handled by away mode
84  if ha_mode is not None and ha_mode != STATE_OFF:
85  ha_modes.append(ha_mode)
86 
87  return ha_modes
88 
89  @property
90  def supported_features(self) -> WaterHeaterEntityFeature:
91  """Return the list of supported features."""
92  supports_vacation_mode = any(
93  supported_mode.mode == AOSmithOperationMode.VACATION
94  for supported_mode in self.devicedevice.supported_modes
95  )
96 
97  support_flags = WaterHeaterEntityFeature.TARGET_TEMPERATURE
98 
99  # Operation mode only supported if there is more than one mode
100  if len(self.operation_listoperation_listoperation_list) > 1:
101  support_flags |= WaterHeaterEntityFeature.OPERATION_MODE
102 
103  if supports_vacation_mode:
104  support_flags |= WaterHeaterEntityFeature.AWAY_MODE
105 
106  return support_flags
107 
108  @property
109  def target_temperature(self) -> float | None:
110  """Return the temperature we try to reach."""
111  return self.devicedevice.status.temperature_setpoint
112 
113  @property
114  def max_temp(self) -> float:
115  """Return the maximum temperature."""
116  return self.devicedevice.status.temperature_setpoint_maximum
117 
118  @property
119  def current_operation(self) -> str:
120  """Return the current operation mode."""
121  return MODE_AOSMITH_TO_HA.get(self.devicedevice.status.current_mode, STATE_OFF)
122 
123  @property
124  def is_away_mode_on(self):
125  """Return True if away mode is on."""
126  return self.devicedevice.status.current_mode == AOSmithOperationMode.VACATION
127 
128  async def async_set_operation_mode(self, operation_mode: str) -> None:
129  """Set new target operation mode."""
130  if operation_mode not in self.operation_listoperation_listoperation_list:
131  raise HomeAssistantError("Operation mode not supported")
132 
133  aosmith_mode = MODE_HA_TO_AOSMITH.get(operation_mode)
134  if aosmith_mode is not None:
135  await self.clientclient.update_mode(self.junction_id, aosmith_mode)
136 
137  await self.coordinator.async_request_refresh()
138 
139  async def async_set_temperature(self, **kwargs: Any) -> None:
140  """Set new target temperature."""
141  temperature = kwargs.get("temperature")
142  if temperature is not None:
143  await self.clientclient.update_setpoint(self.junction_id, temperature)
144 
145  await self.coordinator.async_request_refresh()
146 
147  async def async_turn_away_mode_on(self) -> None:
148  """Turn away mode on."""
149  await self.clientclient.update_mode(self.junction_id, AOSmithOperationMode.VACATION)
150 
151  await self.coordinator.async_request_refresh()
152 
153  async def async_turn_away_mode_off(self) -> None:
154  """Turn away mode off."""
155  supported_aosmith_modes = [x.mode for x in self.devicedevice.supported_modes]
156 
157  for mode in DEFAULT_OPERATION_MODE_PRIORITY:
158  if mode in supported_aosmith_modes:
159  await self.clientclient.update_mode(self.junction_id, mode)
160  break
None __init__(self, AOSmithStatusCoordinator coordinator, str junction_id)
Definition: water_heater.py:71
None async_setup_entry(HomeAssistant hass, AOSmithConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: water_heater.py:50