Skip to content

Commit

Permalink
Handle set/current tempes if they have different units #216
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xZetsubou committed May 3, 2024
1 parent 3005fc1 commit 7229afa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
28 changes: 22 additions & 6 deletions custom_components/localtuya/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]
)
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions custom_components/localtuya/core/ha_entities/climates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -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),
Expand Down

0 comments on commit 7229afa

Please sign in to comment.