Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Roth Touchline SL climate integration implementation for Home Assistant."""
2 
3 from typing import Any
4 
6  ClimateEntity,
7  ClimateEntityFeature,
8  HVACAction,
9  HVACMode,
10 )
11 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import TouchlineSLConfigEntry
16 from .coordinator import TouchlineSLModuleCoordinator
17 from .entity import TouchlineSLZoneEntity
18 
19 
21  hass: HomeAssistant,
22  entry: TouchlineSLConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Set up the Touchline devices."""
26  coordinators = entry.runtime_data
28  TouchlineSLZone(coordinator=coordinator, zone_id=zone_id)
29  for coordinator in coordinators
30  for zone_id in coordinator.data.zones
31  )
32 
33 
34 CONSTANT_TEMPERATURE = "constant_temperature"
35 
36 
38  """Roth Touchline SL Zone."""
39 
40  _attr_hvac_action = HVACAction.IDLE
41  _attr_hvac_mode = HVACMode.HEAT
42  _attr_hvac_modes = [HVACMode.HEAT]
43  _attr_name = None
44  _attr_supported_features = (
45  ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
46  )
47  _attr_temperature_unit = UnitOfTemperature.CELSIUS
48  _attr_translation_key = "zone"
49 
50  def __init__(self, coordinator: TouchlineSLModuleCoordinator, zone_id: int) -> None:
51  """Construct a Touchline SL climate zone."""
52  super().__init__(coordinator, zone_id)
53 
54  self._attr_unique_id_attr_unique_id = (
55  f"module-{self.coordinator.data.module.id}-zone-{self.zone_id}"
56  )
57 
58  # Call this in __init__ so data is populated right away, since it's
59  # already available in the coordinator data.
60  self.set_attrset_attr()
61 
62  @callback
63  def _handle_coordinator_update(self) -> None:
64  """Handle updated data from the coordinator."""
65  self.set_attrset_attr()
67 
68  async def async_set_temperature(self, **kwargs: Any) -> None:
69  """Set new target temperature."""
70  if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
71  return
72 
73  await self.zonezone.set_temperature(temperature)
74  await self.coordinator.async_request_refresh()
75 
76  async def async_set_preset_mode(self, preset_mode: str) -> None:
77  """Assign the zone to a particular global schedule."""
78  if not self.zonezone:
79  return
80 
81  if preset_mode == CONSTANT_TEMPERATURE and self._attr_target_temperature_attr_target_temperature:
82  await self.zonezone.set_temperature(temperature=self._attr_target_temperature_attr_target_temperature)
83  await self.coordinator.async_request_refresh()
84  return
85 
86  if schedule := self.coordinator.data.schedules[preset_mode]:
87  await self.zonezone.set_schedule(schedule_id=schedule.id)
88  await self.coordinator.async_request_refresh()
89 
90  def set_attr(self) -> None:
91  """Populate attributes with data from the coordinator."""
92  schedule_names = self.coordinator.data.schedules.keys()
93 
94  self._attr_current_temperature_attr_current_temperature = self.zonezone.temperature
95  self._attr_target_temperature_attr_target_temperature = self.zonezone.target_temperature
96  self._attr_current_humidity_attr_current_humidity = int(self.zonezone.humidity)
97  self._attr_preset_modes_attr_preset_modes = [*schedule_names, CONSTANT_TEMPERATURE]
98 
99  if self.zonezone.mode == "constantTemp":
100  self._attr_preset_mode_attr_preset_mode = CONSTANT_TEMPERATURE
101  elif self.zonezone.mode == "globalSchedule":
102  schedule = self.zonezone.schedule
103  self._attr_preset_mode_attr_preset_mode = schedule.name
104 
105  if self.zonezone.algorithm == "heating":
106  self._attr_hvac_action_attr_hvac_action = (
107  HVACAction.HEATING if self.zonezone.relay_on else HVACAction.IDLE
108  )
109  self._attr_hvac_mode_attr_hvac_mode = HVACMode.HEAT
110  self._attr_hvac_modes_attr_hvac_modes_attr_hvac_modes = [HVACMode.HEAT]
111  elif self.zonezone.algorithm == "cooling":
112  self._attr_hvac_action_attr_hvac_action = (
113  HVACAction.COOLING if self.zonezone.relay_on else HVACAction.IDLE
114  )
115  self._attr_hvac_mode_attr_hvac_mode = HVACMode.COOL
116  self._attr_hvac_modes_attr_hvac_modes_attr_hvac_modes = [HVACMode.COOL]
None set_temperature(self, **Any kwargs)
Definition: __init__.py:771
None __init__(self, TouchlineSLModuleCoordinator coordinator, int zone_id)
Definition: climate.py:50
None async_setup_entry(HomeAssistant hass, TouchlineSLConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:24