From 35ddf1564de37fe4ddea970c4925c49f50d93654 Mon Sep 17 00:00:00 2001 From: gpaesano <97922012+gpaesano@users.noreply.github.com> Date: Sat, 29 Jan 2022 10:33:58 +0100 Subject: [PATCH 1/3] Update climate.py There is a typo in target precision which prevents corrct handling of temperature --- custom_components/localtuya/climate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/localtuya/climate.py b/custom_components/localtuya/climate.py index 7b03f31..9d9358f 100644 --- a/custom_components/localtuya/climate.py +++ b/custom_components/localtuya/climate.py @@ -196,7 +196,7 @@ class LocaltuyaClimate(LocalTuyaEntity, ClimateEntity): return self._precision @property - def target_recision(self): + def target_precision(self): """Return the precision of the target.""" return self._target_precision From 2a277a0c26497085a310306805df8ce7fb60a4d3 Mon Sep 17 00:00:00 2001 From: "Peter A. Bigot" Date: Sun, 31 Oct 2021 13:53:34 -0700 Subject: [PATCH 2/3] Fix kelvin-to-mired conversion Use the core utility rather than reimplementing it. Signed-off-by: Peter A. Bigot --- custom_components/localtuya/light.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index e99414e..5a33192 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -33,7 +33,6 @@ from .const import ( _LOGGER = logging.getLogger(__name__) -MIRED_TO_KELVIN_CONST = 1000000 DEFAULT_MIN_KELVIN = 2700 # MIRED 370 DEFAULT_MAX_KELVIN = 6500 # MIRED 153 @@ -154,13 +153,11 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): CONF_BRIGHTNESS_UPPER, DEFAULT_UPPER_BRIGHTNESS ) self._upper_color_temp = self._upper_brightness - self._max_mired = round( - MIRED_TO_KELVIN_CONST - / self._config.get(CONF_COLOR_TEMP_MIN_KELVIN, DEFAULT_MIN_KELVIN) + self._max_mired = color_util.color_temperature_kelvin_to_mired( + self._config.get(CONF_COLOR_TEMP_MIN_KELVIN, DEFAULT_MIN_KELVIN) ) - self._min_mired = round( - MIRED_TO_KELVIN_CONST - / self._config.get(CONF_COLOR_TEMP_MAX_KELVIN, DEFAULT_MAX_KELVIN) + self._min_mired = color_util.color_temperature_kelvin_to_mired( + self._config.get(CONF_COLOR_TEMP_MAX_KELVIN, DEFAULT_MAX_KELVIN) ) self._color_temp_reverse = self._config.get( CONF_COLOR_TEMP_REVERSE, DEFAULT_COLOR_TEMP_REVERSE From 8a1c6e03806fbcf410e896b323e76436f581b439 Mon Sep 17 00:00:00 2001 From: "Peter A. Bigot" Date: Sun, 31 Oct 2021 13:59:53 -0700 Subject: [PATCH 3/3] Fix out-of-range color temperatures If light.turn_on is invoked with `brightness: 128, color_temp: 500` for a bulb that supports mired range 154..370 in value range 1..255 the calculated color temperature will be negative. When the bulb receives the command it may turn on, but will fail processing the color_temp operation and will not send a response. At this point `light.toggle` will have no effect because Home Assistant is unaware that the bulb is on. Fix the state by clamping the sent color temperature to the allowed range. Signed-off-by: Peter A. Bigot --- custom_components/localtuya/light.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index 5a33192..7c74e49 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -377,16 +377,17 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): if ATTR_COLOR_TEMP in kwargs and (features & SUPPORT_COLOR_TEMP): if brightness is None: brightness = self._brightness - color_temp_value = ( - (self._max_mired - self._min_mired) - - (int(kwargs[ATTR_COLOR_TEMP]) - self._min_mired) - if self._color_temp_reverse - else (int(kwargs[ATTR_COLOR_TEMP]) - self._min_mired) - ) + mired = int(kwargs[ATTR_COLOR_TEMP]) + if self._color_temp_reverse: + mired = self._max_mired - (mired - self._min_mired) + if mired < self._min_mired: + mired = self._min_mired + elif mired > self._max_mired: + mired = self._max_mired color_temp = int( self._upper_color_temp - (self._upper_color_temp / (self._max_mired - self._min_mired)) - * color_temp_value + * (mired - self._min_mired) ) states[self._config.get(CONF_COLOR_MODE)] = MODE_WHITE states[self._config.get(CONF_BRIGHTNESS)] = brightness