From cac8e3484144ff3293f32681b53c4496cad1924b Mon Sep 17 00:00:00 2001 From: Marco Orovecchia Date: Wed, 13 Mar 2019 20:54:15 +0100 Subject: [PATCH] Nanoleaf availability check (#21945) * Added availability check for nanoleaf lights * pylint errors fixed * pynanoleaf bump --- homeassistant/components/light/nanoleaf.py | 34 +++++++++++++++------- requirements_all.txt | 2 +- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/light/nanoleaf.py b/homeassistant/components/light/nanoleaf.py index 571f4efc4ad..bd34c535b70 100644 --- a/homeassistant/components/light/nanoleaf.py +++ b/homeassistant/components/light/nanoleaf.py @@ -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 diff --git a/requirements_all.txt b/requirements_all.txt index 3d2cc183564..584330b833b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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