2019-02-13 20:21:14 +00:00
|
|
|
"""Support for the Tuya lights."""
|
2018-07-15 00:48:32 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ENTITY_ID_FORMAT,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-07-15 00:48:32 +00:00
|
|
|
from homeassistant.util import color as colorutil
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DATA_TUYA, TuyaDevice
|
|
|
|
|
2020-04-22 03:29:42 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
2018-07-15 00:48:32 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-07-15 00:48:32 +00:00
|
|
|
"""Set up Tuya light platform."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
tuya = hass.data[DATA_TUYA]
|
2019-07-31 19:25:30 +00:00
|
|
|
dev_ids = discovery_info.get("dev_ids")
|
2018-07-15 00:48:32 +00:00
|
|
|
devices = []
|
|
|
|
for dev_id in dev_ids:
|
|
|
|
device = tuya.get_device_by_id(dev_id)
|
|
|
|
if device is None:
|
|
|
|
continue
|
|
|
|
devices.append(TuyaLight(device))
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(devices)
|
2018-07-15 00:48:32 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class TuyaLight(TuyaDevice, LightEntity):
|
2018-07-15 00:48:32 +00:00
|
|
|
"""Tuya light device."""
|
|
|
|
|
|
|
|
def __init__(self, tuya):
|
|
|
|
"""Init Tuya light device."""
|
|
|
|
super().__init__(tuya)
|
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(tuya.object_id())
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of the light."""
|
2019-09-09 19:44:00 +00:00
|
|
|
if self.tuya.brightness() is None:
|
|
|
|
return None
|
2018-10-19 07:17:19 +00:00
|
|
|
return int(self.tuya.brightness())
|
2018-07-15 00:48:32 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def hs_color(self):
|
|
|
|
"""Return the hs_color of the light."""
|
2018-10-19 07:17:19 +00:00
|
|
|
return tuple(map(int, self.tuya.hs_color()))
|
2018-07-15 00:48:32 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the color_temp of the light."""
|
2018-10-19 07:17:19 +00:00
|
|
|
color_temp = int(self.tuya.color_temp())
|
2018-07-15 00:48:32 +00:00
|
|
|
if color_temp is None:
|
|
|
|
return None
|
|
|
|
return colorutil.color_temperature_kelvin_to_mired(color_temp)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
|
|
|
return self.tuya.state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def min_mireds(self):
|
|
|
|
"""Return color temperature min mireds."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return colorutil.color_temperature_kelvin_to_mired(self.tuya.min_color_temp())
|
2018-07-15 00:48:32 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self):
|
|
|
|
"""Return color temperature max mireds."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return colorutil.color_temperature_kelvin_to_mired(self.tuya.max_color_temp())
|
2018-07-15 00:48:32 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn on or control the light."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
ATTR_BRIGHTNESS not in kwargs
|
|
|
|
and ATTR_HS_COLOR not in kwargs
|
|
|
|
and ATTR_COLOR_TEMP not in kwargs
|
|
|
|
):
|
2018-07-15 00:48:32 +00:00
|
|
|
self.tuya.turn_on()
|
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
self.tuya.set_brightness(kwargs[ATTR_BRIGHTNESS])
|
|
|
|
if ATTR_HS_COLOR in kwargs:
|
|
|
|
self.tuya.set_color(kwargs[ATTR_HS_COLOR])
|
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
|
|
|
color_temp = colorutil.color_temperature_mired_to_kelvin(
|
2019-07-31 19:25:30 +00:00
|
|
|
kwargs[ATTR_COLOR_TEMP]
|
|
|
|
)
|
2018-07-15 00:48:32 +00:00
|
|
|
self.tuya.set_color_temp(color_temp)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Instruct the light to turn off."""
|
|
|
|
self.tuya.turn_off()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
supports = SUPPORT_BRIGHTNESS
|
|
|
|
if self.tuya.support_color():
|
|
|
|
supports = supports | SUPPORT_COLOR
|
|
|
|
if self.tuya.support_color_temp():
|
|
|
|
supports = supports | SUPPORT_COLOR_TEMP
|
|
|
|
return supports
|