Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Platform for climate integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from smarttub import Spa
8 
10  PRESET_ECO,
11  PRESET_NONE,
12  ClimateEntity,
13  ClimateEntityFeature,
14  HVACAction,
15  HVACMode,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.util.unit_conversion import TemperatureConverter
22 
23 from .const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, DOMAIN, SMARTTUB_CONTROLLER
24 from .entity import SmartTubEntity
25 
26 PRESET_DAY = "day"
27 PRESET_READY = "ready"
28 
29 PRESET_MODES = {
30  Spa.HeatMode.AUTO: PRESET_NONE,
31  Spa.HeatMode.ECONOMY: PRESET_ECO,
32  Spa.HeatMode.DAY: PRESET_DAY,
33  Spa.HeatMode.READY: PRESET_READY,
34 }
35 
36 HEAT_MODES = {v: k for k, v in PRESET_MODES.items()}
37 
38 HVAC_ACTIONS = {
39  "OFF": HVACAction.IDLE,
40  "ON": HVACAction.HEATING,
41 }
42 
43 
45  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
46 ) -> None:
47  """Set up climate entity for the thermostat in the tub."""
48 
49  controller = hass.data[DOMAIN][entry.entry_id][SMARTTUB_CONTROLLER]
50 
51  entities = [
52  SmartTubThermostat(controller.coordinator, spa) for spa in controller.spas
53  ]
54 
55  async_add_entities(entities)
56 
57 
59  """The target water temperature for the spa."""
60 
61  # SmartTub devices don't seem to have the option of disabling the heater,
62  # so this is always HVACMode.HEAT.
63  _attr_hvac_mode = HVACMode.HEAT
64  _attr_hvac_modes = [HVACMode.HEAT]
65  # Only target temperature is supported.
66  _attr_supported_features = (
67  ClimateEntityFeature.PRESET_MODE | ClimateEntityFeature.TARGET_TEMPERATURE
68  )
69  _attr_temperature_unit = UnitOfTemperature.CELSIUS
70  _attr_preset_modes = list(PRESET_MODES.values())
71  _enable_turn_on_off_backwards_compatibility = False
72 
73  def __init__(self, coordinator, spa):
74  """Initialize the entity."""
75  super().__init__(coordinator, spa, "Thermostat")
76 
77  @property
78  def hvac_action(self) -> HVACAction | None:
79  """Return the current running hvac operation."""
80  return HVAC_ACTIONS.get(self.spa_statusspa_status.heater)
81 
82  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
83  """Set new target hvac mode.
84 
85  As with hvac_mode, we don't really have an option here.
86  """
87  if hvac_mode == HVACMode.HEAT:
88  return
89  raise NotImplementedError(hvac_mode)
90 
91  @property
92  def min_temp(self):
93  """Return the minimum temperature."""
94  min_temp = DEFAULT_MIN_TEMP
95  return TemperatureConverter.convert(
96  min_temp, UnitOfTemperature.CELSIUS, self.temperature_unittemperature_unit
97  )
98 
99  @property
100  def max_temp(self):
101  """Return the maximum temperature."""
102  max_temp = DEFAULT_MAX_TEMP
103  return TemperatureConverter.convert(
104  max_temp, UnitOfTemperature.CELSIUS, self.temperature_unittemperature_unit
105  )
106 
107  @property
108  def preset_mode(self):
109  """Return the current preset mode."""
110  return PRESET_MODES[self.spa_statusspa_status.heat_mode]
111 
112  @property
114  """Return the current water temperature."""
115  return self.spa_statusspa_status.water.temperature
116 
117  @property
119  """Return the target water temperature."""
120  return self.spa_statusspa_status.set_temperature
121 
122  async def async_set_temperature(self, **kwargs: Any) -> None:
123  """Set new target temperature."""
124  temperature = kwargs[ATTR_TEMPERATURE]
125  await self.spaspa.set_temperature(temperature)
126  await self.coordinator.async_refresh()
127 
128  async def async_set_preset_mode(self, preset_mode: str) -> None:
129  """Activate the specified preset mode."""
130  heat_mode = HEAT_MODES[preset_mode]
131  await self.spaspa.set_heat_mode(heat_mode)
132  await self.coordinator.async_refresh()
None set_temperature(self, **Any kwargs)
Definition: __init__.py:771
None async_set_hvac_mode(self, HVACMode hvac_mode)
Definition: climate.py:82
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:46