hass-core/homeassistant/components/light/wemo.py
2016-02-21 21:57:53 -05:00

117 lines
3.4 KiB
Python

"""
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)