2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Wink lights."""
|
2019-10-10 16:51:28 +00:00
|
|
|
import pywink
|
|
|
|
|
2016-09-12 13:52:22 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
|
|
|
Light,
|
|
|
|
)
|
2016-04-18 02:07:21 +00:00
|
|
|
from homeassistant.util import color as color_util
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.util.color import (
|
2019-07-31 19:25:30 +00:00
|
|
|
color_temperature_mired_to_kelvin as mired_to_kelvin,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import DOMAIN, WinkDevice
|
2014-12-16 08:01:15 +00:00
|
|
|
|
2015-12-17 03:45:58 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Set up the Wink lights."""
|
2015-07-20 07:41:57 +00:00
|
|
|
|
2017-02-02 06:43:12 +00:00
|
|
|
for light in pywink.get_light_bulbs():
|
|
|
|
_id = light.object_id() + light.name()
|
2019-07-31 19:25:30 +00:00
|
|
|
if _id not in hass.data[DOMAIN]["unique_ids"]:
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([WinkLight(light, hass)])
|
2017-07-19 22:27:39 +00:00
|
|
|
for light in pywink.get_light_groups():
|
|
|
|
_id = light.object_id() + light.name()
|
2019-07-31 19:25:30 +00:00
|
|
|
if _id not in hass.data[DOMAIN]["unique_ids"]:
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([WinkLight(light, hass)])
|
2014-12-16 08:01:15 +00:00
|
|
|
|
|
|
|
|
2016-06-29 21:16:53 +00:00
|
|
|
class WinkLight(WinkDevice, Light):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Representation of a Wink light."""
|
2014-12-16 08:01:15 +00:00
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_added_to_hass(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Call when entity is added to hass."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.data[DOMAIN]["entities"]["light"].append(self)
|
2017-05-13 18:09:00 +00:00
|
|
|
|
2016-02-08 16:53:22 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return true if light is on."""
|
2016-02-08 16:53:22 +00:00
|
|
|
return self.wink.state()
|
|
|
|
|
2016-02-07 06:28:29 +00:00
|
|
|
@property
|
|
|
|
def brightness(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return the brightness of the light."""
|
2016-11-06 15:27:15 +00:00
|
|
|
if self.wink.brightness() is not None:
|
|
|
|
return int(self.wink.brightness() * 255)
|
2017-07-06 06:30:01 +00:00
|
|
|
return None
|
2016-02-07 06:28:29 +00:00
|
|
|
|
2016-09-11 14:30:31 +00:00
|
|
|
@property
|
2018-05-04 10:28:56 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Define current bulb color."""
|
|
|
|
if self.wink.supports_xy_color():
|
|
|
|
return color_util.color_xy_to_hs(*self.wink.color_xy())
|
|
|
|
|
|
|
|
if self.wink.supports_hue_saturation():
|
2016-09-11 14:30:31 +00:00
|
|
|
hue = self.wink.color_hue()
|
|
|
|
saturation = self.wink.color_saturation()
|
2018-05-04 10:28:56 +00:00
|
|
|
if hue is not None and saturation is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
return hue * 360, saturation * 100
|
2016-09-11 14:30:31 +00:00
|
|
|
|
2018-05-04 10:28:56 +00:00
|
|
|
return None
|
2016-04-18 02:07:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Define current bulb color in degrees Kelvin."""
|
2016-04-18 02:07:21 +00:00
|
|
|
if not self.wink.supports_temperature():
|
|
|
|
return None
|
|
|
|
return color_util.color_temperature_kelvin_to_mired(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.wink.color_temperature_kelvin()
|
|
|
|
)
|
2016-04-18 02:07:21 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2018-05-15 05:53:12 +00:00
|
|
|
supports = SUPPORT_BRIGHTNESS
|
|
|
|
if self.wink.supports_temperature():
|
|
|
|
supports = supports | SUPPORT_COLOR_TEMP
|
|
|
|
if self.wink.supports_xy_color():
|
|
|
|
supports = supports | SUPPORT_COLOR
|
|
|
|
elif self.wink.supports_hue_saturation():
|
|
|
|
supports = supports | SUPPORT_COLOR
|
|
|
|
return supports
|
2016-08-16 06:07:07 +00:00
|
|
|
|
2015-01-16 05:25:24 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Turn the switch on."""
|
2015-01-16 05:25:24 +00:00
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
2018-03-18 22:00:29 +00:00
|
|
|
hs_color = kwargs.get(ATTR_HS_COLOR)
|
2016-04-18 02:07:21 +00:00
|
|
|
color_temp_mired = kwargs.get(ATTR_COLOR_TEMP)
|
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
state_kwargs = {}
|
2016-04-18 02:07:21 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if hs_color:
|
2016-09-11 14:30:31 +00:00
|
|
|
if self.wink.supports_xy_color():
|
2018-03-18 22:00:29 +00:00
|
|
|
xy_color = color_util.color_hs_to_xy(*hs_color)
|
2019-07-31 19:25:30 +00:00
|
|
|
state_kwargs["color_xy"] = xy_color
|
2017-07-19 22:27:39 +00:00
|
|
|
if self.wink.supports_hue_saturation():
|
2019-07-31 19:25:30 +00:00
|
|
|
hs_scaled = hs_color[0] / 360, hs_color[1] / 100
|
|
|
|
state_kwargs["color_hue_saturation"] = hs_scaled
|
2016-04-18 02:07:21 +00:00
|
|
|
|
|
|
|
if color_temp_mired:
|
2019-07-31 19:25:30 +00:00
|
|
|
state_kwargs["color_kelvin"] = mired_to_kelvin(color_temp_mired)
|
2016-04-18 02:07:21 +00:00
|
|
|
|
|
|
|
if brightness:
|
2019-07-31 19:25:30 +00:00
|
|
|
state_kwargs["brightness"] = brightness / 255.0
|
2014-12-16 08:01:15 +00:00
|
|
|
|
2016-04-18 02:07:21 +00:00
|
|
|
self.wink.set_state(True, **state_kwargs)
|
2016-02-08 16:53:22 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Turn the switch off."""
|
2016-02-08 16:53:22 +00:00
|
|
|
self.wink.set_state(False)
|