Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Support for Velbus thermostat."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from velbusaio.channels import Temperature as VelbusTemp
8 
10  ClimateEntity,
11  ClimateEntityFeature,
12  HVACMode,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import ServiceValidationError
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import DOMAIN, PRESET_MODES
21 from .entity import VelbusEntity, api_call
22 
23 
25  hass: HomeAssistant,
26  entry: ConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Set up Velbus switch based on config_entry."""
30  await hass.data[DOMAIN][entry.entry_id]["tsk"]
31  cntrl = hass.data[DOMAIN][entry.entry_id]["cntrl"]
32  async_add_entities(VelbusClimate(channel) for channel in cntrl.get_all("climate"))
33 
34 
36  """Representation of a Velbus thermostat."""
37 
38  _channel: VelbusTemp
39  _attr_supported_features = (
40  ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
41  )
42  _attr_temperature_unit = UnitOfTemperature.CELSIUS
43  _attr_hvac_modes = [HVACMode.HEAT, HVACMode.COOL]
44  _attr_preset_modes = list(PRESET_MODES)
45  _enable_turn_on_off_backwards_compatibility = False
46 
47  @property
48  def target_temperature(self) -> float | None:
49  """Return the temperature we try to reach."""
50  return self._channel_channel.get_climate_target()
51 
52  @property
53  def preset_mode(self) -> str | None:
54  """Return the current Preset for this channel."""
55  return next(
56  (
57  key
58  for key, val in PRESET_MODES.items()
59  if val == self._channel_channel.get_climate_preset()
60  ),
61  None,
62  )
63 
64  @property
65  def current_temperature(self) -> int | None:
66  """Return the current temperature."""
67  return self._channel_channel.get_state()
68 
69  @property
70  def hvac_mode(self) -> HVACMode:
71  """Return the current hvac mode based on cool_mode message."""
72  return HVACMode.COOL if self._channel_channel.get_cool_mode() else HVACMode.HEAT
73 
74  @api_call
75  async def async_set_temperature(self, **kwargs: Any) -> None:
76  """Set new target temperatures."""
77  if (temp := kwargs.get(ATTR_TEMPERATURE)) is None:
78  return
79  await self._channel_channel.set_temp(temp)
80  self.async_write_ha_stateasync_write_ha_state()
81 
82  @api_call
83  async def async_set_preset_mode(self, preset_mode: str) -> None:
84  """Set the new preset mode."""
85  await self._channel_channel.set_preset(PRESET_MODES[preset_mode])
86  self.async_write_ha_stateasync_write_ha_state()
87 
88  @api_call
89  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
90  """Set the new hvac mode."""
91  if hvac_mode not in self._attr_hvac_modes_attr_hvac_modes:
93  translation_domain=DOMAIN,
94  translation_key="invalid_hvac_mode",
95  translation_placeholders={"hvac_mode": str(hvac_mode)},
96  )
97  await self._channel_channel.set_mode(hvac_mode)
98  self.async_write_ha_stateasync_write_ha_state()
None async_set_preset_mode(self, str preset_mode)
Definition: climate.py:83
None async_set_hvac_mode(self, HVACMode hvac_mode)
Definition: climate.py:89
str|float get_state(dict[str, float] data, str key)
Definition: sensor.py:26
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:28