Add support of RGB lights (#120)
* Make set_dps available. * Adding RGB support, brightness fix, color_temp range. * Update README for light options * Formatting * Formatting * Need to force to white to ensure color picker is updated. * Detect color mode RGB/HSV based on color length. * Extract rgb encoded color check in method. * Make check method private. * Fix exception when color is not available (maybe when powered off) * Reformat.
This commit is contained in:
@@ -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)
|
||||
|
Reference in New Issue
Block a user