Update avion.py (#8364)
* Update avion.py * Update decora.py * Update decora.py * Update decora.py * Update avion.py * Update decora.py * Update decora.py * Update decora.py * Update decora.py
This commit is contained in:
parent
fb184b4b6f
commit
e12a9eaadd
2 changed files with 39 additions and 63 deletions
|
@ -17,10 +17,6 @@ from homeassistant.components.light import (
|
|||
PLATFORM_SCHEMA)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
# pylint: disable=import-error
|
||||
|
||||
AVION_EXCEPTION = None
|
||||
|
||||
REQUIREMENTS = ['avion==0.7']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -41,12 +37,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up an Avion switch."""
|
||||
global AVION_EXCEPTION
|
||||
|
||||
# pylint: disable=import-error
|
||||
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])
|
||||
|
@ -77,6 +70,7 @@ class AvionLight(Light):
|
|||
|
||||
def __init__(self, device):
|
||||
"""Initialize the light."""
|
||||
# pylint: disable=import-error
|
||||
import avion
|
||||
|
||||
self._name = device['name']
|
||||
|
@ -123,6 +117,9 @@ class AvionLight(Light):
|
|||
|
||||
def set_state(self, brightness):
|
||||
"""Set the state of this lamp to the provided brightness."""
|
||||
# pylint: disable=import-error
|
||||
import avion
|
||||
|
||||
# Bluetooth LE is unreliable, and the connection may drop at any
|
||||
# time. Make an effort to re-establish the link.
|
||||
initial = time.monotonic()
|
||||
|
@ -132,7 +129,7 @@ class AvionLight(Light):
|
|||
try:
|
||||
self._switch.set_brightness(brightness)
|
||||
break
|
||||
except AVION_EXCEPTION:
|
||||
except avion.avionException:
|
||||
self._switch.connect()
|
||||
return True
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ For more details about this platform, please refer to the documentation at
|
|||
https://home-assistant.io/components/light.decora/
|
||||
"""
|
||||
import logging
|
||||
from functools import wraps
|
||||
import time
|
||||
|
||||
import voluptuous as vol
|
||||
|
@ -15,10 +16,6 @@ from homeassistant.components.light import (
|
|||
PLATFORM_SCHEMA)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
# pylint: disable=import-error
|
||||
|
||||
DECORA_EXCEPTION = None
|
||||
|
||||
REQUIREMENTS = ['decora==0.6']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -35,14 +32,28 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up an Decora switch."""
|
||||
global DECORA_EXCEPTION
|
||||
|
||||
def retry(method):
|
||||
"""Retry bluetooth commands."""
|
||||
@wraps(method)
|
||||
def wrapper_retry(device, *args, **kwds):
|
||||
"""Try send command and retry on error."""
|
||||
# pylint: disable=import-error
|
||||
import decora
|
||||
|
||||
DECORA_EXCEPTION = decora.decoraException
|
||||
initial = time.monotonic()
|
||||
while True:
|
||||
if time.monotonic() - initial >= 10:
|
||||
return None
|
||||
try:
|
||||
return method(device, *args, **kwds)
|
||||
except (decora.decoraException, AttributeError):
|
||||
# pylint: disable=protected-access
|
||||
device._switch.connect()
|
||||
return wrapper_retry
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up an Decora switch."""
|
||||
lights = []
|
||||
for address, device_config in config[CONF_DEVICES].items():
|
||||
device = {}
|
||||
|
@ -60,6 +71,7 @@ class DecoraLight(Light):
|
|||
|
||||
def __init__(self, device):
|
||||
"""Initialize the light."""
|
||||
# pylint: disable=import-error
|
||||
import decora
|
||||
|
||||
self._name = device['name']
|
||||
|
@ -82,13 +94,11 @@ class DecoraLight(Light):
|
|||
@property
|
||||
def is_on(self):
|
||||
"""Return true if device is on."""
|
||||
self.update()
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
"""Return the brightness of this light between 0..255."""
|
||||
self.update()
|
||||
return self._brightness
|
||||
|
||||
@property
|
||||
|
@ -106,61 +116,30 @@ class DecoraLight(Light):
|
|||
"""We can read the actual state."""
|
||||
return False
|
||||
|
||||
@retry
|
||||
def set_state(self, brightness):
|
||||
"""Set the state of this lamp to the provided brightness."""
|
||||
initial = time.monotonic()
|
||||
while True:
|
||||
if time.monotonic() - initial >= 10:
|
||||
return None
|
||||
try:
|
||||
self._switch.set_brightness(brightness / 2.55)
|
||||
break
|
||||
except (DECORA_EXCEPTION, AttributeError):
|
||||
self._switch.connect()
|
||||
|
||||
self._brightness = brightness
|
||||
return True
|
||||
|
||||
@retry
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the specified or all lights on."""
|
||||
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
||||
|
||||
initial = time.monotonic()
|
||||
while True:
|
||||
if time.monotonic() - initial >= 10:
|
||||
return None
|
||||
try:
|
||||
self._switch.on()
|
||||
self._state = True
|
||||
break
|
||||
except (DECORA_EXCEPTION, AttributeError):
|
||||
self._switch.connect()
|
||||
|
||||
if brightness is not None:
|
||||
self.set_state(brightness)
|
||||
|
||||
@retry
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the specified or all lights off."""
|
||||
initial = time.monotonic()
|
||||
while True:
|
||||
if time.monotonic() - initial >= 10:
|
||||
return None
|
||||
try:
|
||||
self._switch.off()
|
||||
self._state = False
|
||||
break
|
||||
except (DECORA_EXCEPTION, AttributeError):
|
||||
self._switch.connect()
|
||||
|
||||
@retry
|
||||
def update(self):
|
||||
"""Synchronise internal state with the actual light state."""
|
||||
initial = time.monotonic()
|
||||
while True:
|
||||
if time.monotonic() - initial >= 10:
|
||||
return None
|
||||
try:
|
||||
self._brightness = self._switch.get_brightness() * 2.55
|
||||
self._state = self._switch.get_on()
|
||||
break
|
||||
except (DECORA_EXCEPTION, AttributeError):
|
||||
self._switch.connect()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue