Improve yeelight imports (#22804)

This commit is contained in:
zewelor authored and Sebastian Muszynski committed 2019-04-07 16:07:34 +02:00
1 parent bb5c18f7be
commit a91e79ee77
2 files changed
+16 -21

No files matched your search

+7 -13
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