Nanoleaf availability check (#21945)

* Added availability check for nanoleaf lights

* pylint errors fixed

* pynanoleaf bump
This commit is contained in:
Marco Orovecchia 2019-03-13 20:54:15 +01:00 committed by Paulus Schoutsen
parent deb66bb748
commit cac8e34841
2 changed files with 25 additions and 11 deletions

View file

@ -19,7 +19,7 @@ from homeassistant.util.color import \
color_temperature_mired_to_kelvin as mired_to_kelvin
from homeassistant.util.json import load_json, save_json
REQUIREMENTS = ['pynanoleaf==0.0.2']
REQUIREMENTS = ['pynanoleaf==0.0.5']
_LOGGER = logging.getLogger(__name__)
@ -43,7 +43,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Nanoleaf light."""
import pynanoleaf
from pynanoleaf import Nanoleaf, Unavailable
if DATA_NANOLEAF not in hass.data:
hass.data[DATA_NANOLEAF] = dict()
@ -63,7 +63,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
name = config[CONF_NAME]
token = config[CONF_TOKEN]
nanoleaf_light = pynanoleaf.Nanoleaf(host)
nanoleaf_light = Nanoleaf(host)
if not token:
token = nanoleaf_light.request_token()
@ -78,7 +78,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
nanoleaf_light.token = token
if nanoleaf_light.on is None:
try:
nanoleaf_light.available
except Unavailable:
_LOGGER.error(
"Could not connect to Nanoleaf Light: %s on %s", name, host)
return
@ -92,6 +94,7 @@ class NanoleafLight(Light):
def __init__(self, light, name):
"""Initialize an Nanoleaf light."""
self._available = True
self._brightness = None
self._color_temp = None
self._effect = None
@ -101,6 +104,11 @@ class NanoleafLight(Light):
self._hs_color = None
self._state = None
@property
def available(self):
"""Return availability."""
return self._available
@property
def brightness(self):
"""Return the brightness of the light."""
@ -187,9 +195,15 @@ class NanoleafLight(Light):
def update(self):
"""Fetch new state data for this light."""
self._brightness = self._light.brightness
self._color_temp = self._light.color_temperature
self._effect = self._light.effect
self._effects_list = self._light.effects
self._hs_color = self._light.hue, self._light.saturation
self._state = self._light.on
try:
self._available = self._light.available
self._brightness = self._light.brightness
self._color_temp = self._light.color_temperature
self._effect = self._light.effect
self._effects_list = self._light.effects
self._hs_color = self._light.hue, self._light.saturation
self._state = self._light.on
except Exception as err: # pylint:disable=broad-except
_LOGGER.error("Could not update status for %s (%s)",
self.name, err)
self._available = False

View file

@ -1174,7 +1174,7 @@ pymyq==1.1.0
pymysensors==0.18.0
# homeassistant.components.light.nanoleaf
pynanoleaf==0.0.2
pynanoleaf==0.0.5
# homeassistant.components.lock.nello
pynello==2.0.2