Skip to content

Commit

Permalink
Add Yeelight auto discovery, color temp (#5145)
Browse files Browse the repository at this point in the history
* Add Yeelight auto discovery, color temp

* Fixing linting issues
  • Loading branch information
jjensn authored and balloob committed Jan 5, 2017
1 parent db62304 commit c959637
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'directv': ('media_player', 'directv'),
'denonavr': ('media_player', 'denonavr'),
'samsung_tv': ('media_player', 'samsungtv'),
'yeelight': ('light', 'yeelight'),
}

CONFIG_SCHEMA = vol.Schema({
Expand Down
36 changes: 31 additions & 5 deletions homeassistant/components/light/yeelight.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@

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']

_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, })

Expand All @@ -33,9 +39,14 @@
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)

Expand All @@ -54,6 +65,7 @@ def __init__(self, device):
self._state = None
self._bright = None
self._rgb = None
self._ct = None
try:
self._bulb = pyyeelight.YeelightBulb(self._ipaddr)
except socket.error:
Expand Down Expand Up @@ -86,6 +98,11 @@ def rgb_color(self):
"""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."""
Expand All @@ -101,6 +118,11 @@ def turn_on(self, **kwargs):
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)
Expand Down Expand Up @@ -134,3 +156,7 @@ def update(self):
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))

0 comments on commit c959637

Please sign in to comment.