Skip to content

Commit

Permalink
fix(climate): add is_on property to support all on/off state types (#498
Browse files Browse the repository at this point in the history
)
  • Loading branch information
xZetsubou authored Jan 19, 2025
1 parent 430f62d commit c6787db
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions custom_components/localtuya/climate.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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"),
Expand All @@ -99,7 +98,6 @@
HVACAction.HEATING: "opened",
HVACAction.IDLE: "closed",
}
from enum import StrEnum


class SupportedTemps(StrEnum):
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -419,15 +422,15 @@ 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)

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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c6787db

Please sign in to comment.