Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Support for Alpha2 room control unit via Alpha2 base."""
2 
3 import logging
4 from typing import Any
5 
7  ClimateEntity,
8  ClimateEntityFeature,
9  HVACAction,
10  HVACMode,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.update_coordinator import CoordinatorEntity
17 
18 from .const import DOMAIN, PRESET_AUTO, PRESET_DAY, PRESET_NIGHT
19 from .coordinator import Alpha2BaseCoordinator
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
25  hass: HomeAssistant,
26  config_entry: ConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Add Alpha2Climate entities from a config_entry."""
30 
31  coordinator: Alpha2BaseCoordinator = hass.data[DOMAIN][config_entry.entry_id]
32 
34  Alpha2Climate(coordinator, heat_area_id)
35  for heat_area_id in coordinator.data["heat_areas"]
36  )
37 
38 
39 class Alpha2Climate(CoordinatorEntity[Alpha2BaseCoordinator], ClimateEntity):
40  """Alpha2 ClimateEntity."""
41 
42  target_temperature_step = 0.2
43 
44  _attr_supported_features = (
45  ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
46  )
47  _attr_hvac_modes = [HVACMode.HEAT, HVACMode.COOL]
48  _attr_temperature_unit = UnitOfTemperature.CELSIUS
49  _attr_preset_modes = [PRESET_AUTO, PRESET_DAY, PRESET_NIGHT]
50  _enable_turn_on_off_backwards_compatibility = False
51 
52  def __init__(self, coordinator: Alpha2BaseCoordinator, heat_area_id: str) -> None:
53  """Initialize Alpha2 ClimateEntity."""
54  super().__init__(coordinator)
55  self.heat_area_idheat_area_id = heat_area_id
56  self._attr_unique_id_attr_unique_id = heat_area_id
57  self._attr_name_attr_name = self.heat_areaheat_area["HEATAREA_NAME"]
58 
59  @property
60  def heat_area(self) -> dict[str, Any]:
61  """Return the heat area."""
62  return self.coordinator.data["heat_areas"][self.heat_area_idheat_area_id]
63 
64  @property
65  def min_temp(self) -> float:
66  """Return the minimum temperature."""
67  return float(self.heat_areaheat_area.get("T_TARGET_MIN", 0.0))
68 
69  @property
70  def max_temp(self) -> float:
71  """Return the maximum temperature."""
72  return float(self.heat_areaheat_area.get("T_TARGET_MAX", 30.0))
73 
74  @property
75  def current_temperature(self) -> float:
76  """Return the current temperature."""
77  return float(self.heat_areaheat_area.get("T_ACTUAL", 0.0))
78 
79  @property
80  def hvac_mode(self) -> HVACMode:
81  """Return current hvac mode."""
82  if self.coordinator.get_cooling():
83  return HVACMode.COOL
84  return HVACMode.HEAT
85 
86  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
87  """Set new target hvac mode."""
88  await self.coordinator.async_set_cooling(hvac_mode == HVACMode.COOL)
89 
90  @property
91  def hvac_action(self) -> HVACAction:
92  """Return the current running hvac operation."""
93  if not self.heat_areaheat_area["_HEATCTRL_STATE"]:
94  return HVACAction.IDLE
95  if self.coordinator.get_cooling():
96  return HVACAction.COOLING
97  return HVACAction.HEATING
98 
99  @property
100  def target_temperature(self) -> float:
101  """Return the temperature we try to reach."""
102  return float(self.heat_areaheat_area.get("T_TARGET", 0.0))
103 
104  async def async_set_temperature(self, **kwargs: Any) -> None:
105  """Set new target temperatures."""
106  if (target_temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
107  return
108 
109  await self.coordinator.async_set_target_temperature(
110  self.heat_area_idheat_area_id, target_temperature
111  )
112 
113  @property
114  def preset_mode(self) -> str:
115  """Return the current preset mode."""
116  if self.heat_areaheat_area["HEATAREA_MODE"] == 1:
117  return PRESET_DAY
118  if self.heat_areaheat_area["HEATAREA_MODE"] == 2:
119  return PRESET_NIGHT
120  return PRESET_AUTO
121 
122  async def async_set_preset_mode(self, preset_mode: str) -> None:
123  """Set new operation mode."""
124  heat_area_mode = 0
125  if preset_mode == PRESET_DAY:
126  heat_area_mode = 1
127  elif preset_mode == PRESET_NIGHT:
128  heat_area_mode = 2
129 
130  await self.coordinator.async_set_heat_area_mode(
131  self.heat_area_idheat_area_id, heat_area_mode
132  )
None __init__(self, Alpha2BaseCoordinator coordinator, str heat_area_id)
Definition: climate.py:52
None async_set_target_temperature(self, str heat_area_id, float target_temperature)
Definition: coordinator.py:55
None async_set_heat_area_mode(self, str heat_area_id, int heat_area_mode)
Definition: coordinator.py:88
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:28