From 27aba5c834d349a8245eb484d9d2ed70fd77e13f Mon Sep 17 00:00:00 2001
From: Jan Harkes <jaharkes@cs.cmu.edu>
Date: Wed, 24 Feb 2016 21:54:21 -0500
Subject: [PATCH] Update to new pywemo bridge API

Support RGBW and tunable white lights.
---
 homeassistant/components/light/wemo.py | 68 ++++++++++++++++++--------
 homeassistant/components/wemo.py       |  2 +-
 requirements_all.txt                   |  2 +-
 3 files changed, 50 insertions(+), 22 deletions(-)

diff --git a/homeassistant/components/light/wemo.py b/homeassistant/components/light/wemo.py
index c533b3acf13..d2844826400 100644
--- a/homeassistant/components/light/wemo.py
+++ b/homeassistant/components/light/wemo.py
@@ -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."""
diff --git a/homeassistant/components/wemo.py b/homeassistant/components/wemo.py
index d6b127a0906..c78685737e0 100644
--- a/homeassistant/components/wemo.py
+++ b/homeassistant/components/wemo.py
@@ -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'
diff --git a/requirements_all.txt b/requirements_all.txt
index 1283963d4d0..c65810cbd97 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -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