diff --git a/README.md b/README.md index d837161..e9087b1 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,16 @@ localtuya: - platform: light friendly_name: Device Light - 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: 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 + - platform: sensor friendly_name: Plug Voltage diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index 4f76843..d628e57 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -182,6 +182,18 @@ class TuyaDevice(pytuya.TuyaListener): "Not connected to device %s", self._config_entry[CONF_FRIENDLY_NAME] ) + async def set_dps(self, states): + """Change value of a DPs of the Tuya device.""" + if self._interface is not None: + try: + await self._interface.set_dps(states) + except Exception: + _LOGGER.exception("Failed to set DPs %r", states) + else: + _LOGGER.error( + "Not connected to device %s", self._config_entry[CONF_FRIENDLY_NAME] + ) + @callback def status_updated(self, status): """Device updated status.""" diff --git a/custom_components/localtuya/const.py b/custom_components/localtuya/const.py index be5969a..942d42f 100644 --- a/custom_components/localtuya/const.py +++ b/custom_components/localtuya/const.py @@ -13,6 +13,8 @@ CONF_BRIGHTNESS_LOWER = "brightness_lower" CONF_BRIGHTNESS_UPPER = "brightness_upper" 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" # switch CONF_CURRENT = "current" diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index 67e3adf..fbd67cd 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -3,6 +3,7 @@ import logging import textwrap from functools import partial +import homeassistant.util.color as color_util import voluptuous as vol from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -10,8 +11,8 @@ from homeassistant.components.light import ( ATTR_HS_COLOR, DOMAIN, SUPPORT_BRIGHTNESS, - SUPPORT_COLOR_TEMP, SUPPORT_COLOR, + SUPPORT_COLOR_TEMP, LightEntity, ) from homeassistant.const import CONF_BRIGHTNESS, CONF_COLOR_TEMP @@ -22,12 +23,15 @@ from .const import ( CONF_BRIGHTNESS_UPPER, CONF_COLOR, CONF_COLOR_MODE, + CONF_COLOR_TEMP_MAX_KELVIN, + CONF_COLOR_TEMP_MIN_KELVIN, ) _LOGGER = logging.getLogger(__name__) -MIN_MIRED = 153 -MAX_MIRED = 370 +MIRED_TO_KELVIN_CONST = 1000000 +DEFAULT_MIN_KELVIN = 2700 # MIRED 370 +DEFAULT_MAX_KELVIN = 6500 # MIRED 153 DEFAULT_LOWER_BRIGHTNESS = 29 DEFAULT_UPPER_BRIGHTNESS = 1000 @@ -54,6 +58,12 @@ def flow_schema(dps): ), vol.Optional(CONF_COLOR_MODE): vol.In(dps), vol.Optional(CONF_COLOR): vol.In(dps), + vol.Optional(CONF_COLOR_TEMP_MIN_KELVIN, default=DEFAULT_MIN_KELVIN): vol.All( + vol.Coerce(int), vol.Range(min=1500, max=8000) + ), + vol.Optional(CONF_COLOR_TEMP_MAX_KELVIN, default=DEFAULT_MAX_KELVIN): vol.All( + vol.Coerce(int), vol.Range(min=1500, max=8000) + ), } @@ -78,6 +88,15 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): self._upper_brightness = self._config.get( 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._min_mired = round( + MIRED_TO_KELVIN_CONST + / self._config.get(CONF_COLOR_TEMP_MAX_KELVIN, DEFAULT_MAX_KELVIN) + ) self._is_white_mode = True # Hopefully sane default self._hs = None @@ -102,18 +121,24 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): def color_temp(self): """Return the color_temp of the light.""" if self.has_config(CONF_COLOR_TEMP): - return int(MAX_MIRED - (((MAX_MIRED - MIN_MIRED) / 255) * self._color_temp)) + return int( + self._max_mired + - ( + ((self._max_mired - self._min_mired) / self._upper_color_temp) + * self._color_temp + ) + ) return None @property def min_mireds(self): """Return color temperature min mireds.""" - return MIN_MIRED + return self._min_mired @property def max_mireds(self): """Return color temperature max mireds.""" - return MAX_MIRED + return self._max_mired @property def supported_features(self): @@ -127,11 +152,14 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): supports |= SUPPORT_COLOR | SUPPORT_BRIGHTNESS return supports + def __is_color_rgb_encoded(self): + return len(self.dps_conf(CONF_COLOR)) > 12 + async def async_turn_on(self, **kwargs): """Turn on or control the light.""" - await self._device.set_dp(True, self._dp_id) + states = {} + states[self._dp_id] = True features = self.supported_features - if ATTR_BRIGHTNESS in kwargs and (features & SUPPORT_BRIGHTNESS): brightness = map_range( int(kwargs[ATTR_BRIGHTNESS]), @@ -141,31 +169,63 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): self._upper_brightness, ) if self._is_white_mode: - await self._device.set_dp(brightness, self._config.get(CONF_BRIGHTNESS)) + states[self._config.get(CONF_BRIGHTNESS)] = brightness + else: - color = "{:04x}{:04x}{:04x}".format( - round(self._hs[0]), round(self._hs[1] * 10.0), brightness - ) - await self._device.set_dp(color, self._config.get(CONF_COLOR)) + if self.__is_color_rgb_encoded(): + rgb = color_util.color_hsv_to_RGB( + self._hs[0], + self._hs[1], + int(brightness * 100 / self._upper_brightness), + ) + color = "{:02x}{:02x}{:02x}{:04x}{:02x}{:02x}".format( + round(rgb[0]), + round(rgb[1]), + round(rgb[2]), + round(self._hs[0]), + round(self._hs[1] * 255 / 100), + brightness, + ) + else: + color = "{:04x}{:04x}{:04x}".format( + round(self._hs[0]), round(self._hs[1] * 10.0), brightness + ) + states[self._config.get(CONF_COLOR)] = color if ATTR_HS_COLOR in kwargs and (features & SUPPORT_COLOR): hs = kwargs[ATTR_HS_COLOR] - color = "{:04x}{:04x}{:04x}".format( - round(hs[0]), round(hs[1] * 10.0), self._brightness - ) - await self._device.set_dp(color, self._config.get(CONF_COLOR)) + if self.__is_color_rgb_encoded(): + rgb = color_util.color_hsv_to_RGB( + hs[0], hs[1], int(self._brightness * 100 / self._upper_brightness) + ) + color = "{:02x}{:02x}{:02x}{:04x}{:02x}{:02x}".format( + round(rgb[0]), + round(rgb[1]), + round(rgb[2]), + round(hs[0]), + round(hs[1] * 255 / 100), + self._brightness, + ) + else: + color = "{:04x}{:04x}{:04x}".format( + round(hs[0]), round(hs[1] * 10.0), self._brightness + ) + states[self._config.get(CONF_COLOR)] = color if self._is_white_mode: - await self._device.set_dp("colour", self._config.get(CONF_COLOR_MODE)) + states[self._config.get(CONF_COLOR_MODE)] = "colour" if ATTR_COLOR_TEMP in kwargs and (features & SUPPORT_COLOR_TEMP): color_temp = int( - 255 - - (255 / (MAX_MIRED - MIN_MIRED)) - * (int(kwargs[ATTR_COLOR_TEMP]) - MIN_MIRED) + self._upper_color_temp + - (self._upper_color_temp / (self._max_mired - self._min_mired)) + * (int(kwargs[ATTR_COLOR_TEMP]) - self._min_mired) ) - await self._device.set_dp(color_temp, self._config.get(CONF_COLOR_TEMP)) if not self._is_white_mode: - await self._device.set_dp("white", self._config.get(CONF_COLOR_MODE)) + states[self._config.get(CONF_COLOR_MODE)] = "white" + states[self._config.get(CONF_BRIGHTNESS)] = self._brightness + states[self._config.get(CONF_COLOR_TEMP)] = color_temp + + await self._device.set_dps(states) async def async_turn_off(self, **kwargs): """Turn Tuya light off.""" @@ -179,16 +239,26 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): if supported & SUPPORT_COLOR: self._is_white_mode = self.dps_conf(CONF_COLOR_MODE) == "white" if self._is_white_mode: - self._hs = None + self._hs = [0, 0] if self._is_white_mode: if supported & SUPPORT_BRIGHTNESS: self._brightness = self.dps_conf(CONF_BRIGHTNESS) else: - hsv_color = self.dps_conf(CONF_COLOR) - hue, sat, value = [int(value, 16) for value in textwrap.wrap(hsv_color, 4)] - self._hs = [hue, sat / 10.0] - self._brightness = value + color = self.dps_conf(CONF_COLOR) + if color is not None: + if self.__is_color_rgb_encoded(): + hue = int(color[6:10], 16) + sat = int(color[10:12], 16) + value = int(color[12:14], 16) + self._hs = [hue, (sat * 100 / 255)] + self._brightness = value + else: + hue, sat, value = [ + int(value, 16) for value in textwrap.wrap(color, 4) + ] + self._hs = [hue, sat / 10.0] + self._brightness = value if supported & SUPPORT_COLOR_TEMP: self._color_temp = self.dps_conf(CONF_COLOR_TEMP) diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index fb4f57b..d90d52f 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -444,7 +444,7 @@ class TuyaProtocol(asyncio.Protocol): """ return await self.exchange(SET, {str(dp_index): value}) - async def set_dps(self, value, dps): + async def set_dps(self, dps): """Set values for a set of datapoints.""" return await self.exchange(SET, dps) diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index 2f98576..9e87bf2 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -61,7 +61,9 @@ "brightness_upper": "Brightness Upper Value", "color_temp": "Color Temperature", "color": "Color", - "color_mode": "Color Mode" + "color_mode": "Color Mode", + "color_temp_min_kelvin": "Minimum Color Temperature in K", + "color_temp_max_kelvin": "Maximum Color Temperature in K" } } } @@ -103,7 +105,9 @@ "brightness_upper": "Brightness Upper Value", "color_temp": "Color Temperature", "color": "Color", - "color_mode": "Color Mode" + "color_mode": "Color Mode", + "color_temp_min_kelvin": "Minimum Color Temperature in K", + "color_temp_max_kelvin": "Maximum Color Temperature in K" } }, "yaml_import": {