From c6787dbc9a0852dda8f47ad78de6c3b24f9c7630 Mon Sep 17 00:00:00 2001 From: Bander <46300268+xZetsubou@users.noreply.github.com> Date: Sun, 19 Jan 2025 23:54:20 +0300 Subject: [PATCH] fix(climate): add is_on property to support all on/off state types (#498) --- custom_components/localtuya/climate.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/custom_components/localtuya/climate.py b/custom_components/localtuya/climate.py index 205803e34..be7f3a174 100644 --- a/custom_components/localtuya/climate.py +++ b/custom_components/localtuya/climate.py @@ -1,8 +1,7 @@ -"""Platform to locally control Tuya-based climate devices. - # PRESETS and HVAC_MODE Needs to be handle in better way. -""" +"""Platform to locally control Tuya-based climate devices.""" import asyncio +from enum import StrEnum import logging from functools import partial from .config_flow import col_to_select @@ -59,7 +58,7 @@ _LOGGER = logging.getLogger(__name__) -HVAC_OFF = {HVACMode.OFF.value: "off"} +HVAC_OFF = {HVACMode.OFF.value: "off"} # Migrate to 3 RENAME_HVAC_MODE_SETS = { # Migrate to 3 ("manual", "Manual", "hot", "m", "True"): HVACMode.HEAT.value, ("auto", "0", "p", "Program"): HVACMode.AUTO.value, @@ -73,7 +72,7 @@ ("cooling"): HVACAction.COOLING.value, ("off"): HVACAction.OFF.value, } -RENAME_PRESET_SETS = { +RENAME_PRESET_SETS = { # Migrate to 3 "Holiday": (PRESET_AWAY), "Program": (PRESET_HOME), "Manual": (PRESET_NONE, "manual"), @@ -99,7 +98,6 @@ HVACAction.HEATING: "opened", HVACAction.IDLE: "closed", } -from enum import StrEnum class SupportedTemps(StrEnum): @@ -258,6 +256,11 @@ def __init__( set_temp_unit = config_unit(config_temp_unit) self._temperature_unit = set_temp_unit + @property + def _is_on(self): + """Return if the device is on.""" + return self._state and self._state != self._state_off + @property def supported_features(self): """Flag supported features.""" @@ -299,7 +302,7 @@ def max_temp(self): @property def hvac_mode(self): """Return current operation ie. heat, cool, idle.""" - if not self._state: + if not self._is_on: return HVACMode.OFF if not self._hvac_mode_dp: return HVACMode.HEAT @@ -321,7 +324,7 @@ def hvac_modes(self): @property def hvac_action(self): """Return the current running hvac operation if supported.""" - if not self._state: + if not self._is_on: return HVACAction.OFF if not self._conf_hvac_action_dp: @@ -419,7 +422,7 @@ async def async_set_temperature(self, **kwargs): async def async_set_fan_mode(self, fan_mode): """Set new target fan mode.""" - if not self._state: + if not self._is_on: await self._device.set_dp(self._state_on, self._dp_id) await self._device.set_dp(fan_mode, self._fan_speed_dp) @@ -427,7 +430,7 @@ async def async_set_fan_mode(self, fan_mode): async def async_set_hvac_mode(self, hvac_mode: HVACMode): """Set new target operation mode.""" new_states = {} - if not self._state: + if not self._is_on: new_states[self._dp_id] = self._state_on elif hvac_mode == HVACMode.OFF and HVACMode.OFF not in self._hvac_mode_set: new_states[self._dp_id] = self._state_off @@ -501,7 +504,7 @@ def status_updated(self): self._preset_mode = PRESET_NONE # If device is off there is no needs to check the states. - if not self._state: + if not self._is_on: return # Update the HVAC Mode