From 4dad40fffb52c1fe93b8cf1cf5a8a7c6a37d24ff Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Sat, 26 Dec 2015 00:41:33 -0500 Subject: [PATCH] Add support for WeMo LED lights. --- .coveragerc | 1 + homeassistant/components/light/wemo.py | 117 +++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 homeassistant/components/light/wemo.py diff --git a/.coveragerc b/.coveragerc index aa010d67b2b..5d54e8c10da 100644 --- a/.coveragerc +++ b/.coveragerc @@ -91,6 +91,7 @@ omit = homeassistant/components/light/hyperion.py homeassistant/components/light/lifx.py homeassistant/components/light/limitlessled.py + homeassistant/components/light/wemo.py homeassistant/components/media_player/cast.py homeassistant/components/media_player/denon.py homeassistant/components/media_player/firetv.py diff --git a/homeassistant/components/light/wemo.py b/homeassistant/components/light/wemo.py new file mode 100644 index 00000000000..1b8ed06a148 --- /dev/null +++ b/homeassistant/components/light/wemo.py @@ -0,0 +1,117 @@ +""" +homeassistant.components.light.wemo +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +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 +from homeassistant.components.light import ( + Light, ATTR_BRIGHTNESS) + +# REQUIREMENTS = ['pywemo==0.3.12'] +MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) +MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100) + +_LOGGER = logging.getLogger(__name__) + + +def setup_platform(hass, config, add_devices_callback, discovery_info=None): + """ Find WeMo bridges and return connected lights. """ + import pywemo + import pywemo.discovery as discovery + + if discovery_info is not None: + location = discovery_info[2] + mac = discovery_info[3] + device = discovery.device_from_description(location, mac) + + if device and isinstance(device, pywemo.Bridge): + setup_bridge(device, add_devices_callback) + return + + _LOGGER.info("Scanning for WeMo devices.") + devices = pywemo.discover_devices() + + # Filter out the bridges + for device in devices: + if isinstance(device, pywemo.Bridge): + _LOGGER.info("Found bridge %s.", device) + setup_bridge(device, add_devices_callback) + + +def setup_bridge(bridge, add_devices_callback): + """ Setup a WeMo link. """ + + lights = {} + + @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS) + def update_lights(): + """ Updates the WeMo led objects with latest info from the bridge. """ + + bridge.bridge_get_lights() + + new_lights = [] + + for light_id, info in bridge.Lights.items(): + if light_id not in lights: + lights[light_id] = WemoLight(light_id, info, + bridge, update_lights) + new_lights.append(lights[light_id]) + else: + lights[light_id].info = info + + if new_lights: + add_devices_callback(new_lights) + + update_lights() + + +class WemoLight(Light): + """ Represents a WeMo light """ + + def __init__(self, light_id, info, bridge, update_lights): + self.light_id = light_id + self.info = info + self.bridge = bridge + self.update_lights = update_lights + + @property + def unique_id(self): + """ Returns the id of this light """ + deviceid = self.bridge.light_get_id(self.info) + return "{}.{}".format(self.__class__, deviceid) + + @property + def name(self): + """ Get the name of the light. """ + return self.bridge.light_name(self.info) + + @property + def brightness(self): + """ Brightness of this light between 0..255. """ + state = self.bridge.light_get_state(self.info) + return int(state['dim']) + + @property + def is_on(self): + """ True if device is on. """ + state = self.bridge.light_get_state(self.info) + return int(state['state']) + + def turn_on(self, **kwargs): + """ Turn the light on. """ + dim = kwargs.get(ATTR_BRIGHTNESS, self.brightness) + self.bridge.light_set_state(self.info, state=1, dim=dim) + + def turn_off(self, **kwargs): + """ Turn the light off. """ + self.bridge.light_set_state(self.info, state=0, dim=0) + + def update(self): + """ Synchronize state with bridge. """ + self.update_lights(no_throttle=True)