Add support for HSV encoded color lights (#90)

* Add support for HSV encoded color light

* Add debug logs for entity config

* Unset color when going to white mode
This commit is contained in:
Pierre Ståhl
2020-10-26 12:09:31 +01:00
committed by GitHub
parent f3cfde0308
commit d783293a21
4 changed files with 72 additions and 21 deletions

View File

@@ -219,6 +219,8 @@ class LocalTuyaEntity(Entity):
"""Subscribe localtuya events.""" """Subscribe localtuya events."""
await super().async_added_to_hass() await super().async_added_to_hass()
_LOGGER.debug("Adding %s with configuration: %s", self.entity_id, self._config)
def _update_handler(status): def _update_handler(status):
"""Update entity state when status was updated.""" """Update entity state when status was updated."""
if status is not None: if status is not None:
@@ -291,7 +293,14 @@ class LocalTuyaEntity(Entity):
This method looks up which DP a certain config item uses based on This method looks up which DP a certain config item uses based on
user configuration and returns its value. user configuration and returns its value.
""" """
return self.dps(self._config.get(conf_item)) dp_index = self._config.get(conf_item)
if dp_index is None:
_LOGGER.warning(
"Entity %s is requesting unset index for option %s",
self.entity_id,
conf_item,
)
return self.dps(dp_index)
def status_updated(self): def status_updated(self):
"""Device status was updated. """Device status was updated.

View File

@@ -11,6 +11,8 @@ CONF_DPS_STRINGS = "dps_strings"
# light # light
CONF_BRIGHTNESS_LOWER = "brightness_lower" CONF_BRIGHTNESS_LOWER = "brightness_lower"
CONF_BRIGHTNESS_UPPER = "brightness_upper" CONF_BRIGHTNESS_UPPER = "brightness_upper"
CONF_COLOR = "color"
CONF_COLOR_MODE = "color_mode"
# switch # switch
CONF_CURRENT = "current" CONF_CURRENT = "current"

View File

@@ -1,5 +1,6 @@
"""Platform to locally control Tuya-based light devices.""" """Platform to locally control Tuya-based light devices."""
import logging import logging
import textwrap
from functools import partial from functools import partial
import voluptuous as vol import voluptuous as vol
@@ -10,12 +11,18 @@ from homeassistant.components.light import (
DOMAIN, DOMAIN,
SUPPORT_BRIGHTNESS, SUPPORT_BRIGHTNESS,
SUPPORT_COLOR_TEMP, SUPPORT_COLOR_TEMP,
SUPPORT_COLOR,
LightEntity, LightEntity,
) )
from homeassistant.const import CONF_BRIGHTNESS, CONF_COLOR_TEMP from homeassistant.const import CONF_BRIGHTNESS, CONF_COLOR_TEMP
from .common import LocalTuyaEntity, async_setup_entry from .common import LocalTuyaEntity, async_setup_entry
from .const import CONF_BRIGHTNESS_LOWER, CONF_BRIGHTNESS_UPPER from .const import (
CONF_BRIGHTNESS_LOWER,
CONF_BRIGHTNESS_UPPER,
CONF_COLOR,
CONF_COLOR_MODE,
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -45,6 +52,8 @@ def flow_schema(dps):
vol.Optional(CONF_BRIGHTNESS_UPPER, default=DEFAULT_UPPER_BRIGHTNESS): vol.All( vol.Optional(CONF_BRIGHTNESS_UPPER, default=DEFAULT_UPPER_BRIGHTNESS): vol.All(
vol.Coerce(int), vol.Range(min=0, max=10000) vol.Coerce(int), vol.Range(min=0, max=10000)
), ),
vol.Optional(CONF_COLOR_MODE): vol.In(dps),
vol.Optional(CONF_COLOR): vol.In(dps),
} }
@@ -69,6 +78,8 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
self._upper_brightness = self._config.get( self._upper_brightness = self._config.get(
CONF_BRIGHTNESS_UPPER, DEFAULT_UPPER_BRIGHTNESS CONF_BRIGHTNESS_UPPER, DEFAULT_UPPER_BRIGHTNESS
) )
self._is_white_mode = True # Hopefully sane default
self._hs = None
@property @property
def is_on(self): def is_on(self):
@@ -78,7 +89,14 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
@property @property
def brightness(self): def brightness(self):
"""Return the brightness of the light.""" """Return the brightness of the light."""
return self._brightness return map_range(
self._brightness, self._lower_brightness, self._upper_brightness, 0, 255
)
@property
def hs_color(self):
"""Return the hs color value."""
return self._hs
@property @property
def color_temp(self): def color_temp(self):
@@ -105,6 +123,8 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
supports |= SUPPORT_BRIGHTNESS supports |= SUPPORT_BRIGHTNESS
if self.has_config(CONF_COLOR_TEMP): if self.has_config(CONF_COLOR_TEMP):
supports |= SUPPORT_COLOR_TEMP supports |= SUPPORT_COLOR_TEMP
if self.has_config(CONF_COLOR):
supports |= SUPPORT_COLOR | SUPPORT_BRIGHTNESS
return supports return supports
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
@@ -120,10 +140,22 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
self._lower_brightness, self._lower_brightness,
self._upper_brightness, self._upper_brightness,
) )
await self._device.set_dp(brightness, self._config.get(CONF_BRIGHTNESS)) if self._is_white_mode:
await self._device.set_dp(brightness, self._config.get(CONF_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 ATTR_HS_COLOR in kwargs: if ATTR_HS_COLOR in kwargs and (features & SUPPORT_COLOR):
raise ValueError(" TODO implement RGB from HS") 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_white_mode:
await self._device.set_dp("colour", self._config.get(CONF_COLOR_MODE))
if ATTR_COLOR_TEMP in kwargs and (features & SUPPORT_COLOR_TEMP): if ATTR_COLOR_TEMP in kwargs and (features & SUPPORT_COLOR_TEMP):
color_temp = int( color_temp = int(
@@ -132,6 +164,8 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
* (int(kwargs[ATTR_COLOR_TEMP]) - MIN_MIRED) * (int(kwargs[ATTR_COLOR_TEMP]) - MIN_MIRED)
) )
await self._device.set_dp(color_temp, self._config.get(CONF_COLOR_TEMP)) 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))
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs):
"""Turn Tuya light off.""" """Turn Tuya light off."""
@@ -142,17 +176,19 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
self._state = self.dps(self._dp_id) self._state = self.dps(self._dp_id)
supported = self.supported_features supported = self.supported_features
if supported & SUPPORT_BRIGHTNESS: if supported & SUPPORT_COLOR:
brightness = self.dps_conf(CONF_BRIGHTNESS) self._is_white_mode = self.dps_conf(CONF_COLOR_MODE) == "white"
if brightness is not None: if self._is_white_mode:
brightness = map_range( self._hs = None
brightness,
self._lower_brightness, if self._is_white_mode:
self._upper_brightness, if supported & SUPPORT_BRIGHTNESS:
0, self._brightness = self.dps_conf(CONF_BRIGHTNESS)
255, else:
) hsv_color = self.dps_conf(CONF_COLOR)
self._brightness = brightness hue, sat, value = [int(value, 16) for value in textwrap.wrap(hsv_color, 4)]
self._hs = [hue, sat / 10.0]
self._brightness = value
if supported & SUPPORT_COLOR_TEMP: if supported & SUPPORT_COLOR_TEMP:
self._color_temp = self.dps_conf(CONF_COLOR_TEMP) self._color_temp = self.dps_conf(CONF_COLOR_TEMP)

View File

@@ -56,10 +56,12 @@
"scaling": "Scaling Factor", "scaling": "Scaling Factor",
"state_on": "On Value", "state_on": "On Value",
"state_off": "Off Value", "state_off": "Off Value",
"brightness": "Brightness", "brightness": "Brightness (only for white color)",
"brightness_lower": "Brightness Lower Value", "brightness_lower": "Brightness Lower Value",
"brightness_upper": "Brightness Upper Value", "brightness_upper": "Brightness Upper Value",
"color_temp": "Color Temperature" "color_temp": "Color Temperature",
"color": "Color",
"color_mode": "Color Mode"
} }
} }
} }
@@ -96,10 +98,12 @@
"scaling": "Scaling Factor", "scaling": "Scaling Factor",
"state_on": "On Value", "state_on": "On Value",
"state_off": "Off Value", "state_off": "Off Value",
"brightness": "Brightness", "brightness": "Brightness (only for white color)",
"brightness_lower": "Brightness Lower Value", "brightness_lower": "Brightness Lower Value",
"brightness_upper": "Brightness Upper Value", "brightness_upper": "Brightness Upper Value",
"color_temp": "Color Temperature" "color_temp": "Color Temperature",
"color": "Color",
"color_mode": "Color Mode"
} }
}, },
"yaml_import": { "yaml_import": {