light.yeelight: catch i/o related exceptions from the backend lib (#6952)

Fixes/mitigates problems with #5949 and #6624
This commit is contained in:
Teemu R 2017-04-07 07:41:47 +02:00 committed by Paulus Schoutsen
parent 9254e7e862
commit d952a07658

View File

@ -255,7 +255,8 @@ class YeelightLight(Light):
def set_flash(self, flash) -> None: def set_flash(self, flash) -> None:
"""Activate flash.""" """Activate flash."""
if flash: if flash:
from yeelight import RGBTransition, SleepTransition, Flow from yeelight import (RGBTransition, SleepTransition, Flow,
BulbException)
if self._bulb.last_properties["color_mode"] != 1: if self._bulb.last_properties["color_mode"] != 1:
_LOGGER.error("Flash supported currently only in RGB mode.") _LOGGER.error("Flash supported currently only in RGB mode.")
return return
@ -280,10 +281,14 @@ class YeelightLight(Light):
duration=duration)) duration=duration))
flow = Flow(count=count, transitions=transitions) flow = Flow(count=count, transitions=transitions)
self._bulb.start_flow(flow) try:
self._bulb.start_flow(flow)
except BulbException as ex:
_LOGGER.error("Unable to set flash: %s", ex)
def turn_on(self, **kwargs) -> None: def turn_on(self, **kwargs) -> None:
"""Turn the bulb on.""" """Turn the bulb on."""
import yeelight
brightness = kwargs.get(ATTR_BRIGHTNESS) brightness = kwargs.get(ATTR_BRIGHTNESS)
colortemp = kwargs.get(ATTR_COLOR_TEMP) colortemp = kwargs.get(ATTR_COLOR_TEMP)
rgb = kwargs.get(ATTR_RGB_COLOR) rgb = kwargs.get(ATTR_RGB_COLOR)
@ -293,22 +298,43 @@ class YeelightLight(Light):
if ATTR_TRANSITION in kwargs: # passed kwarg overrides config if ATTR_TRANSITION in kwargs: # passed kwarg overrides config
duration = int(kwargs.get(ATTR_TRANSITION) * 1000) # kwarg in s duration = int(kwargs.get(ATTR_TRANSITION) * 1000) # kwarg in s
self._bulb.turn_on(duration=duration) try:
self._bulb.turn_on(duration=duration)
except yeelight.BulbException as ex:
_LOGGER.error("Unable to turn the bulb on: %s", ex)
return
if self.config[CONF_MODE_MUSIC] and not self._bulb.music_mode: if self.config[CONF_MODE_MUSIC] and not self._bulb.music_mode:
self.set_music_mode(self.config[CONF_MODE_MUSIC]) try:
self.set_music_mode(self.config[CONF_MODE_MUSIC])
except yeelight.BulbException as ex:
_LOGGER.error("Unable to turn on music mode,"
"consider disabling it: %s", ex)
# values checked for none in methods try:
self.set_rgb(rgb, duration) # values checked for none in methods
self.set_colortemp(colortemp, duration) self.set_rgb(rgb, duration)
self.set_brightness(brightness, duration) self.set_colortemp(colortemp, duration)
self.set_flash(flash) self.set_brightness(brightness, duration)
self.set_flash(flash)
except yeelight.BulbException as ex:
_LOGGER.error("Unable to set bulb properties: %s", ex)
return
# save the current state if we had a manual change. # save the current state if we had a manual change.
if self.config[CONF_SAVE_ON_CHANGE]: if self.config[CONF_SAVE_ON_CHANGE] and (brightness
if brightness or colortemp or rgb: or colortemp
or rgb):
try:
self.set_default() self.set_default()
except yeelight.BulbException as ex:
_LOGGER.error("Unable to set the defaults: %s", ex)
return
def turn_off(self, **kwargs) -> None: def turn_off(self, **kwargs) -> None:
"""Turn off.""" """Turn off."""
self._bulb.turn_off() import yeelight
try:
self._bulb.turn_off()
except yeelight.BulbException as ex:
_LOGGER.error("Unable to turn the bulb off: %s", ex)