From d271da1b503d22418fe84f4803f367ad4745e13f Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 11 Dec 2021 21:54:57 +1100 Subject: [PATCH 1/6] Implement optional light variable to reverse color temp --- custom_components/localtuya/const.py | 1 + custom_components/localtuya/light.py | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/custom_components/localtuya/const.py b/custom_components/localtuya/const.py index bd8a5d3..f981582 100644 --- a/custom_components/localtuya/const.py +++ b/custom_components/localtuya/const.py @@ -16,6 +16,7 @@ CONF_COLOR = "color" CONF_COLOR_MODE = "color_mode" CONF_COLOR_TEMP_MIN_KELVIN = "color_temp_min_kelvin" CONF_COLOR_TEMP_MAX_KELVIN = "color_temp_max_kelvin" +CONF_COLOR_TEMP_REVERSE = "color_temp_reverse" CONF_MUSIC_MODE = "music_mode" # switch diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index 49fade8..a910997 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -27,6 +27,7 @@ from .const import ( CONF_COLOR_MODE, CONF_COLOR_TEMP_MAX_KELVIN, CONF_COLOR_TEMP_MIN_KELVIN, + CONF_COLOR_TEMP_REVERSE, CONF_MUSIC_MODE, ) @@ -35,6 +36,7 @@ _LOGGER = logging.getLogger(__name__) MIRED_TO_KELVIN_CONST = 1000000 DEFAULT_MIN_KELVIN = 2700 # MIRED 370 DEFAULT_MAX_KELVIN = 6500 # MIRED 153 +DEFAULT_COLOR_TEMP_REVERSE = False DEFAULT_LOWER_BRIGHTNESS = 29 DEFAULT_UPPER_BRIGHTNESS = 1000 @@ -117,6 +119,9 @@ def flow_schema(dps): vol.Optional(CONF_COLOR_TEMP_MAX_KELVIN, default=DEFAULT_MAX_KELVIN): vol.All( vol.Coerce(int), vol.Range(min=1500, max=8000) ), + vol.Optional( + CONF_COLOR_TEMP_REVERSE, default=False, description={"suggested_value": False} + ): bool, vol.Optional(CONF_SCENE): vol.In(dps), vol.Optional( CONF_MUSIC_MODE, default=False, description={"suggested_value": False} @@ -154,6 +159,9 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): MIRED_TO_KELVIN_CONST / 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 + ) self._hs = None self._effect = None self._effect_list = [] @@ -199,13 +207,15 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): def color_temp(self): """Return the color_temp of the light.""" if self.has_config(CONF_COLOR_TEMP) and self.is_white_mode: - return int( + color_temp_value = self._upper_color_temp - self._color_temp if self._color_temp_reverse else self._color_temp + color_temp_scaled = int( self._max_mired - ( ((self._max_mired - self._min_mired) / self._upper_color_temp) - * self._color_temp + * color_temp_value ) ) + return color_temp_scaled return None @property @@ -364,14 +374,16 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): if ATTR_COLOR_TEMP in kwargs and (features & SUPPORT_COLOR_TEMP): if brightness is None: brightness = self._brightness - color_temp = int( + + 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) + color_temp_scaled = int( self._upper_color_temp - (self._upper_color_temp / (self._max_mired - self._min_mired)) - * (int(kwargs[ATTR_COLOR_TEMP]) - self._min_mired) + * color_temp_value ) states[self._config.get(CONF_COLOR_MODE)] = MODE_WHITE states[self._config.get(CONF_BRIGHTNESS)] = brightness - states[self._config.get(CONF_COLOR_TEMP)] = color_temp + states[self._config.get(CONF_COLOR_TEMP)] = color_temp_scaled await self._device.set_dps(states) async def async_turn_off(self, **kwargs): From b79c9d4841797496ea673a7ade5e29dbdc3fe5f2 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 11 Dec 2021 22:07:42 +1100 Subject: [PATCH 2/6] update light config info --- info.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/info.md b/info.md index 3c69026..9e4c1d5 100644 --- a/info.md +++ b/info.md @@ -66,6 +66,13 @@ localtuya: - platform: light friendly_name: Device Light + brightness: 10 # Optional + brightness_lower: 0 # Optional + brightness_upper: 100 # Optional + color_temp: 11 # Optional + color_temp_reverse: false # Optional + color_temp_min_kelvin: 2700 # Optional + color_temp_max_kelvin: 6500 # Optional id: 4 - platform: sensor From 65758ba6e2de53db45ca7a7b83e3df97a690a3e4 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 11 Dec 2021 22:19:52 +1100 Subject: [PATCH 3/6] Formatting and variables --- custom_components/localtuya/light.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index a910997..8e76dca 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -36,6 +36,7 @@ _LOGGER = logging.getLogger(__name__) MIRED_TO_KELVIN_CONST = 1000000 DEFAULT_MIN_KELVIN = 2700 # MIRED 370 DEFAULT_MAX_KELVIN = 6500 # MIRED 153 + DEFAULT_COLOR_TEMP_REVERSE = False DEFAULT_LOWER_BRIGHTNESS = 29 @@ -120,7 +121,9 @@ def flow_schema(dps): vol.Coerce(int), vol.Range(min=1500, max=8000) ), vol.Optional( - CONF_COLOR_TEMP_REVERSE, default=False, description={"suggested_value": False} + CONF_COLOR_TEMP_REVERSE, + default=DEFAULT_COLOR_TEMP_REVERSE, + description={"suggested_value": DEFAULT_COLOR_TEMP_REVERSE}, ): bool, vol.Optional(CONF_SCENE): vol.In(dps), vol.Optional( @@ -207,7 +210,11 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): def color_temp(self): """Return the color_temp of the light.""" if self.has_config(CONF_COLOR_TEMP) and self.is_white_mode: - color_temp_value = self._upper_color_temp - self._color_temp if self._color_temp_reverse else self._color_temp + color_temp_value = ( + self._upper_color_temp - self._color_temp + if self._color_temp_reverse + else self._color_temp + ) color_temp_scaled = int( self._max_mired - ( @@ -374,8 +381,13 @@ 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) + + 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) + ) color_temp_scaled = int( self._upper_color_temp - (self._upper_color_temp / (self._max_mired - self._min_mired)) From f5ee4202233c5fdd53f625902b65862ae14eb11c Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 11 Dec 2021 22:22:45 +1100 Subject: [PATCH 4/6] update readme and info --- README.md | 2 +- info.md | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0030cf3..a5faa83 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ localtuya: color_mode: 21 # Optional, usually 2 or 21, default: "none" brightness: 22 # Optional, usually 3 or 22, default: "none" color_temp: 23 # Optional, usually 4 or 23, default: "none" + color_temp_reverse: false # Optional, default: false color: 24 # Optional, usually 5 (RGB_HSV) or 24 (HSV), default: "none" brightness_lower: 29 # Optional, usually 0 or 29, default: 29 brightness_upper: 1000 # Optional, usually 255 or 1000, default: 1000 @@ -79,7 +80,6 @@ localtuya: scene: 25 # Optional, usually 6 (RGB_HSV) or 25 (HSV), default: "none" music_mode: False # Optional, some use internal mic, others, phone mic. Only internal mic is supported, default: "False" - - platform: sensor friendly_name: Plug Voltage id: 20 diff --git a/info.md b/info.md index 9e4c1d5..1954f9d 100644 --- a/info.md +++ b/info.md @@ -66,14 +66,18 @@ localtuya: - platform: light friendly_name: Device Light - brightness: 10 # Optional - brightness_lower: 0 # Optional - brightness_upper: 100 # Optional - color_temp: 11 # Optional - color_temp_reverse: false # Optional - color_temp_min_kelvin: 2700 # Optional - color_temp_max_kelvin: 6500 # Optional - id: 4 + id: 4 # Usually 1 or 20 + color_mode: 21 # Optional, usually 2 or 21, default: "none" + brightness: 22 # Optional, usually 3 or 22, default: "none" + color_temp: 23 # Optional, usually 4 or 23, default: "none" + color_temp_reverse: false # Optional, default: false + color: 24 # Optional, usually 5 (RGB_HSV) or 24 (HSV), default: "none" + brightness_lower: 29 # Optional, usually 0 or 29, default: 29 + brightness_upper: 1000 # Optional, usually 255 or 1000, default: 1000 + color_temp_min_kelvin: 2700 # Optional, default: 2700 + color_temp_max_kelvin: 6500 # Optional, default: 6500 + scene: 25 # Optional, usually 6 (RGB_HSV) or 25 (HSV), default: "none" + music_mode: False # Optional, some use internal mic, others, phone mic. Only internal mic is supported, default: "False" - platform: sensor friendly_name: Plug Voltage From e45be52a9e7845c5ab4e3a460528ed829a9b07ea Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 13 Dec 2021 11:09:13 +1100 Subject: [PATCH 5/6] rename variables --- custom_components/localtuya/light.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index 8e76dca..e99414e 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -215,14 +215,13 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): if self._color_temp_reverse else self._color_temp ) - color_temp_scaled = int( + return int( self._max_mired - ( ((self._max_mired - self._min_mired) / self._upper_color_temp) * color_temp_value ) ) - return color_temp_scaled return None @property @@ -381,21 +380,20 @@ 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) ) - color_temp_scaled = int( + color_temp = int( self._upper_color_temp - (self._upper_color_temp / (self._max_mired - self._min_mired)) * color_temp_value ) states[self._config.get(CONF_COLOR_MODE)] = MODE_WHITE states[self._config.get(CONF_BRIGHTNESS)] = brightness - states[self._config.get(CONF_COLOR_TEMP)] = color_temp_scaled + states[self._config.get(CONF_COLOR_TEMP)] = color_temp await self._device.set_dps(states) async def async_turn_off(self, **kwargs): From ad0e1b186c381d7480d0d38f8b384aadabc7df8b Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 22 Dec 2021 17:34:32 +1100 Subject: [PATCH 6/6] fix missing color temp label --- custom_components/localtuya/translations/en.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index f12c4ab..5981c59 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -64,6 +64,7 @@ "brightness_lower": "Brightness Lower Value", "brightness_upper": "Brightness Upper Value", "color_temp": "Color Temperature", + "color_temp_reverse": "Color Temperature Reverse", "color": "Color", "color_mode": "Color Mode", "color_temp_min_kelvin": "Minimum Color Temperature in K", @@ -120,6 +121,7 @@ "brightness_lower": "Brightness Lower Value", "brightness_upper": "Brightness Upper Value", "color_temp": "Color Temperature", + "color_temp_reverse": "Color Temperature Reverse", "color": "Color", "color_mode": "Color Mode", "color_temp_min_kelvin": "Minimum Color Temperature in K",