Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Support for Toon thermostat."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from toonapi import (
8  ACTIVE_STATE_AWAY,
9  ACTIVE_STATE_COMFORT,
10  ACTIVE_STATE_HOME,
11  ACTIVE_STATE_SLEEP,
12 )
13 
15  PRESET_AWAY,
16  PRESET_COMFORT,
17  PRESET_HOME,
18  PRESET_SLEEP,
19  ClimateEntity,
20  ClimateEntityFeature,
21  HVACAction,
22  HVACMode,
23 )
24 from homeassistant.config_entries import ConfigEntry
25 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
26 from homeassistant.core import HomeAssistant
27 from homeassistant.helpers.entity_platform import AddEntitiesCallback
28 
29 from . import ToonDataUpdateCoordinator
30 from .const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, DOMAIN
31 from .entity import ToonDisplayDeviceEntity
32 from .helpers import toon_exception_handler
33 
34 
36  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
37 ) -> None:
38  """Set up a Toon binary sensors based on a config entry."""
39  coordinator = hass.data[DOMAIN][entry.entry_id]
41 
42 
44  """Representation of a Toon climate device."""
45 
46  _attr_hvac_mode = HVACMode.HEAT
47  _attr_icon = "mdi:thermostat"
48  _attr_max_temp = DEFAULT_MAX_TEMP
49  _attr_min_temp = DEFAULT_MIN_TEMP
50  _attr_name = "Thermostat"
51  _attr_supported_features = (
52  ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
53  )
54  _attr_temperature_unit = UnitOfTemperature.CELSIUS
55  _enable_turn_on_off_backwards_compatibility = False
56 
57  def __init__(
58  self,
59  coordinator: ToonDataUpdateCoordinator,
60  ) -> None:
61  """Initialize Toon climate entity."""
62  super().__init__(coordinator)
63  self._attr_hvac_modes_attr_hvac_modes = [HVACMode.HEAT]
64  self._attr_preset_modes_attr_preset_modes = [
65  PRESET_AWAY,
66  PRESET_COMFORT,
67  PRESET_HOME,
68  PRESET_SLEEP,
69  ]
70  self._attr_unique_id_attr_unique_id = (
71  f"{DOMAIN}_{coordinator.data.agreement.agreement_id}_climate"
72  )
73 
74  @property
75  def hvac_action(self) -> HVACAction:
76  """Return the current running hvac operation."""
77  if self.coordinator.data.thermostat.heating:
78  return HVACAction.HEATING
79  return HVACAction.IDLE
80 
81  @property
82  def preset_mode(self) -> str | None:
83  """Return the current preset mode, e.g., home, away, temp."""
84  mapping = {
85  ACTIVE_STATE_AWAY: PRESET_AWAY,
86  ACTIVE_STATE_COMFORT: PRESET_COMFORT,
87  ACTIVE_STATE_HOME: PRESET_HOME,
88  ACTIVE_STATE_SLEEP: PRESET_SLEEP,
89  }
90  return mapping.get(self.coordinator.data.thermostat.active_state)
91 
92  @property
93  def current_temperature(self) -> float | None:
94  """Return the current temperature."""
95  return self.coordinator.data.thermostat.current_display_temperature
96 
97  @property
98  def target_temperature(self) -> float | None:
99  """Return the temperature we try to reach."""
100  return self.coordinator.data.thermostat.current_setpoint
101 
102  @property
103  def extra_state_attributes(self) -> dict[str, Any]:
104  """Return the current state of the burner."""
105  return {"heating_type": self.coordinator.data.agreement.heating_type}
106 
107  @toon_exception_handler
108  async def async_set_temperature(self, **kwargs: Any) -> None:
109  """Change the setpoint of the thermostat."""
110  temperature = kwargs.get(ATTR_TEMPERATURE)
111  await self.coordinator.toon.set_current_setpoint(temperature)
112 
113  @toon_exception_handler
114  async def async_set_preset_mode(self, preset_mode: str) -> None:
115  """Set new preset mode."""
116  mapping = {
117  PRESET_AWAY: ACTIVE_STATE_AWAY,
118  PRESET_COMFORT: ACTIVE_STATE_COMFORT,
119  PRESET_HOME: ACTIVE_STATE_HOME,
120  PRESET_SLEEP: ACTIVE_STATE_SLEEP,
121  }
122  if preset_mode in mapping:
123  await self.coordinator.toon.set_active_state(mapping[preset_mode])
124 
125  def set_hvac_mode(self, hvac_mode: HVACMode) -> None:
126  """Set new target hvac mode."""
127  # Intentionally left empty
128  # The HAVC mode is always HEAT
None __init__(self, ToonDataUpdateCoordinator coordinator)
Definition: climate.py:60
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:37