Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Support for Duotecno climate devices."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, Final
6 
7 from duotecno.controller import PyDuotecno
8 from duotecno.unit import SensUnit
9 
11  ClimateEntity,
12  ClimateEntityFeature,
13  HVACMode,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import DOMAIN
21 from .entity import DuotecnoEntity, api_call
22 
23 HVACMODE: Final = {
24  0: HVACMode.OFF,
25  1: HVACMode.HEAT,
26  2: HVACMode.COOL,
27 }
28 HVACMODE_REVERSE: Final = {value: key for key, value in HVACMODE.items()}
29 
30 PRESETMODES: Final = {"sun": 0, "half_sun": 1, "moon": 2, "half_moon": 3}
31 PRESETMODES_REVERSE: Final = {value: key for key, value in PRESETMODES.items()}
32 
33 
35  hass: HomeAssistant,
36  entry: ConfigEntry,
37  async_add_entities: AddEntitiesCallback,
38 ) -> None:
39  """Set up Duotecno climate based on config_entry."""
40  cntrl: PyDuotecno = hass.data[DOMAIN][entry.entry_id]
42  DuotecnoClimate(channel) for channel in cntrl.get_units(["SensUnit"])
43  )
44 
45 
47  """Representation of a Duotecno climate entity."""
48 
49  _unit: SensUnit
50  _attr_supported_features = (
51  ClimateEntityFeature.TARGET_TEMPERATURE
52  | ClimateEntityFeature.PRESET_MODE
53  | ClimateEntityFeature.TURN_OFF
54  | ClimateEntityFeature.TURN_ON
55  )
56  _attr_temperature_unit = UnitOfTemperature.CELSIUS
57  _attr_hvac_modes = list(HVACMODE_REVERSE)
58  _attr_preset_modes = list(PRESETMODES)
59  _attr_translation_key = "duotecno"
60  _enable_turn_on_off_backwards_compatibility = False
61 
62  @property
63  def current_temperature(self) -> float | None:
64  """Get the current temperature."""
65  return self._unit_unit.get_cur_temp()
66 
67  @property
68  def target_temperature(self) -> float | None:
69  """Get the target temperature."""
70  return self._unit_unit.get_target_temp()
71 
72  @property
73  def hvac_mode(self) -> HVACMode:
74  """Get the current hvac_mode."""
75  return HVACMODE[self._unit_unit.get_state()]
76 
77  @property
78  def preset_mode(self) -> str:
79  """Get the preset mode."""
80  return PRESETMODES_REVERSE[self._unit_unit.get_preset()]
81 
82  @api_call
83  async def async_set_temperature(self, **kwargs: Any) -> None:
84  """Set new target temperatures."""
85  if (temp := kwargs.get(ATTR_TEMPERATURE)) is None:
86  return
87  await self._unit_unit.set_temp(temp)
88 
89  @api_call
90  async def async_set_preset_mode(self, preset_mode: str) -> None:
91  """Set the preset mode."""
92  await self._unit_unit.set_preset(PRESETMODES[preset_mode])
93 
94  @api_call
95  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
96  """Duotecno does not support setting this, we can only display it."""
97  if hvac_mode == HVACMode.OFF:
98  await self._unit_unit.turn_off()
99  else:
100  await self._unit_unit.turn_on()
None async_set_hvac_mode(self, HVACMode hvac_mode)
Definition: climate.py:95
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:38