Make less imports from yeelight (#23124)

This commit is contained in:
zewelor 2019-04-17 22:05:49 +02:00 committed by Fabian Affolter
parent 0afa01609c
commit fa73b8e37a
2 changed files with 12 additions and 14 deletions

View file

@ -184,15 +184,14 @@ class YeelightDevice:
def bulb(self):
"""Return bulb device."""
if self._bulb_device is None:
import yeelight
from yeelight import Bulb, BulbException
try:
self._bulb_device = yeelight.Bulb(self._ipaddr,
model=self._model)
self._bulb_device = Bulb(self._ipaddr, model=self._model)
# force init for type
self.update()
self._available = True
except yeelight.BulbException as ex:
except BulbException as ex:
self._available = False
_LOGGER.error("Failed to connect to bulb %s, %s: %s",
self._ipaddr, self._name, ex)

View file

@ -92,12 +92,12 @@ def _transitions_config_parser(transitions):
def _parse_custom_effects(effects_config):
import yeelight
from yeelight import Flow
effects = {}
for config in effects_config:
params = config[CONF_FLOW_PARAMS]
action = yeelight.Flow.actions[params[ATTR_ACTION]]
action = Flow.actions[params[ATTR_ACTION]]
transitions = _transitions_config_parser(
params[ATTR_TRANSITIONS])
@ -113,11 +113,11 @@ def _parse_custom_effects(effects_config):
def _cmd(func):
"""Define a wrapper to catch exceptions from the bulb."""
def _wrap(self, *args, **kwargs):
import yeelight
from yeelight import BulbException
try:
_LOGGER.debug("Calling %s with %s %s", func, args, kwargs)
return func(self, *args, **kwargs)
except yeelight.BulbException as ex:
except BulbException as ex:
_LOGGER.error("Error when calling %s: %s", func, ex)
return _wrap
@ -347,15 +347,14 @@ class YeelightLight(Light):
def update(self) -> None:
"""Update properties from the bulb."""
import yeelight
from yeelight import BulbType, enums
bulb_type = self._bulb.bulb_type
if bulb_type == yeelight.BulbType.Color:
if bulb_type == BulbType.Color:
self._supported_features = SUPPORT_YEELIGHT_RGB
elif self.light_type == yeelight.enums.LightType.Ambient:
elif self.light_type == enums.LightType.Ambient:
self._supported_features = SUPPORT_YEELIGHT_RGB
elif bulb_type in (yeelight.BulbType.WhiteTemp,
yeelight.BulbType.WhiteTempMood):
elif bulb_type in (BulbType.WhiteTemp, BulbType.WhiteTempMood):
if self._is_nightlight_enabled:
self._supported_features = SUPPORT_YEELIGHT
else:
@ -368,7 +367,7 @@ class YeelightLight(Light):
self._max_mireds = \
kelvin_to_mired(model_specs['color_temp']['min'])
if bulb_type == yeelight.BulbType.WhiteTempMood:
if bulb_type == BulbType.WhiteTempMood:
self._is_on = self._get_property('main_power') == 'on'
else:
self._is_on = self._get_property('power') == 'on'