2015-08-11 14:53:55 +02:00
|
|
|
"""
|
|
|
|
Support for Wink lights.
|
2015-10-21 10:45:08 +02:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 13:12:18 +01:00
|
|
|
https://home-assistant.io/components/light.wink/
|
2015-08-11 14:53:55 +02:00
|
|
|
"""
|
2014-12-16 00:01:15 -08:00
|
|
|
import logging
|
2016-09-11 10:30:31 -04:00
|
|
|
import colorsys
|
2014-12-16 00:01:15 -08:00
|
|
|
|
2016-09-12 15:52:22 +02:00
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR, Light)
|
2016-06-29 17:16:53 -04:00
|
|
|
from homeassistant.components.wink import WinkDevice
|
2015-01-15 21:25:24 -08:00
|
|
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
2016-04-17 20:07:21 -06:00
|
|
|
from homeassistant.util import color as color_util
|
|
|
|
from homeassistant.util.color import \
|
|
|
|
color_temperature_mired_to_kelvin as mired_to_kelvin
|
2014-12-16 00:01:15 -08:00
|
|
|
|
2016-09-11 10:30:31 -04:00
|
|
|
REQUIREMENTS = ['python-wink==0.7.14', 'pubnub==3.8.2']
|
2014-12-16 00:01:15 -08:00
|
|
|
|
2016-08-16 09:07:07 +03:00
|
|
|
SUPPORT_WINK = SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR
|
|
|
|
|
2015-12-16 22:45:58 -05:00
|
|
|
|
2016-09-12 15:52:22 +02:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
"""Setup the Wink lights."""
|
2015-07-20 00:41:57 -07:00
|
|
|
import pywink
|
|
|
|
|
2015-01-10 23:47:23 -08:00
|
|
|
token = config.get(CONF_ACCESS_TOKEN)
|
2014-12-16 00:01:15 -08:00
|
|
|
|
2015-01-19 00:02:25 -08:00
|
|
|
if not pywink.is_token_set() and token is None:
|
2015-01-10 22:53:41 -08:00
|
|
|
logging.getLogger(__name__).error(
|
|
|
|
"Missing wink access_token - "
|
|
|
|
"get one at https://winkbearertoken.appspot.com/")
|
2015-01-19 00:02:25 -08:00
|
|
|
return
|
2014-12-16 00:01:15 -08:00
|
|
|
|
2015-01-19 00:02:25 -08:00
|
|
|
elif token is not None:
|
|
|
|
pywink.set_bearer_token(token)
|
2014-12-16 00:01:15 -08:00
|
|
|
|
2016-09-12 15:52:22 +02:00
|
|
|
add_devices(WinkLight(light) for light in pywink.get_bulbs())
|
2014-12-16 00:01:15 -08:00
|
|
|
|
|
|
|
|
2016-06-29 17:16:53 -04:00
|
|
|
class WinkLight(WinkDevice, Light):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Representation of a Wink light."""
|
2014-12-16 00:01:15 -08:00
|
|
|
|
2016-02-08 08:53:22 -08:00
|
|
|
def __init__(self, wink):
|
2016-06-29 17:16:53 -04:00
|
|
|
"""Initialize the Wink device."""
|
|
|
|
WinkDevice.__init__(self, wink)
|
2016-02-08 08:53:22 -08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Return true if light is on."""
|
2016-02-08 08:53:22 -08:00
|
|
|
return self.wink.state()
|
|
|
|
|
2016-02-06 22:28:29 -08:00
|
|
|
@property
|
|
|
|
def brightness(self):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Return the brightness of the light."""
|
2016-02-06 22:28:29 -08:00
|
|
|
return int(self.wink.brightness() * 255)
|
|
|
|
|
2016-09-11 10:30:31 -04:00
|
|
|
@property
|
|
|
|
def rgb_color(self):
|
|
|
|
"""Current bulb color in RGB."""
|
|
|
|
if not self.wink.supports_hue_saturation():
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
hue = self.wink.color_hue()
|
|
|
|
saturation = self.wink.color_saturation()
|
|
|
|
value = int(self.wink.brightness() * 255)
|
|
|
|
rgb = colorsys.hsv_to_rgb(hue, saturation, value)
|
|
|
|
r_value = int(round(rgb[0]))
|
|
|
|
g_value = int(round(rgb[1]))
|
|
|
|
b_value = int(round(rgb[2]))
|
|
|
|
return r_value, g_value, b_value
|
|
|
|
|
2016-04-17 20:07:21 -06:00
|
|
|
@property
|
|
|
|
def xy_color(self):
|
|
|
|
"""Current bulb color in CIE 1931 (XY) color space."""
|
|
|
|
if not self.wink.supports_xy_color():
|
|
|
|
return None
|
|
|
|
return self.wink.color_xy()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Current bulb color in degrees Kelvin."""
|
|
|
|
if not self.wink.supports_temperature():
|
|
|
|
return None
|
|
|
|
return color_util.color_temperature_kelvin_to_mired(
|
|
|
|
self.wink.color_temperature_kelvin())
|
|
|
|
|
2016-08-16 09:07:07 +03:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_WINK
|
|
|
|
|
2015-01-15 21:25:24 -08:00
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Turn the switch on."""
|
2015-01-15 21:25:24 -08:00
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
2016-04-17 20:07:21 -06:00
|
|
|
rgb_color = kwargs.get(ATTR_RGB_COLOR)
|
|
|
|
color_temp_mired = kwargs.get(ATTR_COLOR_TEMP)
|
|
|
|
|
|
|
|
state_kwargs = {
|
|
|
|
}
|
|
|
|
|
|
|
|
if rgb_color:
|
2016-09-11 10:30:31 -04:00
|
|
|
if self.wink.supports_xy_color():
|
|
|
|
xyb = color_util.color_RGB_to_xy(*rgb_color)
|
|
|
|
state_kwargs['color_xy'] = xyb[0], xyb[1]
|
|
|
|
state_kwargs['brightness'] = xyb[2]
|
|
|
|
elif self.wink.supports_hue_saturation():
|
|
|
|
hsv = colorsys.rgb_to_hsv(rgb_color[0],
|
|
|
|
rgb_color[1], rgb_color[2])
|
|
|
|
state_kwargs['color_hue_saturation'] = hsv[0], hsv[1]
|
2016-04-17 20:07:21 -06:00
|
|
|
|
|
|
|
if color_temp_mired:
|
|
|
|
state_kwargs['color_kelvin'] = mired_to_kelvin(color_temp_mired)
|
|
|
|
|
|
|
|
if brightness:
|
|
|
|
state_kwargs['brightness'] = brightness / 255.0
|
2014-12-16 00:01:15 -08:00
|
|
|
|
2016-04-17 20:07:21 -06:00
|
|
|
self.wink.set_state(True, **state_kwargs)
|
2016-02-08 08:53:22 -08:00
|
|
|
|
|
|
|
def turn_off(self):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Turn the switch off."""
|
2016-02-08 08:53:22 -08:00
|
|
|
self.wink.set_state(False)
|