From fae8265a37944254181dea19f2f72fc178c16db4 Mon Sep 17 00:00:00 2001 From: zewelor Date: Fri, 29 Mar 2019 18:43:29 +0100 Subject: [PATCH] Fixes for yeelight availbility state (#22502) --- homeassistant/components/yeelight/__init__.py | 28 +++++-- homeassistant/components/yeelight/light.py | 81 ++++++++----------- 2 files changed, 55 insertions(+), 54 deletions(-) diff --git a/homeassistant/components/yeelight/__init__.py b/homeassistant/components/yeelight/__init__.py index 14b4656c403..fb218a67698 100644 --- a/homeassistant/components/yeelight/__init__.py +++ b/homeassistant/components/yeelight/__init__.py @@ -212,6 +212,7 @@ class YeelightDevice: self._name = config.get(CONF_NAME) self._model = config.get(CONF_MODEL) self._bulb_device = None + self._available = False @property def bulb(self): @@ -224,7 +225,9 @@ class YeelightDevice: # force init for type self.update() + self._available = True except yeelight.BulbException as ex: + self._available = False _LOGGER.error("Failed to connect to bulb %s, %s: %s", self._ipaddr, self._name, ex) @@ -245,10 +248,15 @@ class YeelightDevice: """Return ip address.""" return self._ipaddr + @property + def available(self): + """Return true is device is available.""" + return self._available + @property def is_nightlight_enabled(self) -> bool: """Return true / false if nightlight is currently enabled.""" - if self._bulb_device is None: + if self.bulb is None: return False return self.bulb.last_properties.get('active_mode') == '1' @@ -271,7 +279,7 @@ class YeelightDevice: light_type = yeelight.enums.LightType.Main try: - self._bulb_device.turn_on(duration=duration, light_type=light_type) + self.bulb.turn_on(duration=duration, light_type=light_type) except yeelight.BulbException as ex: _LOGGER.error("Unable to turn the bulb on: %s", ex) return @@ -284,16 +292,24 @@ class YeelightDevice: light_type = yeelight.enums.LightType.Main try: - self._bulb_device.turn_off(duration=duration, - light_type=light_type) + self.bulb.turn_off(duration=duration, light_type=light_type) except yeelight.BulbException as ex: - _LOGGER.error("Unable to turn the bulb on: %s", ex) + _LOGGER.error("Unable to turn the bulb off: %s", ex) return def update(self): """Read new properties from the device.""" + import yeelight + if not self.bulb: return - self._bulb_device.get_properties(UPDATE_REQUEST_PROPERTIES) + try: + self.bulb.get_properties(UPDATE_REQUEST_PROPERTIES) + self._available = True + except yeelight.BulbException as ex: + if self._available: # just inform once + _LOGGER.error("Unable to update bulb status: %s", ex) + self._available = False + dispatcher_send(self._hass, DATA_UPDATED, self._ipaddr) diff --git a/homeassistant/components/yeelight/light.py b/homeassistant/components/yeelight/light.py index cc3810c4968..92b668c6987 100644 --- a/homeassistant/components/yeelight/light.py +++ b/homeassistant/components/yeelight/light.py @@ -162,7 +162,6 @@ class YeelightLight(Light): self._device = device self._supported_features = SUPPORT_YEELIGHT - self._available = False self._brightness = None self._color_temp = None @@ -196,7 +195,7 @@ class YeelightLight(Light): @property def available(self) -> bool: """Return if bulb is available.""" - return self._available + return self.device.available @property def supported_features(self) -> int: @@ -304,14 +303,7 @@ class YeelightLight(Light): # F821: https://github.com/PyCQA/pyflakes/issues/373 @property def _bulb(self) -> 'yeelight.Bulb': # noqa: F821 - bulb = self.device.bulb - - if bulb: - self._available = True - return bulb - - self._available = False - return None + return self.device.bulb def set_music_mode(self, mode) -> None: """Set the music mode on or off.""" @@ -323,52 +315,45 @@ class YeelightLight(Light): def update(self) -> None: """Update properties from the bulb.""" import yeelight - try: - bulb_type = self._bulb.bulb_type - - if bulb_type == yeelight.BulbType.Color: - self._supported_features = SUPPORT_YEELIGHT_RGB - elif self.light_type == yeelight.enums.LightType.Ambient: - self._supported_features = SUPPORT_YEELIGHT_RGB - elif bulb_type in (yeelight.BulbType.WhiteTemp, - yeelight.BulbType.WhiteTempMood): - if self._is_nightlight_enabled: - self._supported_features = SUPPORT_YEELIGHT - else: - self._supported_features = SUPPORT_YEELIGHT_WHITE_TEMP - - if self.min_mireds is None: - model_specs = self._bulb.get_model_specs() - self._min_mireds = \ - kelvin_to_mired(model_specs['color_temp']['max']) - self._max_mireds = \ - kelvin_to_mired(model_specs['color_temp']['min']) - - if bulb_type == yeelight.BulbType.WhiteTempMood: - self._is_on = self._get_property('main_power') == 'on' - else: - self._is_on = self._get_property('power') == 'on' + bulb_type = self._bulb.bulb_type + if bulb_type == yeelight.BulbType.Color: + self._supported_features = SUPPORT_YEELIGHT_RGB + elif self.light_type == yeelight.enums.LightType.Ambient: + self._supported_features = SUPPORT_YEELIGHT_RGB + elif bulb_type in (yeelight.BulbType.WhiteTemp, + yeelight.BulbType.WhiteTempMood): if self._is_nightlight_enabled: - bright = self._get_property('nl_br', None) + self._supported_features = SUPPORT_YEELIGHT else: - bright = self._get_property('bright', None) + self._supported_features = SUPPORT_YEELIGHT_WHITE_TEMP - if bright: - self._brightness = round(255 * (int(bright) / 100)) + if self.min_mireds is None: + model_specs = self._bulb.get_model_specs() + self._min_mireds = \ + kelvin_to_mired(model_specs['color_temp']['max']) + self._max_mireds = \ + kelvin_to_mired(model_specs['color_temp']['min']) - temp_in_k = self._get_property('ct') + if bulb_type == yeelight.BulbType.WhiteTempMood: + self._is_on = self._get_property('main_power') == 'on' + else: + self._is_on = self._get_property('power') == 'on' - if temp_in_k: - self._color_temp = kelvin_to_mired(int(temp_in_k)) + if self._is_nightlight_enabled: + bright = self._get_property('nl_br') + else: + bright = self._get_property('bright') - self._hs = self._get_hs_from_properties() + if bright: + self._brightness = round(255 * (int(bright) / 100)) - self._available = True - except yeelight.BulbException as ex: - if self._available: # just inform once - _LOGGER.error("Unable to update bulb status: %s", ex) - self._available = False + temp_in_k = self._get_property('ct') + + if temp_in_k: + self._color_temp = kelvin_to_mired(int(temp_in_k)) + + self._hs = self._get_hs_from_properties() @_cmd def set_brightness(self, brightness, duration) -> None: