Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Support for Tuya Climate."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any
7 
8 from tuya_sharing import CustomerDevice, Manager
9 
11  SWING_BOTH,
12  SWING_HORIZONTAL,
13  SWING_OFF,
14  SWING_ON,
15  SWING_VERTICAL,
16  ClimateEntity,
17  ClimateEntityDescription,
18  ClimateEntityFeature,
19  HVACMode,
20 )
21 from homeassistant.const import UnitOfTemperature
22 from homeassistant.core import HomeAssistant, callback
23 from homeassistant.helpers.dispatcher import async_dispatcher_connect
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 
26 from . import TuyaConfigEntry
27 from .const import TUYA_DISCOVERY_NEW, DPCode, DPType
28 from .entity import IntegerTypeData, TuyaEntity
29 
30 TUYA_HVAC_TO_HA = {
31  "auto": HVACMode.HEAT_COOL,
32  "cold": HVACMode.COOL,
33  "freeze": HVACMode.COOL,
34  "heat": HVACMode.HEAT,
35  "hot": HVACMode.HEAT,
36  "manual": HVACMode.HEAT_COOL,
37  "wet": HVACMode.DRY,
38  "wind": HVACMode.FAN_ONLY,
39 }
40 
41 
42 @dataclass(frozen=True, kw_only=True)
44  """Describe an Tuya climate entity."""
45 
46  switch_only_hvac_mode: HVACMode
47 
48 
49 CLIMATE_DESCRIPTIONS: dict[str, TuyaClimateEntityDescription] = {
50  # Air conditioner
51  # https://developer.tuya.com/en/docs/iot/categorykt?id=Kaiuz0z71ov2n
53  key="kt",
54  switch_only_hvac_mode=HVACMode.COOL,
55  ),
56  # Heater
57  # https://developer.tuya.com/en/docs/iot/f?id=K9gf46epy4j82
59  key="qn",
60  switch_only_hvac_mode=HVACMode.HEAT,
61  ),
62  # Heater
63  # https://developer.tuya.com/en/docs/iot/categoryrs?id=Kaiuz0nfferyx
65  key="rs",
66  switch_only_hvac_mode=HVACMode.HEAT,
67  ),
68  # Thermostat
69  # https://developer.tuya.com/en/docs/iot/f?id=K9gf45ld5l0t9
71  key="wk",
72  switch_only_hvac_mode=HVACMode.HEAT_COOL,
73  ),
74  # Thermostatic Radiator Valve
75  # Not documented
77  key="wkf",
78  switch_only_hvac_mode=HVACMode.HEAT,
79  ),
80 }
81 
82 
84  hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
85 ) -> None:
86  """Set up Tuya climate dynamically through Tuya discovery."""
87  hass_data = entry.runtime_data
88 
89  @callback
90  def async_discover_device(device_ids: list[str]) -> None:
91  """Discover and add a discovered Tuya climate."""
92  entities: list[TuyaClimateEntity] = []
93  for device_id in device_ids:
94  device = hass_data.manager.device_map[device_id]
95  if device and device.category in CLIMATE_DESCRIPTIONS:
96  entities.append(
98  device,
99  hass_data.manager,
100  CLIMATE_DESCRIPTIONS[device.category],
101  hass.config.units.temperature_unit,
102  )
103  )
104  async_add_entities(entities)
105 
106  async_discover_device([*hass_data.manager.device_map])
107 
108  entry.async_on_unload(
109  async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
110  )
111 
112 
114  """Tuya Climate Device."""
115 
116  _current_humidity: IntegerTypeData | None = None
117  _current_temperature: IntegerTypeData | None = None
118  _hvac_to_tuya: dict[str, str]
119  _set_humidity: IntegerTypeData | None = None
120  _set_temperature: IntegerTypeData | None = None
121  entity_description: TuyaClimateEntityDescription
122  _attr_name = None
123  _enable_turn_on_off_backwards_compatibility = False
124 
125  def __init__(
126  self,
127  device: CustomerDevice,
128  device_manager: Manager,
129  description: TuyaClimateEntityDescription,
130  system_temperature_unit: UnitOfTemperature,
131  ) -> None:
132  """Determine which values to use."""
133  self._attr_target_temperature_step_attr_target_temperature_step = 1.0
134  self.entity_descriptionentity_description = description
135 
136  super().__init__(device, device_manager)
137 
138  # If both temperature values for celsius and fahrenheit are present,
139  # use whatever the device is set to, with a fallback to celsius.
140  prefered_temperature_unit = None
141  if all(
142  dpcode in device.status
143  for dpcode in (DPCode.TEMP_CURRENT, DPCode.TEMP_CURRENT_F)
144  ) or all(
145  dpcode in device.status for dpcode in (DPCode.TEMP_SET, DPCode.TEMP_SET_F)
146  ):
147  prefered_temperature_unit = UnitOfTemperature.CELSIUS
148  if any(
149  "f" in device.status[dpcode].lower()
150  for dpcode in (DPCode.C_F, DPCode.TEMP_UNIT_CONVERT)
151  if isinstance(device.status.get(dpcode), str)
152  ):
153  prefered_temperature_unit = UnitOfTemperature.FAHRENHEIT
154 
155  # Default to System Temperature Unit
156  self._attr_temperature_unit_attr_temperature_unit = system_temperature_unit
157 
158  # Figure out current temperature, use preferred unit or what is available
159  celsius_type = self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(
160  (DPCode.TEMP_CURRENT, DPCode.UPPER_TEMP), dptype=DPType.INTEGER
161  )
162  fahrenheit_type = self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(
163  (DPCode.TEMP_CURRENT_F, DPCode.UPPER_TEMP_F), dptype=DPType.INTEGER
164  )
165  if fahrenheit_type and (
166  prefered_temperature_unit == UnitOfTemperature.FAHRENHEIT
167  or (
168  prefered_temperature_unit == UnitOfTemperature.CELSIUS
169  and not celsius_type
170  )
171  ):
172  self._attr_temperature_unit_attr_temperature_unit = UnitOfTemperature.FAHRENHEIT
173  self._current_temperature_current_temperature = fahrenheit_type
174  elif celsius_type:
175  self._attr_temperature_unit_attr_temperature_unit = UnitOfTemperature.CELSIUS
176  self._current_temperature_current_temperature = celsius_type
177 
178  # Figure out setting temperature, use preferred unit or what is available
179  celsius_type = self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(
180  DPCode.TEMP_SET, dptype=DPType.INTEGER, prefer_function=True
181  )
182  fahrenheit_type = self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(
183  DPCode.TEMP_SET_F, dptype=DPType.INTEGER, prefer_function=True
184  )
185  if fahrenheit_type and (
186  prefered_temperature_unit == UnitOfTemperature.FAHRENHEIT
187  or (
188  prefered_temperature_unit == UnitOfTemperature.CELSIUS
189  and not celsius_type
190  )
191  ):
192  self._set_temperature_set_temperature = fahrenheit_type
193  elif celsius_type:
194  self._set_temperature_set_temperature = celsius_type
195 
196  # Get integer type data for the dpcode to set temperature, use
197  # it to define min, max & step temperatures
198  if self._set_temperature_set_temperature:
199  self._attr_supported_features |= ClimateEntityFeature.TARGET_TEMPERATURE
200  self._attr_max_temp_attr_max_temp = self._set_temperature_set_temperature.max_scaled
201  self._attr_min_temp_attr_min_temp = self._set_temperature_set_temperature.min_scaled
202  self._attr_target_temperature_step_attr_target_temperature_step = self._set_temperature_set_temperature.step_scaled
203 
204  # Determine HVAC modes
205  self._attr_hvac_modes_attr_hvac_modes: list[HVACMode] = []
206  self._hvac_to_tuya_hvac_to_tuya = {}
207  if enum_type := self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(
208  DPCode.MODE, dptype=DPType.ENUM, prefer_function=True
209  ):
210  self._attr_hvac_modes_attr_hvac_modes = [HVACMode.OFF]
211  unknown_hvac_modes: list[str] = []
212  for tuya_mode in enum_type.range:
213  if tuya_mode in TUYA_HVAC_TO_HA:
214  ha_mode = TUYA_HVAC_TO_HA[tuya_mode]
215  self._hvac_to_tuya_hvac_to_tuya[ha_mode] = tuya_mode
216  self._attr_hvac_modes_attr_hvac_modes.append(ha_mode)
217  else:
218  unknown_hvac_modes.append(tuya_mode)
219 
220  if unknown_hvac_modes: # Tuya modes are presets instead of hvac_modes
221  self._attr_hvac_modes_attr_hvac_modes.append(description.switch_only_hvac_mode)
222  self._attr_preset_modes_attr_preset_modes = unknown_hvac_modes
223  self._attr_supported_features |= ClimateEntityFeature.PRESET_MODE
224  elif self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(DPCode.SWITCH, prefer_function=True):
225  self._attr_hvac_modes_attr_hvac_modes = [
226  HVACMode.OFF,
227  description.switch_only_hvac_mode,
228  ]
229 
230  # Determine dpcode to use for setting the humidity
231  if int_type := self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(
232  DPCode.HUMIDITY_SET, dptype=DPType.INTEGER, prefer_function=True
233  ):
234  self._attr_supported_features |= ClimateEntityFeature.TARGET_HUMIDITY
235  self._set_humidity_set_humidity = int_type
236  self._attr_min_humidity_attr_min_humidity = int(int_type.min_scaled)
237  self._attr_max_humidity_attr_max_humidity = int(int_type.max_scaled)
238 
239  # Determine dpcode to use for getting the current humidity
240  self._current_humidity_current_humidity = self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(
241  DPCode.HUMIDITY_CURRENT, dptype=DPType.INTEGER
242  )
243 
244  # Determine fan modes
245  if enum_type := self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(
246  (DPCode.FAN_SPEED_ENUM, DPCode.WINDSPEED),
247  dptype=DPType.ENUM,
248  prefer_function=True,
249  ):
250  self._attr_supported_features |= ClimateEntityFeature.FAN_MODE
251  self._attr_fan_modes_attr_fan_modes = enum_type.range
252 
253  # Determine swing modes
255  (
256  DPCode.SHAKE,
257  DPCode.SWING,
258  DPCode.SWITCH_HORIZONTAL,
259  DPCode.SWITCH_VERTICAL,
260  ),
261  prefer_function=True,
262  ):
263  self._attr_supported_features |= ClimateEntityFeature.SWING_MODE
264  self._attr_swing_modes_attr_swing_modes = [SWING_OFF]
265  if self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode((DPCode.SHAKE, DPCode.SWING), prefer_function=True):
266  self._attr_swing_modes_attr_swing_modes.append(SWING_ON)
267 
268  if self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(DPCode.SWITCH_HORIZONTAL, prefer_function=True):
269  self._attr_swing_modes_attr_swing_modes.append(SWING_HORIZONTAL)
270 
271  if self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(DPCode.SWITCH_VERTICAL, prefer_function=True):
272  self._attr_swing_modes_attr_swing_modes.append(SWING_VERTICAL)
273 
274  if DPCode.SWITCH in self.devicedevice.function:
275  self._attr_supported_features |= (
276  ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON
277  )
278 
279  async def async_added_to_hass(self) -> None:
280  """Call when entity is added to hass."""
281  await super().async_added_to_hass()
282 
283  def set_hvac_mode(self, hvac_mode: HVACMode) -> None:
284  """Set new target hvac mode."""
285  commands = [{"code": DPCode.SWITCH, "value": hvac_mode != HVACMode.OFF}]
286  if hvac_mode in self._hvac_to_tuya_hvac_to_tuya:
287  commands.append(
288  {"code": DPCode.MODE, "value": self._hvac_to_tuya_hvac_to_tuya[hvac_mode]}
289  )
290  self._send_command_send_command(commands)
291 
292  def set_preset_mode(self, preset_mode):
293  """Set new target preset mode."""
294  commands = [{"code": DPCode.MODE, "value": preset_mode}]
295  self._send_command_send_command(commands)
296 
297  def set_fan_mode(self, fan_mode: str) -> None:
298  """Set new target fan mode."""
299  self._send_command_send_command([{"code": DPCode.FAN_SPEED_ENUM, "value": fan_mode}])
300 
301  def set_humidity(self, humidity: int) -> None:
302  """Set new target humidity."""
303  if self._set_humidity_set_humidity is None:
304  raise RuntimeError(
305  "Cannot set humidity, device doesn't provide methods to set it"
306  )
307 
308  self._send_command_send_command(
309  [
310  {
311  "code": self._set_humidity_set_humidity.dpcode,
312  "value": self._set_humidity_set_humidity.scale_value_back(humidity),
313  }
314  ]
315  )
316 
317  def set_swing_mode(self, swing_mode: str) -> None:
318  """Set new target swing operation."""
319  # The API accepts these all at once and will ignore the codes
320  # that don't apply to the device being controlled.
321  self._send_command_send_command(
322  [
323  {
324  "code": DPCode.SHAKE,
325  "value": swing_mode == SWING_ON,
326  },
327  {
328  "code": DPCode.SWING,
329  "value": swing_mode == SWING_ON,
330  },
331  {
332  "code": DPCode.SWITCH_VERTICAL,
333  "value": swing_mode in (SWING_BOTH, SWING_VERTICAL),
334  },
335  {
336  "code": DPCode.SWITCH_HORIZONTAL,
337  "value": swing_mode in (SWING_BOTH, SWING_HORIZONTAL),
338  },
339  ]
340  )
341 
342  def set_temperature(self, **kwargs: Any) -> None:
343  """Set new target temperature."""
344  if self._set_temperature_set_temperature is None:
345  raise RuntimeError(
346  "Cannot set target temperature, device doesn't provide methods to"
347  " set it"
348  )
349 
350  self._send_command_send_command(
351  [
352  {
353  "code": self._set_temperature_set_temperature.dpcode,
354  "value": round(
355  self._set_temperature_set_temperature.scale_value_back(kwargs["temperature"])
356  ),
357  }
358  ]
359  )
360 
361  @property
362  def current_temperature(self) -> float | None:
363  """Return the current temperature."""
364  if self._current_temperature_current_temperature is None:
365  return None
366 
367  temperature = self.devicedevice.status.get(self._current_temperature_current_temperature.dpcode)
368  if temperature is None:
369  return None
370 
371  if self._current_temperature_current_temperature.scale == 0 and self._current_temperature_current_temperature.step != 1:
372  # The current temperature can have a scale of 0 or 1 and is used for
373  # rounding, Home Assistant doesn't need to round but we will always
374  # need to divide the value by 10^1 in case of 0 as scale.
375  # https://developer.tuya.com/en/docs/iot/shift-temperature-scale-follow-the-setting-of-app-account-center?id=Ka9qo7so58efq#title-7-Round%20values
376  temperature = temperature / 10
377 
378  return self._current_temperature_current_temperature.scale_value(temperature)
379 
380  @property
381  def current_humidity(self) -> int | None:
382  """Return the current humidity."""
383  if self._current_humidity_current_humidity is None:
384  return None
385 
386  humidity = self.devicedevice.status.get(self._current_humidity_current_humidity.dpcode)
387  if humidity is None:
388  return None
389 
390  return round(self._current_humidity_current_humidity.scale_value(humidity))
391 
392  @property
393  def target_temperature(self) -> float | None:
394  """Return the temperature currently set to be reached."""
395  if self._set_temperature_set_temperature is None:
396  return None
397 
398  temperature = self.devicedevice.status.get(self._set_temperature_set_temperature.dpcode)
399  if temperature is None:
400  return None
401 
402  return self._set_temperature_set_temperature.scale_value(temperature)
403 
404  @property
405  def target_humidity(self) -> int | None:
406  """Return the humidity currently set to be reached."""
407  if self._set_humidity_set_humidity is None:
408  return None
409 
410  humidity = self.devicedevice.status.get(self._set_humidity_set_humidity.dpcode)
411  if humidity is None:
412  return None
413 
414  return round(self._set_humidity_set_humidity.scale_value(humidity))
415 
416  @property
417  def hvac_mode(self) -> HVACMode:
418  """Return hvac mode."""
419  # If the switch off, hvac mode is off as well. Unless the switch
420  # the switch is on or doesn't exists of course...
421  if not self.devicedevice.status.get(DPCode.SWITCH, True):
422  return HVACMode.OFF
423 
424  if DPCode.MODE not in self.devicedevice.function:
425  if self.devicedevice.status.get(DPCode.SWITCH, False):
426  return self.entity_descriptionentity_description.switch_only_hvac_mode
427  return HVACMode.OFF
428 
429  if (
430  mode := self.devicedevice.status.get(DPCode.MODE)
431  ) is not None and mode in TUYA_HVAC_TO_HA:
432  return TUYA_HVAC_TO_HA[mode]
433 
434  # If the switch is on, and the mode does not match any hvac mode.
435  if self.devicedevice.status.get(DPCode.SWITCH, False):
436  return self.entity_descriptionentity_description.switch_only_hvac_mode
437 
438  return HVACMode.OFF
439 
440  @property
441  def preset_mode(self) -> str | None:
442  """Return preset mode."""
443  if DPCode.MODE not in self.devicedevice.function:
444  return None
445 
446  mode = self.devicedevice.status.get(DPCode.MODE)
447  if mode in TUYA_HVAC_TO_HA:
448  return None
449 
450  return mode
451 
452  @property
453  def fan_mode(self) -> str | None:
454  """Return fan mode."""
455  return self.devicedevice.status.get(DPCode.FAN_SPEED_ENUM)
456 
457  @property
458  def swing_mode(self) -> str:
459  """Return swing mode."""
460  if any(
461  self.devicedevice.status.get(dpcode) for dpcode in (DPCode.SHAKE, DPCode.SWING)
462  ):
463  return SWING_ON
464 
465  horizontal = self.devicedevice.status.get(DPCode.SWITCH_HORIZONTAL)
466  vertical = self.devicedevice.status.get(DPCode.SWITCH_VERTICAL)
467  if horizontal and vertical:
468  return SWING_BOTH
469  if horizontal:
470  return SWING_HORIZONTAL
471  if vertical:
472  return SWING_VERTICAL
473 
474  return SWING_OFF
475 
476  def turn_on(self) -> None:
477  """Turn the device on, retaining current HVAC (if supported)."""
478  self._send_command_send_command([{"code": DPCode.SWITCH, "value": True}])
479 
480  def turn_off(self) -> None:
481  """Turn the device on, retaining current HVAC (if supported)."""
482  self._send_command_send_command([{"code": DPCode.SWITCH, "value": False}])
None set_hvac_mode(self, HVACMode hvac_mode)
Definition: climate.py:283
None __init__(self, CustomerDevice device, Manager device_manager, TuyaClimateEntityDescription description, UnitOfTemperature system_temperature_unit)
Definition: climate.py:131
None _send_command(self, list[dict[str, Any]] commands)
Definition: entity.py:295
DPCode|EnumTypeData|IntegerTypeData|None find_dpcode(self, str|DPCode|tuple[DPCode,...]|None dpcodes, *bool prefer_function=False, DPType|None dptype=None)
Definition: entity.py:206
IntegerTypeData|None find_dpcode(self, str|DPCode|tuple[DPCode,...]|None dpcodes, *bool prefer_function=False, Literal[DPType.INTEGER] dptype)
Definition: entity.py:190
DPCode|None find_dpcode(self, str|DPCode|tuple[DPCode,...]|None dpcodes, *bool prefer_function=False)
Definition: entity.py:198
EnumTypeData|None find_dpcode(self, str|DPCode|tuple[DPCode,...]|None dpcodes, *bool prefer_function=False, Literal[DPType.ENUM] dptype)
Definition: entity.py:181
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
Definition: discovery.py:78
None async_setup_entry(HomeAssistant hass, TuyaConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:85
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103