Skip to content

Commit

Permalink
Add support for WeMo LED lights.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaharkes committed Feb 22, 2016
1 parent 368ad93 commit 4dad40f
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
117 changes: 117 additions & 0 deletions homeassistant/components/light/wemo.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4dad40f

Please sign in to comment.