Add Tuya light platform (#15444)
* add tuya light platform * fix as review required
This commit is contained in:
parent
37ccf87516
commit
ca4f69f557
3 changed files with 112 additions and 4 deletions
102
homeassistant/components/light/tuya.py
Normal file
102
homeassistant/components/light/tuya.py
Normal file
|
@ -0,0 +1,102 @@
|
|||
"""
|
||||
Support for the Tuya light.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/light.tuya/
|
||||
"""
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_HS_COLOR, ENTITY_ID_FORMAT,
|
||||
SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_COLOR, Light)
|
||||
|
||||
from homeassistant.components.tuya import DATA_TUYA, TuyaDevice
|
||||
from homeassistant.util import color as colorutil
|
||||
|
||||
DEPENDENCIES = ['tuya']
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up Tuya light platform."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
tuya = hass.data[DATA_TUYA]
|
||||
dev_ids = discovery_info.get('dev_ids')
|
||||
devices = []
|
||||
for dev_id in dev_ids:
|
||||
device = tuya.get_device_by_id(dev_id)
|
||||
if device is None:
|
||||
continue
|
||||
devices.append(TuyaLight(device))
|
||||
add_devices(devices)
|
||||
|
||||
|
||||
class TuyaLight(TuyaDevice, Light):
|
||||
"""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."""
|
||||
return self.tuya.brightness()
|
||||
|
||||
@property
|
||||
def hs_color(self):
|
||||
"""Return the hs_color of the light."""
|
||||
return self.tuya.hs_color()
|
||||
|
||||
@property
|
||||
def color_temp(self):
|
||||
"""Return the color_temp of the light."""
|
||||
color_temp = self.tuya.color_temp()
|
||||
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."""
|
||||
return colorutil.color_temperature_kelvin_to_mired(
|
||||
self.tuya.min_color_temp())
|
||||
|
||||
@property
|
||||
def max_mireds(self):
|
||||
"""Return color temperature max mireds."""
|
||||
return colorutil.color_temperature_kelvin_to_mired(
|
||||
self.tuya.max_color_temp())
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn on or control the light."""
|
||||
if (ATTR_BRIGHTNESS not in kwargs
|
||||
and ATTR_HS_COLOR not in kwargs
|
||||
and ATTR_COLOR_TEMP not in kwargs):
|
||||
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(
|
||||
kwargs[ATTR_COLOR_TEMP])
|
||||
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
|
|
@ -17,7 +17,7 @@ from homeassistant.helpers.dispatcher import (
|
|||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.event import track_time_interval
|
||||
|
||||
REQUIREMENTS = ['tuyapy==0.1.1']
|
||||
REQUIREMENTS = ['tuyapy==0.1.2']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -33,7 +33,8 @@ SERVICE_FORCE_UPDATE = 'force_update'
|
|||
SERVICE_PULL_DEVICES = 'pull_devices'
|
||||
|
||||
TUYA_TYPE_TO_HA = {
|
||||
'switch': 'switch'
|
||||
'light': 'light',
|
||||
'switch': 'switch',
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
|
@ -129,13 +130,18 @@ class TuyaDevice(Entity):
|
|||
"""Return Tuya device id."""
|
||||
return self.tuya.object_id()
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique ID."""
|
||||
return 'tuya.{}'.format(self.tuya.object_id())
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return Tuya device name."""
|
||||
return self.tuya.name()
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
def entity_picture(self):
|
||||
"""Return the entity picture to use in the frontend, if any."""
|
||||
return self.tuya.iconurl()
|
||||
|
||||
|
|
|
@ -1345,7 +1345,7 @@ total_connect_client==0.18
|
|||
transmissionrpc==0.11
|
||||
|
||||
# homeassistant.components.tuya
|
||||
tuyapy==0.1.1
|
||||
tuyapy==0.1.2
|
||||
|
||||
# homeassistant.components.twilio
|
||||
twilio==5.7.0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue