core/homeassistant/components/light/wink.py

80 lines
2.1 KiB
Python
Raw Normal View History

2015-08-11 12:53:55 +00:00
"""
homeassistant.components.light.wink
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support for Wink lights.
2015-10-21 08:45:08 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/light.wink/
2015-08-11 12:53:55 +00:00
"""
import logging
2016-02-08 16:53:22 +00:00
from homeassistant.components.light import ATTR_BRIGHTNESS, Light
2015-01-16 05:25:24 +00:00
from homeassistant.const import CONF_ACCESS_TOKEN
REQUIREMENTS = ['python-wink==0.6.2']
2015-12-17 03:45:58 +00:00
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Find and return Wink lights. """
import pywink
token = config.get(CONF_ACCESS_TOKEN)
if not pywink.is_token_set() and token is None:
logging.getLogger(__name__).error(
"Missing wink access_token - "
"get one at https://winkbearertoken.appspot.com/")
return
elif token is not None:
pywink.set_bearer_token(token)
add_devices_callback(
WinkLight(light) for light in pywink.get_bulbs())
2016-02-08 16:53:22 +00:00
class WinkLight(Light):
2015-08-11 12:53:55 +00:00
""" Represents a Wink light. """
2016-02-08 16:53:22 +00:00
def __init__(self, wink):
self.wink = wink
@property
def unique_id(self):
""" Returns the id of this Wink switch. """
return "{}.{}".format(self.__class__, self.wink.device_id())
@property
def name(self):
""" Returns the name of the light if any. """
return self.wink.name()
@property
def is_on(self):
""" True if light is on. """
return self.wink.state()
@property
def brightness(self):
"""Brightness of the light."""
return int(self.wink.brightness() * 255)
2015-01-16 05:25:24 +00:00
# pylint: disable=too-few-public-methods
def turn_on(self, **kwargs):
""" Turns the switch on. """
brightness = kwargs.get(ATTR_BRIGHTNESS)
2015-01-16 05:25:24 +00:00
if brightness is not None:
2015-12-16 03:43:12 +00:00
self.wink.set_state(True, brightness=brightness / 255)
2015-01-16 05:25:24 +00:00
else:
self.wink.set_state(True)
2016-02-08 16:53:22 +00:00
def turn_off(self):
""" Turns the switch off. """
self.wink.set_state(False)
def update(self):
""" Update state of the light. """
self.wink.update_state()