Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Commit

Permalink
Climate fix (#61)
Browse files Browse the repository at this point in the history
- added list of allowed temperature step to use in HA
- fixed set_temperature to manage digits and choose update mode
  • Loading branch information
ollo69 authored Jan 29, 2021
1 parent aacd734 commit 302ec3e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
42 changes: 30 additions & 12 deletions tuyaha/devices/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
UNIT_CELSIUS = "CELSIUS"
UNIT_FAHRENHEIT = "FAHRENHEIT"

STEP_WHOLE = 1
STEP_HALVES = 0.5
STEP_TENTHS = 0.1

TEMP_STEPS = {
STEP_WHOLE: "Whole",
STEP_HALVES: "Halves",
STEP_TENTHS: "Tenths",
}


class TuyaClimate(TuyaDevice):

Expand Down Expand Up @@ -56,9 +66,9 @@ def temp_divider(self, divider):
raise ValueError("Temperature divider must be a positive value")
# this check is to avoid that divider is reset from
# calculated value when is set to 0
if (self._divider_set and divider == 0) or divider > 0:
self._divider = divider
self._divider_set = divider > 0
if (self._divider_set and divider < 1) or divider >= 1:
self._divider = int(divider)
self._divider_set = divider >= 1

@property
def curr_temp_divider(self):
Expand All @@ -70,7 +80,7 @@ def curr_temp_divider(self, divider):
If not defined standard temperature divider is used"""
if divider < 0:
raise ValueError("Current temperature divider must be a positive value")
self._ct_divider = divider
self._ct_divider = int(divider)

def has_decimal(self):
"""Return if temperature values support decimal"""
Expand Down Expand Up @@ -110,8 +120,11 @@ def target_temperature(self):

def target_temperature_step(self):
if self.has_decimal():
return 0.5
return 1.0
return STEP_HALVES
return STEP_WHOLE

def supported_temperature_steps(self):
return TEMP_STEPS

def current_fan_mode(self):
"""Return the fan setting."""
Expand Down Expand Up @@ -150,17 +163,22 @@ def min_humidity(self):
def max_humidity(self):
pass

def set_temperature(self, temperature):
def set_temperature(self, temperature, use_divider=True):
"""Set new target temperature."""

# the value used to set temperature is scaled based on the configured divider
divider = self._divider or 1

if not self.has_decimal():
temp_val = round(float(temperature))
set_val = temp_val * divider
input_val = float(temperature)
scaled_val = input_val * divider
digits1 = None if input_val.is_integer() else 1
digits2 = None if scaled_val.is_integer() else 1

set_val = round(scaled_val, digits2)
if use_divider:
temp_val = round(input_val, digits1)
else:
temp_val = set_val = round(float(temperature) * divider)
temp_val = set_val

if self._control_device("temperatureSet", {"value": temp_val}):
self._update_data("temperature", set_val)

Expand Down
3 changes: 2 additions & 1 deletion tuyaha/tuyaapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,6 @@ class TuyaServerException(Exception):
class TuyaFrequentlyInvokeException(Exception):
pass


class TuyaAPIRateLimitException(Exception):
pass
pass

0 comments on commit 302ec3e

Please sign in to comment.