Improve yeelight imports (#24020)

* Improve yeelight imports

* Move import on top

* Fix lint
pull/22151/head^2
zewelor 2019-05-22 04:47:10 +02:00 committed by Teemu R
parent 636077c74d
commit fdf1fa48e3
2 changed files with 12 additions and 34 deletions

View File

@ -4,6 +4,7 @@ import logging
from datetime import timedelta
import voluptuous as vol
from yeelight import Bulb, BulbException
from homeassistant.components.discovery import SERVICE_YEELIGHT
from homeassistant.const import CONF_DEVICES, CONF_NAME, CONF_SCAN_INTERVAL, \
CONF_HOST, ATTR_ENTITY_ID
@ -184,7 +185,6 @@ class YeelightDevice:
def bulb(self):
"""Return bulb device."""
if self._bulb_device is None:
from yeelight import Bulb, BulbException
try:
self._bulb_device = Bulb(self._ipaddr, model=self._model)
# force init for type
@ -238,8 +238,6 @@ class YeelightDevice:
def turn_on(self, duration=DEFAULT_TRANSITION, light_type=None):
"""Turn on device."""
from yeelight import BulbException
try:
self.bulb.turn_on(duration=duration, light_type=light_type)
except BulbException as ex:
@ -248,8 +246,6 @@ class YeelightDevice:
def turn_off(self, duration=DEFAULT_TRANSITION, light_type=None):
"""Turn off device."""
from yeelight import BulbException
try:
self.bulb.turn_off(duration=duration, light_type=light_type)
except BulbException as ex:
@ -258,8 +254,6 @@ class YeelightDevice:
def update(self):
"""Read new properties from the device."""
from yeelight import BulbException
if not self.bulb:
return

View File

@ -2,6 +2,8 @@
import logging
import voluptuous as vol
from yeelight import (RGBTransition, SleepTransition, Flow, BulbException)
from yeelight.enums import PowerMode, LightType, BulbType
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.service import extract_entity_ids
from homeassistant.util.color import (
@ -92,8 +94,6 @@ def _transitions_config_parser(transitions):
def _parse_custom_effects(effects_config):
from yeelight import Flow
effects = {}
for config in effects_config:
params = config[CONF_FLOW_PARAMS]
@ -113,7 +113,6 @@ def _parse_custom_effects(effects_config):
def _cmd(func):
"""Define a wrapper to catch exceptions from the bulb."""
def _wrap(self, *args, **kwargs):
from yeelight import BulbException
try:
_LOGGER.debug("Calling %s with %s %s", func, args, kwargs)
return func(self, *args, **kwargs)
@ -125,8 +124,6 @@ def _cmd(func):
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Yeelight bulbs."""
from yeelight.enums import PowerMode
data_key = '{}_lights'.format(DATA_YEELIGHT)
if not discovery_info:
@ -187,8 +184,6 @@ class YeelightLight(Light):
def __init__(self, device, custom_effects=None):
"""Initialize the Yeelight light."""
from yeelight.enums import LightType
self.config = device.config
self._device = device
@ -347,12 +342,11 @@ class YeelightLight(Light):
def update(self) -> None:
"""Update properties from the bulb."""
from yeelight import BulbType, enums
bulb_type = self._bulb.bulb_type
if bulb_type == BulbType.Color:
self._supported_features = SUPPORT_YEELIGHT_RGB
elif self.light_type == enums.LightType.Ambient:
elif self.light_type == LightType.Ambient:
self._supported_features = SUPPORT_YEELIGHT_RGB
elif bulb_type in (BulbType.WhiteTemp, BulbType.WhiteTempMood):
if self._is_nightlight_enabled:
@ -423,8 +417,6 @@ class YeelightLight(Light):
def set_flash(self, flash) -> None:
"""Activate flash."""
if flash:
from yeelight import (RGBTransition, SleepTransition, Flow,
BulbException)
if self._bulb.last_properties["color_mode"] != 1:
_LOGGER.error("Flash supported currently only in RGB mode.")
return
@ -458,7 +450,6 @@ class YeelightLight(Light):
def set_effect(self, effect) -> None:
"""Activate effect."""
if effect:
from yeelight import (Flow, BulbException)
from yeelight.transitions import (disco, temp, strobe, pulse,
strobe_color, alarm, police,
police2, christmas, rgb,
@ -502,7 +493,6 @@ class YeelightLight(Light):
def turn_on(self, **kwargs) -> None:
"""Turn the bulb on."""
import yeelight
brightness = kwargs.get(ATTR_BRIGHTNESS)
colortemp = kwargs.get(ATTR_COLOR_TEMP)
hs_color = kwargs.get(ATTR_HS_COLOR)
@ -519,7 +509,7 @@ class YeelightLight(Light):
if self.config[CONF_MODE_MUSIC] and not self._bulb.music_mode:
try:
self.set_music_mode(self.config[CONF_MODE_MUSIC])
except yeelight.BulbException as ex:
except BulbException as ex:
_LOGGER.error("Unable to turn on music mode,"
"consider disabling it: %s", ex)
@ -530,7 +520,7 @@ class YeelightLight(Light):
self.set_brightness(brightness, duration)
self.set_flash(flash)
self.set_effect(effect)
except yeelight.BulbException as ex:
except BulbException as ex:
_LOGGER.error("Unable to set bulb properties: %s", ex)
return
@ -540,7 +530,7 @@ class YeelightLight(Light):
or rgb):
try:
self.set_default()
except yeelight.BulbException as ex:
except BulbException as ex:
_LOGGER.error("Unable to set the defaults: %s", ex)
return
self.device.update()
@ -556,27 +546,23 @@ class YeelightLight(Light):
def set_mode(self, mode: str):
"""Set a power mode."""
import yeelight
try:
self._bulb.set_power_mode(yeelight.enums.PowerMode[mode.upper()])
self._bulb.set_power_mode(PowerMode[mode.upper()])
self.device.update()
except yeelight.BulbException as ex:
except BulbException as ex:
_LOGGER.error("Unable to set the power mode: %s", ex)
def start_flow(self, transitions, count=0, action=ACTION_RECOVER):
"""Start flow."""
import yeelight
try:
flow = yeelight.Flow(
flow = Flow(
count=count,
action=yeelight.Flow.actions[action],
action=Flow.actions[action],
transitions=transitions)
self._bulb.start_flow(flow, light_type=self.light_type)
self.device.update()
except yeelight.BulbException as ex:
except BulbException as ex:
_LOGGER.error("Unable to set effect: %s", ex)
@ -590,8 +576,6 @@ class YeelightAmbientLight(YeelightLight):
def __init__(self, *args, **kwargs):
"""Initialize the Yeelight Ambient light."""
from yeelight.enums import LightType
super().__init__(*args, **kwargs)
self._min_mireds = kelvin_to_mired(6500)
self._max_mireds = kelvin_to_mired(1700)