From e7dc96397ced6662f2a581d293d7cc6a8799d88a Mon Sep 17 00:00:00 2001
From: Matthew Donoughe <mdonoughe@gmail.com>
Date: Fri, 10 Nov 2017 06:17:25 -0500
Subject: [PATCH] upgrade to new pylutron_caseta with TLS (#10286)

* upgrade to new pylutron with TLS

* rename configuration options

* change more methods to coroutines

* use async_add_devices
---
 .../components/cover/lutron_caseta.py         | 18 ++++++---
 .../components/light/lutron_caseta.py         | 15 +++++---
 homeassistant/components/lutron_caseta.py     | 38 ++++++++++++-------
 .../components/scene/lutron_caseta.py         |  9 +++--
 .../components/switch/lutron_caseta.py        | 15 +++++---
 requirements_all.txt                          |  2 +-
 6 files changed, 63 insertions(+), 34 deletions(-)

diff --git a/homeassistant/components/cover/lutron_caseta.py b/homeassistant/components/cover/lutron_caseta.py
index 31e4f1e3cf2..6ad9b093ed8 100644
--- a/homeassistant/components/cover/lutron_caseta.py
+++ b/homeassistant/components/cover/lutron_caseta.py
@@ -4,6 +4,7 @@ Support for Lutron Caseta shades.
 For more details about this platform, please refer to the documentation at
 https://home-assistant.io/components/cover.lutron_caseta/
 """
+import asyncio
 import logging
 
 from homeassistant.components.cover import (
@@ -18,7 +19,8 @@ DEPENDENCIES = ['lutron_caseta']
 
 
 # pylint: disable=unused-argument
-def setup_platform(hass, config, add_devices, discovery_info=None):
+@asyncio.coroutine
+def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
     """Set up the Lutron Caseta shades as a cover device."""
     devs = []
     bridge = hass.data[LUTRON_CASETA_SMARTBRIDGE]
@@ -27,7 +29,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
         dev = LutronCasetaCover(cover_device, bridge)
         devs.append(dev)
 
-    add_devices(devs, True)
+    async_add_devices(devs, True)
 
 
 class LutronCasetaCover(LutronCasetaDevice, CoverDevice):
@@ -48,21 +50,25 @@ class LutronCasetaCover(LutronCasetaDevice, CoverDevice):
         """Return the current position of cover."""
         return self._state['current_state']
 
-    def close_cover(self, **kwargs):
+    @asyncio.coroutine
+    def async_close_cover(self, **kwargs):
         """Close the cover."""
         self._smartbridge.set_value(self._device_id, 0)
 
-    def open_cover(self, **kwargs):
+    @asyncio.coroutine
+    def async_open_cover(self, **kwargs):
         """Open the cover."""
         self._smartbridge.set_value(self._device_id, 100)
 
-    def set_cover_position(self, **kwargs):
+    @asyncio.coroutine
+    def async_set_cover_position(self, **kwargs):
         """Move the shade to a specific position."""
         if ATTR_POSITION in kwargs:
             position = kwargs[ATTR_POSITION]
             self._smartbridge.set_value(self._device_id, position)
 
-    def update(self):
+    @asyncio.coroutine
+    def async_update(self):
         """Call when forcing a refresh of the device."""
         self._state = self._smartbridge.get_device_by_id(self._device_id)
         _LOGGER.debug(self._state)
diff --git a/homeassistant/components/light/lutron_caseta.py b/homeassistant/components/light/lutron_caseta.py
index c11b3da6f75..e4e1baf6c58 100644
--- a/homeassistant/components/light/lutron_caseta.py
+++ b/homeassistant/components/light/lutron_caseta.py
@@ -4,6 +4,7 @@ Support for Lutron Caseta lights.
 For more details about this platform, please refer to the documentation at
 https://home-assistant.io/components/light.lutron_caseta/
 """
+import asyncio
 import logging
 
 from homeassistant.components.light import (
@@ -19,7 +20,8 @@ DEPENDENCIES = ['lutron_caseta']
 
 
 # pylint: disable=unused-argument
-def setup_platform(hass, config, add_devices, discovery_info=None):
+@asyncio.coroutine
+def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
     """Set up the Lutron Caseta lights."""
     devs = []
     bridge = hass.data[LUTRON_CASETA_SMARTBRIDGE]
@@ -28,7 +30,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
         dev = LutronCasetaLight(light_device, bridge)
         devs.append(dev)
 
-    add_devices(devs, True)
+    async_add_devices(devs, True)
 
 
 class LutronCasetaLight(LutronCasetaDevice, Light):
@@ -44,7 +46,8 @@ class LutronCasetaLight(LutronCasetaDevice, Light):
         """Return the brightness of the light."""
         return to_hass_level(self._state["current_state"])
 
-    def turn_on(self, **kwargs):
+    @asyncio.coroutine
+    def async_turn_on(self, **kwargs):
         """Turn the light on."""
         if ATTR_BRIGHTNESS in kwargs:
             brightness = kwargs[ATTR_BRIGHTNESS]
@@ -53,7 +56,8 @@ class LutronCasetaLight(LutronCasetaDevice, Light):
         self._smartbridge.set_value(self._device_id,
                                     to_lutron_level(brightness))
 
-    def turn_off(self, **kwargs):
+    @asyncio.coroutine
+    def async_turn_off(self, **kwargs):
         """Turn the light off."""
         self._smartbridge.set_value(self._device_id, 0)
 
@@ -62,7 +66,8 @@ class LutronCasetaLight(LutronCasetaDevice, Light):
         """Return true if device is on."""
         return self._state["current_state"] > 0
 
-    def update(self):
+    @asyncio.coroutine
+    def async_update(self):
         """Call when forcing a refresh of the device."""
         self._state = self._smartbridge.get_device_by_id(self._device_id)
         _LOGGER.debug(self._state)
diff --git a/homeassistant/components/lutron_caseta.py b/homeassistant/components/lutron_caseta.py
index 8660546c910..63f0315f35c 100644
--- a/homeassistant/components/lutron_caseta.py
+++ b/homeassistant/components/lutron_caseta.py
@@ -14,7 +14,7 @@ from homeassistant.const import CONF_HOST
 from homeassistant.helpers import discovery
 from homeassistant.helpers.entity import Entity
 
-REQUIREMENTS = ['pylutron-caseta==0.2.8']
+REQUIREMENTS = ['pylutron-caseta==0.3.0']
 
 _LOGGER = logging.getLogger(__name__)
 
@@ -22,9 +22,16 @@ LUTRON_CASETA_SMARTBRIDGE = 'lutron_smartbridge'
 
 DOMAIN = 'lutron_caseta'
 
+CONF_KEYFILE = 'keyfile'
+CONF_CERTFILE = 'certfile'
+CONF_CA_CERTS = 'ca_certs'
+
 CONFIG_SCHEMA = vol.Schema({
     DOMAIN: vol.Schema({
-        vol.Required(CONF_HOST): cv.string
+        vol.Required(CONF_HOST): cv.string,
+        vol.Required(CONF_KEYFILE): cv.string,
+        vol.Required(CONF_CERTFILE): cv.string,
+        vol.Required(CONF_CA_CERTS): cv.string
     })
 }, extra=vol.ALLOW_EXTRA)
 
@@ -33,14 +40,21 @@ LUTRON_CASETA_COMPONENTS = [
 ]
 
 
-def setup(hass, base_config):
+@asyncio.coroutine
+def async_setup(hass, base_config):
     """Set up the Lutron component."""
     from pylutron_caseta.smartbridge import Smartbridge
 
     config = base_config.get(DOMAIN)
-    hass.data[LUTRON_CASETA_SMARTBRIDGE] = Smartbridge(
-        hostname=config[CONF_HOST]
-    )
+    keyfile = hass.config.path(config[CONF_KEYFILE])
+    certfile = hass.config.path(config[CONF_CERTFILE])
+    ca_certs = hass.config.path(config[CONF_CA_CERTS])
+    bridge = Smartbridge.create_tls(hostname=config[CONF_HOST],
+                                    keyfile=keyfile,
+                                    certfile=certfile,
+                                    ca_certs=ca_certs)
+    hass.data[LUTRON_CASETA_SMARTBRIDGE] = bridge
+    yield from bridge.connect()
     if not hass.data[LUTRON_CASETA_SMARTBRIDGE].is_connected():
         _LOGGER.error("Unable to connect to Lutron smartbridge at %s",
                       config[CONF_HOST])
@@ -49,7 +63,8 @@ def setup(hass, base_config):
     _LOGGER.info("Connected to Lutron smartbridge at %s", config[CONF_HOST])
 
     for component in LUTRON_CASETA_COMPONENTS:
-        discovery.load_platform(hass, component, DOMAIN, {}, config)
+        hass.async_add_job(discovery.async_load_platform(hass, component,
+                                                         DOMAIN, {}, config))
 
     return True
 
@@ -73,13 +88,8 @@ class LutronCasetaDevice(Entity):
     @asyncio.coroutine
     def async_added_to_hass(self):
         """Register callbacks."""
-        self.hass.async_add_job(
-            self._smartbridge.add_subscriber, self._device_id,
-            self._update_callback
-        )
-
-    def _update_callback(self):
-        self.schedule_update_ha_state()
+        self._smartbridge.add_subscriber(self._device_id,
+                                         self.async_schedule_update_ha_state)
 
     @property
     def name(self):
diff --git a/homeassistant/components/scene/lutron_caseta.py b/homeassistant/components/scene/lutron_caseta.py
index 066be8c9d75..53df0da7617 100644
--- a/homeassistant/components/scene/lutron_caseta.py
+++ b/homeassistant/components/scene/lutron_caseta.py
@@ -4,6 +4,7 @@ Support for Lutron Caseta scenes.
 For more details about this platform, please refer to the documentation at
 https://home-assistant.io/components/scene.lutron_caseta/
 """
+import asyncio
 import logging
 
 from homeassistant.components.lutron_caseta import LUTRON_CASETA_SMARTBRIDGE
@@ -14,7 +15,8 @@ _LOGGER = logging.getLogger(__name__)
 DEPENDENCIES = ['lutron_caseta']
 
 
-def setup_platform(hass, config, add_devices, discovery_info=None):
+@asyncio.coroutine
+def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
     """Set up the Lutron Caseta lights."""
     devs = []
     bridge = hass.data[LUTRON_CASETA_SMARTBRIDGE]
@@ -23,7 +25,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
         dev = LutronCasetaScene(scenes[scene], bridge)
         devs.append(dev)
 
-    add_devices(devs, True)
+    async_add_devices(devs, True)
 
 
 class LutronCasetaScene(Scene):
@@ -50,6 +52,7 @@ class LutronCasetaScene(Scene):
         """There is no way of detecting if a scene is active (yet)."""
         return False
 
-    def activate(self, **kwargs):
+    @asyncio.coroutine
+    def async_activate(self, **kwargs):
         """Activate the scene."""
         self._bridge.activate_scene(self._scene_id)
diff --git a/homeassistant/components/switch/lutron_caseta.py b/homeassistant/components/switch/lutron_caseta.py
index daaba68dc5e..da36c76f41d 100644
--- a/homeassistant/components/switch/lutron_caseta.py
+++ b/homeassistant/components/switch/lutron_caseta.py
@@ -4,6 +4,7 @@ Support for Lutron Caseta switches.
 For more details about this platform, please refer to the documentation at
 https://home-assistant.io/components/sitch.lutron_caseta/
 """
+import asyncio
 import logging
 
 from homeassistant.components.lutron_caseta import (
@@ -16,7 +17,8 @@ DEPENDENCIES = ['lutron_caseta']
 
 
 # pylint: disable=unused-argument
-def setup_platform(hass, config, add_devices, discovery_info=None):
+@asyncio.coroutine
+def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
     """Set up Lutron switch."""
     devs = []
     bridge = hass.data[LUTRON_CASETA_SMARTBRIDGE]
@@ -26,18 +28,20 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
         dev = LutronCasetaLight(switch_device, bridge)
         devs.append(dev)
 
-    add_devices(devs, True)
+    async_add_devices(devs, True)
     return True
 
 
 class LutronCasetaLight(LutronCasetaDevice, SwitchDevice):
     """Representation of a Lutron Caseta switch."""
 
-    def turn_on(self, **kwargs):
+    @asyncio.coroutine
+    def async_turn_on(self, **kwargs):
         """Turn the switch on."""
         self._smartbridge.turn_on(self._device_id)
 
-    def turn_off(self, **kwargs):
+    @asyncio.coroutine
+    def async_turn_off(self, **kwargs):
         """Turn the switch off."""
         self._smartbridge.turn_off(self._device_id)
 
@@ -46,7 +50,8 @@ class LutronCasetaLight(LutronCasetaDevice, SwitchDevice):
         """Return true if device is on."""
         return self._state["current_state"] > 0
 
-    def update(self):
+    @asyncio.coroutine
+    def async_update(self):
         """Update when forcing a refresh of the device."""
         self._state = self._smartbridge.get_device_by_id(self._device_id)
         _LOGGER.debug(self._state)
diff --git a/requirements_all.txt b/requirements_all.txt
index 4b354a43225..dec8f96f39a 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -712,7 +712,7 @@ pylitejet==0.1
 pyloopenergy==0.0.17
 
 # homeassistant.components.lutron_caseta
-pylutron-caseta==0.2.8
+pylutron-caseta==0.3.0
 
 # homeassistant.components.lutron
 pylutron==0.1.0
-- 
GitLab