From 7229afa5c7a8e61e85359b011ccf5a8ebb787c25 Mon Sep 17 00:00:00 2001 From: xZetsubou Date: Fri, 3 May 2024 03:14:01 +0300 Subject: [PATCH] Handle set/current tempes if they have different units #216 * If set temp is the one who have fahrenheit then convert min/max and handle set temp service. * Prefer set_temp over set_temp_f --- custom_components/localtuya/climate.py | 28 +++++++++++++++---- .../localtuya/core/ha_entities/climates.py | 10 +++---- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/custom_components/localtuya/climate.py b/custom_components/localtuya/climate.py index 625a15f9..3438b8dd 100644 --- a/custom_components/localtuya/climate.py +++ b/custom_components/localtuya/climate.py @@ -154,7 +154,7 @@ def convert_temperature(num_1, num_2) -> tuple[float, float]: if None in (num_1, num_2): return num_1, num_2 - def prec_diff(value1, value2): + def perc_diff(value1, value2): """Return the percentage difference between two values""" max_value, min_value = max(value1, value2), min(value1, value2) try: @@ -163,13 +163,14 @@ def prec_diff(value1, value2): return 0 # Check if one value is in Celsius and the other is in Fahrenheit - if prec_diff(num_1, num_2) > 100: + if perc_diff(num_1, num_2) > 110: fahrenheit = max(num_1, num_2) to_celsius = (fahrenheit - 32) * 5 / 9 if fahrenheit == num_1: num_1 = to_celsius elif fahrenheit == num_2: num_2 = to_celsius + return num_1, num_2 @@ -189,6 +190,7 @@ def __init__( super().__init__(device, config_entry, switchid, _LOGGER, **kwargs) self._state = None self._target_temperature = None + self._target_temp_forced_to_celsius = False self._current_temperature = None self._hvac_mode = None self._preset_mode = None @@ -272,13 +274,19 @@ def temperature_unit(self): def min_temp(self): """Return the minimum temperature.""" # DEFAULT_MIN_TEMP is in C - return self._config.get(CONF_MIN_TEMP, DEFAULT_MIN_TEMP) + min_temp = self._config.get(CONF_MIN_TEMP, DEFAULT_MIN_TEMP) + if self._target_temp_forced_to_celsius: + min_temp = round((min_temp - 32) * 5 / 9) + return min_temp @property def max_temp(self): """Return the maximum temperature.""" # DEFAULT_MAX_TEMP is in C - return self._config.get(CONF_MAX_TEMP, DEFAULT_MAX_TEMP) + max_temp = self._config.get(CONF_MAX_TEMP, DEFAULT_MAX_TEMP) + if self._target_temp_forced_to_celsius: + max_temp = round((max_temp - 32) * 5 / 9) + return max_temp @property def hvac_mode(self): @@ -387,6 +395,10 @@ async def async_set_temperature(self, **kwargs): """Set new target temperature.""" if ATTR_TEMPERATURE in kwargs and self.has_config(CONF_TARGET_TEMPERATURE_DP): temperature = round(kwargs[ATTR_TEMPERATURE] / self._precision_target) + if self._target_temp_forced_to_celsius: + # Revert temperture to Fahrenheit it was forced to celsius + temperature = round((temperature * 1.8) + 32) + await self._device.set_dp( temperature, self._config[CONF_TARGET_TEMPERATURE_DP] ) @@ -439,11 +451,15 @@ def status_updated(self): self.dp_value(CONF_CURRENT_TEMPERATURE_DP) * self._precision ) - # Force Current temperature and Target temperature - self._target_temperature, self._current_temperature = convert_temperature( + # Force the Current temperature and Target temperature to matching the unit. + target_temp, self._current_temperature = convert_temperature( self._target_temperature, self._current_temperature ) + # if target temperature converted to celsius, then convert all related values to set temperature. + self._target_temp_forced_to_celsius = target_temp != self.target_temperature + self.target_temperature = target_temp + # Update preset states if self._has_presets: if self.dp_value(CONF_ECO_DP) == self._eco_value: diff --git a/custom_components/localtuya/core/ha_entities/climates.py b/custom_components/localtuya/core/ha_entities/climates.py index 35e40df9..db3d30cc 100644 --- a/custom_components/localtuya/core/ha_entities/climates.py +++ b/custom_components/localtuya/core/ha_entities/climates.py @@ -117,7 +117,7 @@ def localtuya_climate( "kt": ( LocalTuyaEntity( id=DPCode.SWITCH, - target_temperature_dp=(DPCode.TEMP_SET_F, DPCode.TEMP_SET), + target_temperature_dp=(DPCode.TEMP_SET, DPCode.TEMP_SET_F), current_temperature_dp=DPCode.TEMP_CURRENT, hvac_mode_dp=DPCode.MODE, hvac_action_dp=(DPCode.WORK_MODE, DPCode.WORK_STATUS, DPCode.WORK_STATE), @@ -147,7 +147,7 @@ def localtuya_climate( "qn": ( LocalTuyaEntity( id=DPCode.SWITCH, - target_temperature_dp=(DPCode.TEMP_SET_F, DPCode.TEMP_SET), + target_temperature_dp=(DPCode.TEMP_SET, DPCode.TEMP_SET_F), current_temperature_dp=(DPCode.TEMP_CURRENT, DPCode.TEMP_CURRENT_F), hvac_mode_dp=DPCode.SWITCH, hvac_action_dp=(DPCode.WORK_STATE, DPCode.WORK_MODE, DPCode.WORK_STATUS), @@ -174,7 +174,7 @@ def localtuya_climate( "rs": ( LocalTuyaEntity( id=DPCode.SWITCH, - target_temperature_dp=(DPCode.TEMP_SET_F, DPCode.TEMP_SET), + target_temperature_dp=(DPCode.TEMP_SET, DPCode.TEMP_SET_F), current_temperature_dp=(DPCode.TEMP_CURRENT, DPCode.TEMP_CURRENT_F), hvac_action_dp=(DPCode.WORK_STATE, DPCode.WORK_MODE, DPCode.WORK_STATUS), preset_dp=DPCode.MODE, @@ -201,7 +201,7 @@ def localtuya_climate( "wk": ( LocalTuyaEntity( id=(DPCode.SWITCH, DPCode.MODE), - target_temperature_dp=(DPCode.TEMP_SET_F, DPCode.TEMP_SET), + target_temperature_dp=(DPCode.TEMP_SET, DPCode.TEMP_SET_F), current_temperature_dp=(DPCode.TEMP_CURRENT_F, DPCode.TEMP_CURRENT), hvac_mode_dp=(DPCode.SWITCH, DPCode.MODE), hvac_action_dp=(DPCode.WORK_STATE, DPCode.WORK_MODE, DPCode.WORK_STATUS), @@ -225,7 +225,7 @@ def localtuya_climate( "wkf": ( LocalTuyaEntity( id=(DPCode.SWITCH, DPCode.MODE), - target_temperature_dp=(DPCode.TEMP_SET_F, DPCode.TEMP_SET), + target_temperature_dp=(DPCode.TEMP_SET, DPCode.TEMP_SET_F), current_temperature_dp=(DPCode.TEMP_CURRENT_F, DPCode.TEMP_CURRENT), hvac_mode_dp=DPCode.MODE, hvac_action_dp=(DPCode.WORK_STATE, DPCode.WORK_MODE, DPCode.WORK_STATUS),