core/homeassistant/components/light/wemo.py

143 lines
4.4 KiB
Python
Raw Normal View History

2015-12-26 05:41:33 +00:00
"""
Support for Belkin WeMo lights.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/light.wemo/
"""
import logging
from datetime import timedelta
import homeassistant.util as util
import homeassistant.util.color as color_util
2015-12-26 05:41:33 +00:00
from homeassistant.components.light import (
Light, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION,
ATTR_XY_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR,
SUPPORT_TRANSITION, SUPPORT_XY_COLOR)
2015-12-26 05:41:33 +00:00
DEPENDENCIES = ['wemo']
2015-12-26 05:41:33 +00:00
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
_LOGGER = logging.getLogger(__name__)
SUPPORT_WEMO = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR |
SUPPORT_TRANSITION | SUPPORT_XY_COLOR)
2015-12-26 05:41:33 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-07 21:08:21 +00:00
"""Setup WeMo bridges and register connected lights."""
2015-12-26 05:41:33 +00:00
import pywemo.discovery as discovery
if discovery_info is not None:
location = discovery_info['ssdp_description']
mac = discovery_info['mac_address']
2015-12-26 05:41:33 +00:00
device = discovery.device_from_description(location, mac)
if device:
setup_bridge(device, add_devices)
2015-12-26 05:41:33 +00:00
def setup_bridge(bridge, add_devices):
"""Setup a WeMo link."""
2015-12-26 05:41:33 +00:00
lights = {}
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
def update_lights():
2016-03-07 21:08:21 +00:00
"""Update the WeMo led objects with latest info from the bridge."""
bridge.bridge_update()
2015-12-26 05:41:33 +00:00
new_lights = []
for light_id, device in bridge.Lights.items():
2015-12-26 05:41:33 +00:00
if light_id not in lights:
lights[light_id] = WemoLight(device, update_lights)
2015-12-26 05:41:33 +00:00
new_lights.append(lights[light_id])
if new_lights:
add_devices(new_lights)
2015-12-26 05:41:33 +00:00
update_lights()
class WemoLight(Light):
2016-03-07 21:08:21 +00:00
"""Representation of a WeMo light."""
2015-12-26 05:41:33 +00:00
def __init__(self, device, update_lights):
2016-03-07 21:08:21 +00:00
"""Initialize the light."""
self.light_id = device.name
self.device = device
2015-12-26 05:41:33 +00:00
self.update_lights = update_lights
@property
def unique_id(self):
2016-03-07 21:08:21 +00:00
"""Return the ID of this light."""
deviceid = self.device.uniqueID
return '{}.{}'.format(self.__class__, deviceid)
2015-12-26 05:41:33 +00:00
@property
def name(self):
2016-03-07 21:08:21 +00:00
"""Return the name of the light."""
return self.device.name
2015-12-26 05:41:33 +00:00
@property
def brightness(self):
2016-03-07 21:08:21 +00:00
"""Return the brightness of this light between 0..255."""
return self.device.state.get('level', 255)
@property
def xy_color(self):
"""Return the XY color values of this light."""
return self.device.state.get('color_xy')
@property
def color_temp(self):
"""Return the color temperature of this light in mireds."""
return self.device.state.get('temperature_mireds')
2015-12-26 05:41:33 +00:00
@property
def is_on(self):
"""True if device is on."""
return self.device.state['onoff'] != 0
2015-12-26 05:41:33 +00:00
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_WEMO
2015-12-26 05:41:33 +00:00
def turn_on(self, **kwargs):
"""Turn the light on."""
transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))
if ATTR_XY_COLOR in kwargs:
xycolor = kwargs[ATTR_XY_COLOR]
elif ATTR_RGB_COLOR in kwargs:
xycolor = color_util.color_RGB_to_xy(
*(int(val) for val in kwargs[ATTR_RGB_COLOR]))
kwargs.setdefault(ATTR_BRIGHTNESS, xycolor[2])
else:
xycolor = None
if xycolor is not None:
self.device.set_color(xycolor, transition=transitiontime)
if ATTR_COLOR_TEMP in kwargs:
colortemp = kwargs[ATTR_COLOR_TEMP]
self.device.set_temperature(mireds=colortemp,
transition=transitiontime)
if ATTR_BRIGHTNESS in kwargs:
brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness or 255)
self.device.turn_on(level=brightness, transition=transitiontime)
else:
self.device.turn_on(transition=transitiontime)
2015-12-26 05:41:33 +00:00
def turn_off(self, **kwargs):
"""Turn the light off."""
transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))
self.device.turn_off(transition=transitiontime)
2015-12-26 05:41:33 +00:00
def update(self):
"""Synchronize state with bridge."""
2015-12-26 05:41:33 +00:00
self.update_lights(no_throttle=True)