Improve yeelight imports (#22804)

pull/22883/head
zewelor 2019-04-07 16:07:34 +02:00 committed by Pascal Vizeli
parent 7a8aa79f19
commit abb531c06b
2 changed files with 16 additions and 21 deletions

View File

@ -185,8 +185,8 @@ class YeelightDevice:
@property
def bulb(self):
"""Return bulb device."""
import yeelight
if self._bulb_device is None:
import yeelight
try:
self._bulb_device = yeelight.Bulb(self._ipaddr,
model=self._model)
@ -241,33 +241,27 @@ class YeelightDevice:
def turn_on(self, duration=DEFAULT_TRANSITION, light_type=None):
"""Turn on device."""
import yeelight
if not light_type:
light_type = yeelight.enums.LightType.Main
from yeelight import BulbException
try:
self.bulb.turn_on(duration=duration, light_type=light_type)
except yeelight.BulbException as ex:
except BulbException as ex:
_LOGGER.error("Unable to turn the bulb on: %s", ex)
return
def turn_off(self, duration=DEFAULT_TRANSITION, light_type=None):
"""Turn off device."""
import yeelight
if not light_type:
light_type = yeelight.enums.LightType.Main
from yeelight import BulbException
try:
self.bulb.turn_off(duration=duration, light_type=light_type)
except yeelight.BulbException as ex:
except BulbException as ex:
_LOGGER.error("Unable to turn the bulb off: %s", ex)
return
def update(self):
"""Read new properties from the device."""
import yeelight
from yeelight import BulbException
if not self.bulb:
return
@ -275,7 +269,7 @@ class YeelightDevice:
try:
self.bulb.get_properties(UPDATE_REQUEST_PROPERTIES)
self._available = True
except yeelight.BulbException as ex:
except BulbException as ex:
if self._available: # just inform once
_LOGGER.error("Unable to update bulb status: %s", ex)
self._available = False

View File

@ -189,6 +189,8 @@ 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
@ -202,6 +204,8 @@ class YeelightLight(Light):
self._min_mireds = None
self._max_mireds = None
self._light_type = LightType.Main
if custom_effects:
self._custom_effects = custom_effects
else:
@ -281,8 +285,7 @@ class YeelightLight(Light):
@property
def light_type(self):
"""Return light type."""
import yeelight
return yeelight.enums.LightType.Main
return self._light_type
def _get_hs_from_properties(self):
rgb = self._get_property('rgb')
@ -589,21 +592,19 @@ 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)
self._light_type = LightType.Ambient
@property
def name(self) -> str:
"""Return the name of the device if any."""
return "{} ambilight".format(self.device.name)
@property
def light_type(self):
"""Return light type."""
import yeelight
return yeelight.enums.LightType.Ambient
@property
def _is_nightlight_enabled(self):
return False