Improve yeelight imports (#24020)
* Improve yeelight imports * Move import on top * Fix lintpull/22151/head^2
parent
636077c74d
commit
fdf1fa48e3
|
@ -4,6 +4,7 @@ import logging
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
from yeelight import Bulb, BulbException
|
||||||
from homeassistant.components.discovery import SERVICE_YEELIGHT
|
from homeassistant.components.discovery import SERVICE_YEELIGHT
|
||||||
from homeassistant.const import CONF_DEVICES, CONF_NAME, CONF_SCAN_INTERVAL, \
|
from homeassistant.const import CONF_DEVICES, CONF_NAME, CONF_SCAN_INTERVAL, \
|
||||||
CONF_HOST, ATTR_ENTITY_ID
|
CONF_HOST, ATTR_ENTITY_ID
|
||||||
|
@ -184,7 +185,6 @@ 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:
|
||||||
from yeelight import Bulb, BulbException
|
|
||||||
try:
|
try:
|
||||||
self._bulb_device = Bulb(self._ipaddr, model=self._model)
|
self._bulb_device = Bulb(self._ipaddr, model=self._model)
|
||||||
# force init for type
|
# force init for type
|
||||||
|
@ -238,8 +238,6 @@ class YeelightDevice:
|
||||||
|
|
||||||
def turn_on(self, duration=DEFAULT_TRANSITION, light_type=None):
|
def turn_on(self, duration=DEFAULT_TRANSITION, light_type=None):
|
||||||
"""Turn on device."""
|
"""Turn on device."""
|
||||||
from yeelight import BulbException
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.bulb.turn_on(duration=duration, light_type=light_type)
|
self.bulb.turn_on(duration=duration, light_type=light_type)
|
||||||
except BulbException as ex:
|
except BulbException as ex:
|
||||||
|
@ -248,8 +246,6 @@ class YeelightDevice:
|
||||||
|
|
||||||
def turn_off(self, duration=DEFAULT_TRANSITION, light_type=None):
|
def turn_off(self, duration=DEFAULT_TRANSITION, light_type=None):
|
||||||
"""Turn off device."""
|
"""Turn off device."""
|
||||||
from yeelight import BulbException
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.bulb.turn_off(duration=duration, light_type=light_type)
|
self.bulb.turn_off(duration=duration, light_type=light_type)
|
||||||
except BulbException as ex:
|
except BulbException as ex:
|
||||||
|
@ -258,8 +254,6 @@ class YeelightDevice:
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Read new properties from the device."""
|
"""Read new properties from the device."""
|
||||||
from yeelight import BulbException
|
|
||||||
|
|
||||||
if not self.bulb:
|
if not self.bulb:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
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.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.service import extract_entity_ids
|
from homeassistant.helpers.service import extract_entity_ids
|
||||||
from homeassistant.util.color import (
|
from homeassistant.util.color import (
|
||||||
|
@ -92,8 +94,6 @@ def _transitions_config_parser(transitions):
|
||||||
|
|
||||||
|
|
||||||
def _parse_custom_effects(effects_config):
|
def _parse_custom_effects(effects_config):
|
||||||
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]
|
||||||
|
@ -113,7 +113,6 @@ 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):
|
||||||
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)
|
||||||
|
@ -125,8 +124,6 @@ def _cmd(func):
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the Yeelight bulbs."""
|
"""Set up the Yeelight bulbs."""
|
||||||
from yeelight.enums import PowerMode
|
|
||||||
|
|
||||||
data_key = '{}_lights'.format(DATA_YEELIGHT)
|
data_key = '{}_lights'.format(DATA_YEELIGHT)
|
||||||
|
|
||||||
if not discovery_info:
|
if not discovery_info:
|
||||||
|
@ -187,8 +184,6 @@ class YeelightLight(Light):
|
||||||
|
|
||||||
def __init__(self, device, custom_effects=None):
|
def __init__(self, device, custom_effects=None):
|
||||||
"""Initialize the Yeelight light."""
|
"""Initialize the Yeelight light."""
|
||||||
from yeelight.enums import LightType
|
|
||||||
|
|
||||||
self.config = device.config
|
self.config = device.config
|
||||||
self._device = device
|
self._device = device
|
||||||
|
|
||||||
|
@ -347,12 +342,11 @@ class YeelightLight(Light):
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Update properties from the bulb."""
|
"""Update properties from the bulb."""
|
||||||
from yeelight import BulbType, enums
|
|
||||||
bulb_type = self._bulb.bulb_type
|
bulb_type = self._bulb.bulb_type
|
||||||
|
|
||||||
if bulb_type == BulbType.Color:
|
if bulb_type == BulbType.Color:
|
||||||
self._supported_features = SUPPORT_YEELIGHT_RGB
|
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
|
self._supported_features = SUPPORT_YEELIGHT_RGB
|
||||||
elif bulb_type in (BulbType.WhiteTemp, BulbType.WhiteTempMood):
|
elif bulb_type in (BulbType.WhiteTemp, BulbType.WhiteTempMood):
|
||||||
if self._is_nightlight_enabled:
|
if self._is_nightlight_enabled:
|
||||||
|
@ -423,8 +417,6 @@ 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,
|
|
||||||
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
|
||||||
|
@ -458,7 +450,6 @@ class YeelightLight(Light):
|
||||||
def set_effect(self, effect) -> None:
|
def set_effect(self, effect) -> None:
|
||||||
"""Activate effect."""
|
"""Activate effect."""
|
||||||
if effect:
|
if effect:
|
||||||
from yeelight import (Flow, BulbException)
|
|
||||||
from yeelight.transitions import (disco, temp, strobe, pulse,
|
from yeelight.transitions import (disco, temp, strobe, pulse,
|
||||||
strobe_color, alarm, police,
|
strobe_color, alarm, police,
|
||||||
police2, christmas, rgb,
|
police2, christmas, rgb,
|
||||||
|
@ -502,7 +493,6 @@ class YeelightLight(Light):
|
||||||
|
|
||||||
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)
|
||||||
hs_color = kwargs.get(ATTR_HS_COLOR)
|
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:
|
if self.config[CONF_MODE_MUSIC] and not self._bulb.music_mode:
|
||||||
try:
|
try:
|
||||||
self.set_music_mode(self.config[CONF_MODE_MUSIC])
|
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,"
|
_LOGGER.error("Unable to turn on music mode,"
|
||||||
"consider disabling it: %s", ex)
|
"consider disabling it: %s", ex)
|
||||||
|
|
||||||
|
@ -530,7 +520,7 @@ class YeelightLight(Light):
|
||||||
self.set_brightness(brightness, duration)
|
self.set_brightness(brightness, duration)
|
||||||
self.set_flash(flash)
|
self.set_flash(flash)
|
||||||
self.set_effect(effect)
|
self.set_effect(effect)
|
||||||
except yeelight.BulbException as ex:
|
except BulbException as ex:
|
||||||
_LOGGER.error("Unable to set bulb properties: %s", ex)
|
_LOGGER.error("Unable to set bulb properties: %s", ex)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -540,7 +530,7 @@ class YeelightLight(Light):
|
||||||
or rgb):
|
or rgb):
|
||||||
try:
|
try:
|
||||||
self.set_default()
|
self.set_default()
|
||||||
except yeelight.BulbException as ex:
|
except BulbException as ex:
|
||||||
_LOGGER.error("Unable to set the defaults: %s", ex)
|
_LOGGER.error("Unable to set the defaults: %s", ex)
|
||||||
return
|
return
|
||||||
self.device.update()
|
self.device.update()
|
||||||
|
@ -556,27 +546,23 @@ class YeelightLight(Light):
|
||||||
|
|
||||||
def set_mode(self, mode: str):
|
def set_mode(self, mode: str):
|
||||||
"""Set a power mode."""
|
"""Set a power mode."""
|
||||||
import yeelight
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._bulb.set_power_mode(yeelight.enums.PowerMode[mode.upper()])
|
self._bulb.set_power_mode(PowerMode[mode.upper()])
|
||||||
self.device.update()
|
self.device.update()
|
||||||
except yeelight.BulbException as ex:
|
except BulbException as ex:
|
||||||
_LOGGER.error("Unable to set the power mode: %s", ex)
|
_LOGGER.error("Unable to set the power mode: %s", ex)
|
||||||
|
|
||||||
def start_flow(self, transitions, count=0, action=ACTION_RECOVER):
|
def start_flow(self, transitions, count=0, action=ACTION_RECOVER):
|
||||||
"""Start flow."""
|
"""Start flow."""
|
||||||
import yeelight
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
flow = yeelight.Flow(
|
flow = Flow(
|
||||||
count=count,
|
count=count,
|
||||||
action=yeelight.Flow.actions[action],
|
action=Flow.actions[action],
|
||||||
transitions=transitions)
|
transitions=transitions)
|
||||||
|
|
||||||
self._bulb.start_flow(flow, light_type=self.light_type)
|
self._bulb.start_flow(flow, light_type=self.light_type)
|
||||||
self.device.update()
|
self.device.update()
|
||||||
except yeelight.BulbException as ex:
|
except BulbException as ex:
|
||||||
_LOGGER.error("Unable to set effect: %s", ex)
|
_LOGGER.error("Unable to set effect: %s", ex)
|
||||||
|
|
||||||
|
|
||||||
|
@ -590,8 +576,6 @@ class YeelightAmbientLight(YeelightLight):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
"""Initialize the Yeelight Ambient light."""
|
"""Initialize the Yeelight Ambient light."""
|
||||||
from yeelight.enums import LightType
|
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self._min_mireds = kelvin_to_mired(6500)
|
self._min_mireds = kelvin_to_mired(6500)
|
||||||
self._max_mireds = kelvin_to_mired(1700)
|
self._max_mireds = kelvin_to_mired(1700)
|
||||||
|
|
Loading…
Reference in New Issue