Home Assistant Unofficial Reference 2024.12.1
water_heater.py
Go to the documentation of this file.
1 """BSBLAN platform to control a compatible Water Heater Device."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from bsblan import BSBLANError
8 
10  STATE_ECO,
11  STATE_OFF,
12  WaterHeaterEntity,
13  WaterHeaterEntityFeature,
14 )
15 from homeassistant.const import ATTR_TEMPERATURE, STATE_ON
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import HomeAssistantError
18 from homeassistant.helpers.device_registry import format_mac
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import BSBLanConfigEntry, BSBLanData
22 from .const import DOMAIN
23 from .entity import BSBLanEntity
24 
25 PARALLEL_UPDATES = 1
26 
27 # Mapping between BSBLan and HA operation modes
28 OPERATION_MODES = {
29  "Eco": STATE_ECO, # Energy saving mode
30  "Off": STATE_OFF, # Protection mode
31  "On": STATE_ON, # Continuous comfort mode
32 }
33 
34 OPERATION_MODES_REVERSE = {v: k for k, v in OPERATION_MODES.items()}
35 
36 
38  hass: HomeAssistant,
39  entry: BSBLanConfigEntry,
40  async_add_entities: AddEntitiesCallback,
41 ) -> None:
42  """Set up BSBLAN water heater based on a config entry."""
43  data = entry.runtime_data
45 
46 
48  """Defines a BSBLAN water heater entity."""
49 
50  _attr_name = None
51  _attr_supported_features = (
52  WaterHeaterEntityFeature.TARGET_TEMPERATURE
53  | WaterHeaterEntityFeature.OPERATION_MODE
54  )
55 
56  def __init__(self, data: BSBLanData) -> None:
57  """Initialize BSBLAN water heater."""
58  super().__init__(data.coordinator, data)
59  self._attr_unique_id_attr_unique_id = format_mac(data.device.MAC)
60  self._attr_operation_list_attr_operation_list = list(OPERATION_MODES_REVERSE.keys())
61 
62  # Set temperature limits based on device capabilities
63  self._attr_temperature_unit_attr_temperature_unit = data.coordinator.client.get_temperature_unit
64  self._attr_min_temp_attr_min_temp = data.coordinator.data.dhw.reduced_setpoint.value
65  self._attr_max_temp_attr_max_temp = data.coordinator.data.dhw.nominal_setpoint_max.value
66 
67  @property
68  def current_operation(self) -> str | None:
69  """Return current operation."""
70  current_mode = self.coordinator.data.dhw.operating_mode.desc
71  return OPERATION_MODES.get(current_mode)
72 
73  @property
74  def current_temperature(self) -> float | None:
75  """Return the current temperature."""
76  return self.coordinator.data.dhw.dhw_actual_value_top_temperature.value
77 
78  @property
79  def target_temperature(self) -> float | None:
80  """Return the temperature we try to reach."""
81  return self.coordinator.data.dhw.nominal_setpoint.value
82 
83  async def async_set_temperature(self, **kwargs: Any) -> None:
84  """Set new target temperature."""
85  temperature = kwargs.get(ATTR_TEMPERATURE)
86  try:
87  await self.coordinator.client.set_hot_water(nominal_setpoint=temperature)
88  except BSBLANError as err:
89  raise HomeAssistantError(
90  translation_domain=DOMAIN,
91  translation_key="set_temperature_error",
92  ) from err
93 
94  await self.coordinator.async_request_refresh()
95 
96  async def async_set_operation_mode(self, operation_mode: str) -> None:
97  """Set new operation mode."""
98  bsblan_mode = OPERATION_MODES_REVERSE.get(operation_mode)
99  try:
100  await self.coordinator.client.set_hot_water(operating_mode=bsblan_mode)
101  except BSBLANError as err:
102  raise HomeAssistantError(
103  translation_domain=DOMAIN,
104  translation_key="set_operation_mode_error",
105  ) from err
106 
107  await self.coordinator.async_request_refresh()
None async_setup_entry(HomeAssistant hass, BSBLanConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: water_heater.py:41