Update to new pywemo bridge API

Support RGBW and tunable white lights.
pull/1567/head
Jan Harkes 2016-02-24 21:54:21 -05:00
parent fa15d86c2b
commit 27aba5c834
3 changed files with 50 additions and 22 deletions

View File

@ -8,8 +8,10 @@ import logging
from datetime import timedelta
import homeassistant.util as util
import homeassistant.util.color as color_util
from homeassistant.components.light import (
Light, ATTR_BRIGHTNESS)
Light, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION,
ATTR_XY_COLOR)
DEPENDENCIES = ['wemo']
@ -39,17 +41,14 @@ def setup_bridge(bridge, add_devices_callback):
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
def update_lights():
"""Update the WeMo led objects with latest info from the bridge."""
bridge.bridge_get_lights()
bridge.bridge_update()
new_lights = []
for light_id, info in bridge.Lights.items():
for light_id, device in bridge.Lights.items():
if light_id not in lights:
lights[light_id] = WemoLight(bridge, light_id, info,
update_lights)
lights[light_id] = WemoLight(device, update_lights)
new_lights.append(lights[light_id])
else:
lights[light_id].info = info
if new_lights:
add_devices_callback(new_lights)
@ -60,44 +59,73 @@ def setup_bridge(bridge, add_devices_callback):
class WemoLight(Light):
"""Representation of a WeMo light."""
def __init__(self, bridge, light_id, info, update_lights):
def __init__(self, device, update_lights):
"""Initialize the light."""
self.bridge = bridge
self.light_id = light_id
self.info = info
self.light_id = device.name
self.device = device
self.update_lights = update_lights
@property
def unique_id(self):
"""Return the ID of this light."""
deviceid = self.bridge.light_get_id(self.info)
deviceid = self.device.uniqueID
return "{}.{}".format(self.__class__, deviceid)
@property
def name(self):
"""Return the name of the light."""
return self.bridge.light_name(self.info)
return self.device.name
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
state = self.bridge.light_get_state(self.info)
return int(state['dim'])
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')
@property
def is_on(self):
"""True if device is on."""
state = self.bridge.light_get_state(self.info)
return int(state['state'])
return self.device.state['onoff'] != 0
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)
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]))
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)
def turn_off(self, **kwargs):
"""Turn the light off."""
self.bridge.light_set_state(self.info, state=0, dim=0)
transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))
self.device.turn_off(transition=transitiontime)
def update(self):
"""Synchronize state with bridge."""

View File

@ -9,7 +9,7 @@ import logging
from homeassistant.components import discovery
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
REQUIREMENTS = ['pywemo==0.3.16']
REQUIREMENTS = ['pywemo==0.3.17']
DOMAIN = 'wemo'
DISCOVER_LIGHTS = 'wemo.light'

View File

@ -222,7 +222,7 @@ pyuserinput==0.1.9
pyvera==0.2.8
# homeassistant.components.wemo
pywemo==0.3.16
pywemo==0.3.17
# homeassistant.components.thermostat.radiotherm
radiotherm==1.2