mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Fixes for yeelight availbility state (#22502)
This commit is contained in:
parent
a95fb809a5
commit
fae8265a37
@ -212,6 +212,7 @@ class YeelightDevice:
|
|||||||
self._name = config.get(CONF_NAME)
|
self._name = config.get(CONF_NAME)
|
||||||
self._model = config.get(CONF_MODEL)
|
self._model = config.get(CONF_MODEL)
|
||||||
self._bulb_device = None
|
self._bulb_device = None
|
||||||
|
self._available = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def bulb(self):
|
def bulb(self):
|
||||||
@ -224,7 +225,9 @@ class YeelightDevice:
|
|||||||
# force init for type
|
# force init for type
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
self._available = True
|
||||||
except yeelight.BulbException as ex:
|
except yeelight.BulbException as ex:
|
||||||
|
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)
|
||||||
|
|
||||||
@ -245,10 +248,15 @@ class YeelightDevice:
|
|||||||
"""Return ip address."""
|
"""Return ip address."""
|
||||||
return self._ipaddr
|
return self._ipaddr
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return true is device is available."""
|
||||||
|
return self._available
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_nightlight_enabled(self) -> bool:
|
def is_nightlight_enabled(self) -> bool:
|
||||||
"""Return true / false if nightlight is currently enabled."""
|
"""Return true / false if nightlight is currently enabled."""
|
||||||
if self._bulb_device is None:
|
if self.bulb is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return self.bulb.last_properties.get('active_mode') == '1'
|
return self.bulb.last_properties.get('active_mode') == '1'
|
||||||
@ -271,7 +279,7 @@ class YeelightDevice:
|
|||||||
light_type = yeelight.enums.LightType.Main
|
light_type = yeelight.enums.LightType.Main
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._bulb_device.turn_on(duration=duration, light_type=light_type)
|
self.bulb.turn_on(duration=duration, light_type=light_type)
|
||||||
except yeelight.BulbException as ex:
|
except yeelight.BulbException as ex:
|
||||||
_LOGGER.error("Unable to turn the bulb on: %s", ex)
|
_LOGGER.error("Unable to turn the bulb on: %s", ex)
|
||||||
return
|
return
|
||||||
@ -284,16 +292,24 @@ class YeelightDevice:
|
|||||||
light_type = yeelight.enums.LightType.Main
|
light_type = yeelight.enums.LightType.Main
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._bulb_device.turn_off(duration=duration,
|
self.bulb.turn_off(duration=duration, light_type=light_type)
|
||||||
light_type=light_type)
|
|
||||||
except yeelight.BulbException as ex:
|
except yeelight.BulbException as ex:
|
||||||
_LOGGER.error("Unable to turn the bulb on: %s", ex)
|
_LOGGER.error("Unable to turn the bulb off: %s", ex)
|
||||||
return
|
return
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Read new properties from the device."""
|
"""Read new properties from the device."""
|
||||||
|
import yeelight
|
||||||
|
|
||||||
if not self.bulb:
|
if not self.bulb:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._bulb_device.get_properties(UPDATE_REQUEST_PROPERTIES)
|
try:
|
||||||
|
self.bulb.get_properties(UPDATE_REQUEST_PROPERTIES)
|
||||||
|
self._available = True
|
||||||
|
except yeelight.BulbException as ex:
|
||||||
|
if self._available: # just inform once
|
||||||
|
_LOGGER.error("Unable to update bulb status: %s", ex)
|
||||||
|
self._available = False
|
||||||
|
|
||||||
dispatcher_send(self._hass, DATA_UPDATED, self._ipaddr)
|
dispatcher_send(self._hass, DATA_UPDATED, self._ipaddr)
|
||||||
|
@ -162,7 +162,6 @@ class YeelightLight(Light):
|
|||||||
self._device = device
|
self._device = device
|
||||||
|
|
||||||
self._supported_features = SUPPORT_YEELIGHT
|
self._supported_features = SUPPORT_YEELIGHT
|
||||||
self._available = False
|
|
||||||
|
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
self._color_temp = None
|
self._color_temp = None
|
||||||
@ -196,7 +195,7 @@ class YeelightLight(Light):
|
|||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return if bulb is available."""
|
"""Return if bulb is available."""
|
||||||
return self._available
|
return self.device.available
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self) -> int:
|
def supported_features(self) -> int:
|
||||||
@ -304,14 +303,7 @@ class YeelightLight(Light):
|
|||||||
# F821: https://github.com/PyCQA/pyflakes/issues/373
|
# F821: https://github.com/PyCQA/pyflakes/issues/373
|
||||||
@property
|
@property
|
||||||
def _bulb(self) -> 'yeelight.Bulb': # noqa: F821
|
def _bulb(self) -> 'yeelight.Bulb': # noqa: F821
|
||||||
bulb = self.device.bulb
|
return self.device.bulb
|
||||||
|
|
||||||
if bulb:
|
|
||||||
self._available = True
|
|
||||||
return bulb
|
|
||||||
|
|
||||||
self._available = False
|
|
||||||
return None
|
|
||||||
|
|
||||||
def set_music_mode(self, mode) -> None:
|
def set_music_mode(self, mode) -> None:
|
||||||
"""Set the music mode on or off."""
|
"""Set the music mode on or off."""
|
||||||
@ -323,52 +315,45 @@ class YeelightLight(Light):
|
|||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Update properties from the bulb."""
|
"""Update properties from the bulb."""
|
||||||
import yeelight
|
import yeelight
|
||||||
try:
|
bulb_type = self._bulb.bulb_type
|
||||||
bulb_type = self._bulb.bulb_type
|
|
||||||
|
|
||||||
if bulb_type == yeelight.BulbType.Color:
|
|
||||||
self._supported_features = SUPPORT_YEELIGHT_RGB
|
|
||||||
elif self.light_type == yeelight.enums.LightType.Ambient:
|
|
||||||
self._supported_features = SUPPORT_YEELIGHT_RGB
|
|
||||||
elif bulb_type in (yeelight.BulbType.WhiteTemp,
|
|
||||||
yeelight.BulbType.WhiteTempMood):
|
|
||||||
if self._is_nightlight_enabled:
|
|
||||||
self._supported_features = SUPPORT_YEELIGHT
|
|
||||||
else:
|
|
||||||
self._supported_features = SUPPORT_YEELIGHT_WHITE_TEMP
|
|
||||||
|
|
||||||
if self.min_mireds is None:
|
|
||||||
model_specs = self._bulb.get_model_specs()
|
|
||||||
self._min_mireds = \
|
|
||||||
kelvin_to_mired(model_specs['color_temp']['max'])
|
|
||||||
self._max_mireds = \
|
|
||||||
kelvin_to_mired(model_specs['color_temp']['min'])
|
|
||||||
|
|
||||||
if bulb_type == yeelight.BulbType.WhiteTempMood:
|
|
||||||
self._is_on = self._get_property('main_power') == 'on'
|
|
||||||
else:
|
|
||||||
self._is_on = self._get_property('power') == 'on'
|
|
||||||
|
|
||||||
|
if bulb_type == yeelight.BulbType.Color:
|
||||||
|
self._supported_features = SUPPORT_YEELIGHT_RGB
|
||||||
|
elif self.light_type == yeelight.enums.LightType.Ambient:
|
||||||
|
self._supported_features = SUPPORT_YEELIGHT_RGB
|
||||||
|
elif bulb_type in (yeelight.BulbType.WhiteTemp,
|
||||||
|
yeelight.BulbType.WhiteTempMood):
|
||||||
if self._is_nightlight_enabled:
|
if self._is_nightlight_enabled:
|
||||||
bright = self._get_property('nl_br', None)
|
self._supported_features = SUPPORT_YEELIGHT
|
||||||
else:
|
else:
|
||||||
bright = self._get_property('bright', None)
|
self._supported_features = SUPPORT_YEELIGHT_WHITE_TEMP
|
||||||
|
|
||||||
if bright:
|
if self.min_mireds is None:
|
||||||
self._brightness = round(255 * (int(bright) / 100))
|
model_specs = self._bulb.get_model_specs()
|
||||||
|
self._min_mireds = \
|
||||||
|
kelvin_to_mired(model_specs['color_temp']['max'])
|
||||||
|
self._max_mireds = \
|
||||||
|
kelvin_to_mired(model_specs['color_temp']['min'])
|
||||||
|
|
||||||
temp_in_k = self._get_property('ct')
|
if bulb_type == yeelight.BulbType.WhiteTempMood:
|
||||||
|
self._is_on = self._get_property('main_power') == 'on'
|
||||||
|
else:
|
||||||
|
self._is_on = self._get_property('power') == 'on'
|
||||||
|
|
||||||
if temp_in_k:
|
if self._is_nightlight_enabled:
|
||||||
self._color_temp = kelvin_to_mired(int(temp_in_k))
|
bright = self._get_property('nl_br')
|
||||||
|
else:
|
||||||
|
bright = self._get_property('bright')
|
||||||
|
|
||||||
self._hs = self._get_hs_from_properties()
|
if bright:
|
||||||
|
self._brightness = round(255 * (int(bright) / 100))
|
||||||
|
|
||||||
self._available = True
|
temp_in_k = self._get_property('ct')
|
||||||
except yeelight.BulbException as ex:
|
|
||||||
if self._available: # just inform once
|
if temp_in_k:
|
||||||
_LOGGER.error("Unable to update bulb status: %s", ex)
|
self._color_temp = kelvin_to_mired(int(temp_in_k))
|
||||||
self._available = False
|
|
||||||
|
self._hs = self._get_hs_from_properties()
|
||||||
|
|
||||||
@_cmd
|
@_cmd
|
||||||
def set_brightness(self, brightness, duration) -> None:
|
def set_brightness(self, brightness, duration) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user