From c959637ebebcd8651a7d9ecfacb7369c4b2c2b8f Mon Sep 17 00:00:00 2001 From: Jared J Date: Thu, 5 Jan 2017 17:39:28 -0500 Subject: [PATCH] Add Yeelight auto discovery, color temp (#5145) * Add Yeelight auto discovery, color temp * Fixing linting issues --- homeassistant/components/discovery.py | 1 + homeassistant/components/light/yeelight.py | 36 +++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/discovery.py b/homeassistant/components/discovery.py index 32cb205fa0f..32d57b4bf85 100644 --- a/homeassistant/components/discovery.py +++ b/homeassistant/components/discovery.py @@ -38,6 +38,7 @@ SERVICE_HANDLERS = { 'directv': ('media_player', 'directv'), 'denonavr': ('media_player', 'denonavr'), 'samsung_tv': ('media_player', 'samsungtv'), + 'yeelight': ('light', 'yeelight'), } CONFIG_SCHEMA = vol.Schema({ diff --git a/homeassistant/components/light/yeelight.py b/homeassistant/components/light/yeelight.py index d8aa138af47..a8a2ec9b3fc 100644 --- a/homeassistant/components/light/yeelight.py +++ b/homeassistant/components/light/yeelight.py @@ -11,10 +11,15 @@ import voluptuous as vol from homeassistant.const import CONF_DEVICES, CONF_NAME from homeassistant.components.light import (ATTR_BRIGHTNESS, ATTR_RGB_COLOR, + ATTR_COLOR_TEMP, SUPPORT_BRIGHTNESS, - SUPPORT_RGB_COLOR, Light, - PLATFORM_SCHEMA) + SUPPORT_RGB_COLOR, + SUPPORT_COLOR_TEMP, + Light, PLATFORM_SCHEMA) import homeassistant.helpers.config_validation as cv +from homeassistant.util import color as color_util +from homeassistant.util.color import \ + color_temperature_mired_to_kelvin as mired_to_kelvin REQUIREMENTS = ['pyyeelight==1.0-beta'] @@ -22,7 +27,8 @@ _LOGGER = logging.getLogger(__name__) DOMAIN = 'yeelight' -SUPPORT_YEELIGHT = (SUPPORT_BRIGHTNESS | SUPPORT_RGB_COLOR) +SUPPORT_YEELIGHT = (SUPPORT_BRIGHTNESS | SUPPORT_RGB_COLOR | + SUPPORT_COLOR_TEMP) DEVICE_SCHEMA = vol.Schema({vol.Optional(CONF_NAME): cv.string, }) @@ -33,9 +39,14 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the Yeelight bulbs.""" lights = [] - for ipaddr, device_config in config[CONF_DEVICES].items(): - device = {'name': device_config[CONF_NAME], 'ipaddr': ipaddr} + if discovery_info is not None: + device = {'name': discovery_info['hostname'], + 'ipaddr': discovery_info['host']} lights.append(YeelightLight(device)) + else: + for ipaddr, device_config in config[CONF_DEVICES].items(): + device = {'name': device_config[CONF_NAME], 'ipaddr': ipaddr} + lights.append(YeelightLight(device)) add_devices(lights) @@ -54,6 +65,7 @@ class YeelightLight(Light): self._state = None self._bright = None self._rgb = None + self._ct = None try: self._bulb = pyyeelight.YeelightBulb(self._ipaddr) except socket.error: @@ -86,6 +98,11 @@ class YeelightLight(Light): """Return the color property.""" return self._rgb + @property + def color_temp(self): + """Return the color temperature.""" + return color_util.color_temperature_kelvin_to_mired(self._ct) + @property def supported_features(self): """Flag supported features.""" @@ -101,6 +118,11 @@ class YeelightLight(Light): self._bulb.set_rgb_color(rgb[0], rgb[1], rgb[2]) self._rgb = [rgb[0], rgb[1], rgb[2]] + if ATTR_COLOR_TEMP in kwargs: + kelvin = int(mired_to_kelvin(kwargs[ATTR_COLOR_TEMP])) + self._bulb.set_color_temperature(kelvin) + self._ct = kelvin + if ATTR_BRIGHTNESS in kwargs: bright = int(kwargs[ATTR_BRIGHTNESS] * 100 / 255) self._bulb.set_brightness(bright) @@ -134,3 +156,7 @@ class YeelightLight(Light): green = int((raw_rgb - (red * 65536)) / 256) blue = raw_rgb - (red * 65536) - (green * 256) self._rgb = [red, green, blue] + + # Update CT value + self._ct = int(self._bulb.get_property( + self._bulb.PROPERTY_NAME_COLOR_TEMPERATURE))