From ca4f69f5574cc444017532cdf9d3cf01b198fba1 Mon Sep 17 00:00:00 2001 From: huangyupeng Date: Sun, 15 Jul 2018 08:48:32 +0800 Subject: [PATCH] Add Tuya light platform (#15444) * add tuya light platform * fix as review required --- homeassistant/components/light/tuya.py | 102 +++++++++++++++++++++++++ homeassistant/components/tuya.py | 12 ++- requirements_all.txt | 2 +- 3 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 homeassistant/components/light/tuya.py diff --git a/homeassistant/components/light/tuya.py b/homeassistant/components/light/tuya.py new file mode 100644 index 00000000000..d7691cea011 --- /dev/null +++ b/homeassistant/components/light/tuya.py @@ -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 diff --git a/homeassistant/components/tuya.py b/homeassistant/components/tuya.py index 7263871e249..c557774b5f1 100644 --- a/homeassistant/components/tuya.py +++ b/homeassistant/components/tuya.py @@ -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() diff --git a/requirements_all.txt b/requirements_all.txt index 867f9d502b9..d688ef7adb1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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