Update Avion and Decora switches to match upstream changes (#7903)

The upstream Avion and Decora code has changed to punt retry logic out to
consumers, so update to match. As a bonus, Avion also gains support for
pulling switch configuration off the web rather than requiring manual
configuration.
This commit is contained in:
Matthew Garrett 2017-07-04 15:39:51 -07:00 committed by Pascal Vizeli
parent 23fc5e2c9f
commit 061a38cc3b
3 changed files with 101 additions and 25 deletions

View file

@ -5,16 +5,23 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.avion/
"""
import logging
import time
import voluptuous as vol
from homeassistant.const import CONF_API_KEY, CONF_DEVICES, CONF_NAME
from homeassistant.const import CONF_API_KEY, CONF_DEVICES, CONF_NAME, \
CONF_USERNAME, CONF_PASSWORD
from homeassistant.components.light import (
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light,
PLATFORM_SCHEMA)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['avion==0.6']
# pylint: disable=import-error
AVION_EXCEPTION = None
REQUIREMENTS = ['avion==0.7']
_LOGGER = logging.getLogger(__name__)
@ -27,20 +34,40 @@ DEVICE_SCHEMA = vol.Schema({
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_DEVICES, default={}): {cv.string: DEVICE_SCHEMA},
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up an Avion switch."""
global AVION_EXCEPTION
import avion
AVION_EXCEPTION = avion.avionException
lights = []
if CONF_USERNAME in config and CONF_PASSWORD in config:
data = avion.avion_info(config[CONF_USERNAME], config[CONF_PASSWORD])
for location in data['locations']:
for avion_device in location['location']['devices']:
device = {}
mac = avion_device['device']['mac_address']
device['name'] = avion_device['device']['name']
device['key'] = location['location']['passphrase']
device['address'] = '%s%s:%s%s:%s%s:%s%s:%s%s:%s%s' % \
(mac[8], mac[9], mac[10], mac[11], mac[4],
mac[5], mac[6], mac[7], mac[0], mac[1],
mac[2], mac[3])
lights.append(AvionLight(device))
for address, device_config in config[CONF_DEVICES].items():
device = {}
device['name'] = device_config[CONF_NAME]
device['key'] = device_config[CONF_API_KEY]
device['address'] = address
light = AvionLight(device)
if light.is_valid:
lights.append(light)
lights.append(AvionLight(device))
add_devices(lights)
@ -50,7 +77,6 @@ class AvionLight(Light):
def __init__(self, device):
"""Initialize the light."""
# pylint: disable=import-error
import avion
self._name = device['name']
@ -59,8 +85,6 @@ class AvionLight(Light):
self._brightness = 255
self._state = False
self._switch = avion.avion(self._address, self._key)
self._switch.connect()
self.is_valid = True
@property
def unique_id(self):
@ -99,7 +123,17 @@ class AvionLight(Light):
def set_state(self, brightness):
"""Set the state of this lamp to the provided brightness."""
self._switch.set_brightness(brightness)
# Bluetooth LE is unreliable, and the connection may drop at any
# time. Make an effort to re-establish the link.
initial = time.monotonic()
while True:
if time.monotonic() - initial >= 10:
return False
try:
self._switch.set_brightness(brightness)
break
except AVION_EXCEPTION:
self._switch.connect()
return True
def turn_on(self, **kwargs):