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): def bulb(self):
"""Return bulb device.""" """Return bulb device."""
if self._bulb_device is None: if self._bulb_device is None:
import yeelight from yeelight import Bulb, BulbException
try: try:
self._bulb_device = yeelight.Bulb(self._ipaddr, self._bulb_device = Bulb(self._ipaddr, model=self._model)
model=self._model)
# force init for type # force init for type
self.update() self.update()
self._available = True self._available = True
except yeelight.BulbException as ex: except BulbException as ex:
self._available = False self._available = False
_LOGGER.error("Failed to connect to bulb %s, %s: %s", _LOGGER.error("Failed to connect to bulb %s, %s: %s",
self._ipaddr, self._name, ex) self._ipaddr, self._name, ex)

View file

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