Compare commits

...

5 Commits

Author SHA1 Message Date
epenet
ee310bf2c8 Fix 2025-10-15 09:58:44 +00:00
epenet
48f848e837 Invert based on initial values 2025-10-15 09:49:44 +00:00
epenet
10ad9191d5 Adjust 2025-10-15 09:49:44 +00:00
epenet
6fb5ef6268 Replace number with climate 2025-10-15 09:49:43 +00:00
epenet
384f452618 Add lower/upper temperature to Tuya thermostats 2025-10-15 09:49:43 +00:00
4 changed files with 147 additions and 29 deletions

View File

@@ -8,6 +8,8 @@ from typing import TYPE_CHECKING, Any
from tuya_sharing import CustomerDevice, Manager
from homeassistant.components.climate import (
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
SWING_BOTH,
SWING_HORIZONTAL,
SWING_OFF,
@@ -116,6 +118,8 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
_hvac_to_tuya: dict[str, str]
_set_humidity: IntegerTypeData | None = None
_set_temperature: IntegerTypeData | None = None
_set_temperature_lower: IntegerTypeData | None = None
_set_temperature_upper: IntegerTypeData | None = None
entity_description: TuyaClimateEntityDescription
_attr_name = None
@@ -198,6 +202,28 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
self._attr_min_temp = self._set_temperature.min_scaled
self._attr_target_temperature_step = self._set_temperature.step_scaled
# Check for range
if (
lower_type := self.find_dpcode(DPCode.LOWER_TEMP, dptype=DPType.INTEGER)
) and (
upper_type := self.find_dpcode(DPCode.UPPER_TEMP, dptype=DPType.INTEGER)
):
self._attr_supported_features |= (
ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
)
# Some devices have fields inverted, so we need to check the values
lower_value = self.device.status.get(lower_type.dpcode)
upper_value = self.device.status.get(upper_type.dpcode)
if (
lower_value is not None
and upper_value is not None
and lower_value > upper_value
):
lower_type, upper_type = upper_type, lower_type
self._set_temperature_lower = lower_type
self._set_temperature_upper = upper_type
# Determine HVAC modes
self._attr_hvac_modes: list[HVACMode] = []
self._hvac_to_tuya = {}
@@ -343,20 +369,49 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
def set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
if TYPE_CHECKING:
# guarded by ClimateEntityFeature.TARGET_TEMPERATURE
assert self._set_temperature is not None
if ATTR_TEMPERATURE in kwargs:
if TYPE_CHECKING:
# guarded by ClimateEntityFeature.TARGET_TEMPERATURE
assert self._set_temperature is not None
self._send_command(
[
{
"code": self._set_temperature.dpcode,
"value": round(
self._set_temperature.scale_value_back(
kwargs[ATTR_TEMPERATURE]
)
),
}
]
)
return
self._send_command(
[
{
"code": self._set_temperature.dpcode,
"value": round(
self._set_temperature.scale_value_back(kwargs[ATTR_TEMPERATURE])
),
}
]
)
if ATTR_TARGET_TEMP_LOW in kwargs and ATTR_TARGET_TEMP_HIGH in kwargs:
if TYPE_CHECKING:
# guarded by ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
assert self._set_temperature_lower is not None
assert self._set_temperature_upper is not None
self._send_command(
[
{
"code": self._set_temperature_lower.dpcode,
"value": round(
self._set_temperature_lower.scale_value_back(
kwargs[ATTR_TARGET_TEMP_LOW]
)
),
},
{
"code": self._set_temperature_upper.dpcode,
"value": round(
self._set_temperature_upper.scale_value_back(
kwargs[ATTR_TARGET_TEMP_HIGH]
)
),
},
]
)
@property
def current_temperature(self) -> float | None:
@@ -401,6 +456,30 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
return self._set_temperature.scale_value(temperature)
@property
def target_temperature_low(self) -> float | None:
"""Return the lowbound target temperature we try to reach."""
if self._set_temperature_lower is None:
return None
temperature = self.device.status.get(self._set_temperature_lower.dpcode)
if temperature is None:
return None
return self._set_temperature_lower.scale_value(temperature)
@property
def target_temperature_high(self) -> float | None:
"""Return the highbound target temperature we try to reach."""
if self._set_temperature_upper is None:
return None
temperature = self.device.status.get(self._set_temperature_upper.dpcode)
if temperature is None:
return None
return self._set_temperature_upper.scale_value(temperature)
@property
def target_humidity(self) -> int | None:
"""Return the humidity currently set to be reached."""

View File

@@ -769,6 +769,7 @@ class DPCode(StrEnum):
LIQUID_LEVEL_PERCENT = "liquid_level_percent"
LIQUID_STATE = "liquid_state"
LOCK = "lock" # Lock / Child lock
LOWER_TEMP = "lower_temp"
MACH_OPERATE = "mach_operate"
MANUAL_FEED = "manual_feed"
MASTER_MODE = "master_mode" # alarm mode

View File

@@ -264,7 +264,7 @@
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': <ClimateEntityFeature: 384>,
'supported_features': <ClimateEntityFeature: 386>,
'translation_key': None,
'unique_id': 'tuya.zgiyrxflahjowpcckw',
'unit_of_measurement': None,
@@ -281,7 +281,9 @@
]),
'max_temp': 35,
'min_temp': 7,
'supported_features': <ClimateEntityFeature: 384>,
'supported_features': <ClimateEntityFeature: 386>,
'target_temp_high': 60.0,
'target_temp_low': 58.5,
'target_temp_step': 1.0,
}),
'context': <ANY>,
@@ -976,7 +978,7 @@
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': <ClimateEntityFeature: 385>,
'supported_features': <ClimateEntityFeature: 387>,
'translation_key': None,
'unique_id': 'tuya.sb3zdertrw50bgogkw',
'unit_of_measurement': None,
@@ -994,7 +996,9 @@
]),
'max_temp': 90.0,
'min_temp': 5.0,
'supported_features': <ClimateEntityFeature: 385>,
'supported_features': <ClimateEntityFeature: 387>,
'target_temp_high': 30.0,
'target_temp_low': 5.0,
'target_temp_step': 1.0,
'temperature': 12.0,
}),
@@ -1184,7 +1188,7 @@
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': <ClimateEntityFeature: 385>,
'supported_features': <ClimateEntityFeature: 387>,
'translation_key': None,
'unique_id': 'tuya.j6mn1t4ut5end6ifkw',
'unit_of_measurement': None,
@@ -1201,7 +1205,9 @@
]),
'max_temp': 35.0,
'min_temp': 5.0,
'supported_features': <ClimateEntityFeature: 385>,
'supported_features': <ClimateEntityFeature: 387>,
'target_temp_high': 35.0,
'target_temp_low': 5.0,
'target_temp_step': 0.5,
'temperature': 22.0,
}),

View File

@@ -2,6 +2,7 @@
from __future__ import annotations
from typing import Any
from unittest.mock import patch
import pytest
@@ -11,6 +12,8 @@ from tuya_sharing import CustomerDevice, Manager
from homeassistant.components.climate import (
ATTR_FAN_MODE,
ATTR_HUMIDITY,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
ATTR_TEMPERATURE,
DOMAIN as CLIMATE_DOMAIN,
SERVICE_SET_FAN_MODE,
@@ -43,17 +46,51 @@ async def test_platform_setup_and_discovery(
@pytest.mark.parametrize(
"mock_device_code",
["kt_5wnlzekkstwcdsvm"],
("mock_device_code", "entity_id", "extra_kwargs", "commands"),
[
(
"kt_5wnlzekkstwcdsvm",
"climate.air_conditioner",
{ATTR_TEMPERATURE: 22.7},
[{"code": "temp_set", "value": 22}],
),
(
"wk_ccpwojhalfxryigz",
"climate.boiler_temperature_controller",
{ATTR_TARGET_TEMP_HIGH: 22.7, ATTR_TARGET_TEMP_LOW: 20.2},
[
# On this device, the values are inverted
{"code": "upper_temp", "value": 202},
{"code": "lower_temp", "value": 227},
],
),
(
"wk_gogb05wrtredz3bs",
"climate.smart_thermostats",
{ATTR_TEMPERATURE: 22.7},
[{"code": "temp_set", "value": 22}],
),
(
"wk_gogb05wrtredz3bs",
"climate.smart_thermostats",
{ATTR_TARGET_TEMP_HIGH: 22.7, ATTR_TARGET_TEMP_LOW: 20.2},
[
{"code": "lower_temp", "value": 20},
{"code": "upper_temp", "value": 22},
],
),
],
)
async def test_set_temperature(
hass: HomeAssistant,
mock_manager: Manager,
mock_config_entry: MockConfigEntry,
mock_device: CustomerDevice,
entity_id: str,
extra_kwargs: dict[str, Any],
commands: list[dict[str, Any]],
) -> None:
"""Test set temperature service."""
entity_id = "climate.air_conditioner"
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
state = hass.states.get(entity_id)
@@ -61,15 +98,10 @@ async def test_set_temperature(
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{
ATTR_ENTITY_ID: entity_id,
ATTR_TEMPERATURE: 22.7,
},
{ATTR_ENTITY_ID: entity_id, **extra_kwargs},
blocking=True,
)
mock_manager.send_commands.assert_called_once_with(
mock_device.id, [{"code": "temp_set", "value": 22}]
)
mock_manager.send_commands.assert_called_once_with(mock_device.id, commands)
@pytest.mark.parametrize(