Add color temp and brightness config to light

This commit is contained in:
Pierre Ståhl
2020-09-23 22:17:13 +02:00
committed by rospogrigio
parent 8456c6039c
commit 792fcc5186
3 changed files with 29 additions and 13 deletions

View File

@@ -2,6 +2,13 @@
import logging
from functools import partial
import voluptuous as vol
from homeassistant.const import (
CONF_ID,
CONF_BRIGHTNESS,
CONF_COLOR_TEMP,
)
from homeassistant.components.light import (
LightEntity,
DOMAIN,
@@ -20,16 +27,13 @@ MIN_MIRED = 153
MAX_MIRED = 370
UPDATE_RETRY_LIMIT = 3
DPS_INDEX_ON = "1"
DPS_INDEX_MODE = "2"
DPS_INDEX_BRIGHTNESS = "3"
DPS_INDEX_COLOURTEMP = "4"
DPS_INDEX_COLOUR = "5"
def flow_schema(dps):
"""Return schema used in config flow."""
return {}
return {
vol.Optional(CONF_BRIGHTNESS): vol.In(dps),
vol.Optional(CONF_COLOR_TEMP): vol.In(dps),
}
class LocaltuyaLight(LocalTuyaEntity, LightEntity):
@@ -90,7 +94,7 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
if ATTR_BRIGHTNESS in kwargs:
self._device.set_dps(
max(int(kwargs[ATTR_BRIGHTNESS]), 25), DPS_INDEX_BRIGHTNESS
max(int(kwargs[ATTR_BRIGHTNESS]), 25), self._config.get(CONF_BRIGHTNESS)
)
if ATTR_HS_COLOR in kwargs:
@@ -102,7 +106,7 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
- (255 / (MAX_MIRED - MIN_MIRED))
* (int(kwargs[ATTR_COLOR_TEMP]) - MIN_MIRED)
)
self._device.set_dps(color_temp, DPS_INDEX_COLOURTEMP)
self._device.set_dps(color_temp, self._config.get(CONF_COLOR_TEMP))
def turn_off(self, **kwargs):
"""Turn Tuya light off."""
@@ -112,13 +116,13 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
"""Device status was updated."""
self._state = self.dps(self._dps_id)
brightness = self.dps(DPS_INDEX_BRIGHTNESS)
brightness = self.dps_conf(CONF_BRIGHTNESS)
if brightness is not None:
brightness = min(brightness, 255)
brightness = max(brightness, 25)
self._brightness = brightness
self._color_temp = self.dps(DPS_INDEX_COLOURTEMP)
self._color_temp = self.dps_conf(CONF_COLOR_TEMP)
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaLight, flow_schema)