mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Fix MagicHome LEDs with flux_led component (#20733)
* bug fixing for MagicHome LEDs with flux_led component. * corrections of the fixes for flux_led * now asyncio sleep and turn on with brigthness possible * indention fix with flux_led * async now works * houndci fixes * little fixes for flux_led * self._color fix for flux_led * Add docstring
This commit is contained in:
parent
d3bab30dbe
commit
1444a684e0
@ -7,6 +7,8 @@ https://home-assistant.io/components/light.flux_led/
|
|||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
import random
|
import random
|
||||||
|
from asyncio import sleep
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -170,6 +172,8 @@ class FluxLight(Light):
|
|||||||
self._custom_effect = device[CONF_CUSTOM_EFFECT]
|
self._custom_effect = device[CONF_CUSTOM_EFFECT]
|
||||||
self._bulb = None
|
self._bulb = None
|
||||||
self._error_reported = False
|
self._error_reported = False
|
||||||
|
self._color = (0, 0, 100)
|
||||||
|
self._white_value = 0
|
||||||
|
|
||||||
def _connect(self):
|
def _connect(self):
|
||||||
"""Connect to Flux light."""
|
"""Connect to Flux light."""
|
||||||
@ -210,14 +214,14 @@ class FluxLight(Light):
|
|||||||
def brightness(self):
|
def brightness(self):
|
||||||
"""Return the brightness of this light between 0..255."""
|
"""Return the brightness of this light between 0..255."""
|
||||||
if self._mode == MODE_WHITE:
|
if self._mode == MODE_WHITE:
|
||||||
return self.white_value
|
return self._white_value
|
||||||
|
|
||||||
return self._bulb.brightness
|
return int(self._color[2] / 100 * 255)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hs_color(self):
|
def hs_color(self):
|
||||||
"""Return the color property."""
|
"""Return the color property."""
|
||||||
return color_util.color_RGB_to_hs(*self._bulb.getRgb())
|
return self._color[0:2]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
@ -233,7 +237,7 @@ class FluxLight(Light):
|
|||||||
@property
|
@property
|
||||||
def white_value(self):
|
def white_value(self):
|
||||||
"""Return the white value of this light between 0..255."""
|
"""Return the white value of this light between 0..255."""
|
||||||
return self._bulb.getRgbw()[3]
|
return self._white_value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def effect_list(self):
|
def effect_list(self):
|
||||||
@ -257,24 +261,25 @@ class FluxLight(Light):
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
|
"""Turn the specified or all lights on and wait for state."""
|
||||||
|
await self.hass.async_add_executor_job(partial(self._turn_on,
|
||||||
|
**kwargs))
|
||||||
|
# The bulb needs a second to tell its new values,
|
||||||
|
# so we wait 2 seconds before updating
|
||||||
|
await sleep(2)
|
||||||
|
|
||||||
|
def _turn_on(self, **kwargs):
|
||||||
"""Turn the specified or all lights on."""
|
"""Turn the specified or all lights on."""
|
||||||
if not self.is_on:
|
self._bulb.turnOn()
|
||||||
self._bulb.turnOn()
|
|
||||||
|
|
||||||
hs_color = kwargs.get(ATTR_HS_COLOR)
|
hs_color = kwargs.get(ATTR_HS_COLOR)
|
||||||
|
|
||||||
if hs_color:
|
|
||||||
rgb = color_util.color_hs_to_RGB(*hs_color)
|
|
||||||
else:
|
|
||||||
rgb = None
|
|
||||||
|
|
||||||
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
||||||
effect = kwargs.get(ATTR_EFFECT)
|
effect = kwargs.get(ATTR_EFFECT)
|
||||||
white = kwargs.get(ATTR_WHITE_VALUE)
|
white = kwargs.get(ATTR_WHITE_VALUE)
|
||||||
|
|
||||||
# Show warning if effect set with rgb, brightness, or white level
|
# Show warning if effect set with rgb, brightness, or white level
|
||||||
if effect and (brightness or white or rgb):
|
if effect and (brightness or white or hs_color):
|
||||||
_LOGGER.warning("RGB, brightness and white level are ignored when"
|
_LOGGER.warning("RGB, brightness and white level are ignored when"
|
||||||
" an effect is specified for a flux bulb")
|
" an effect is specified for a flux bulb")
|
||||||
|
|
||||||
@ -302,12 +307,11 @@ class FluxLight(Light):
|
|||||||
if brightness is None:
|
if brightness is None:
|
||||||
brightness = self.brightness
|
brightness = self.brightness
|
||||||
|
|
||||||
# Preserve color on brightness/white level change
|
if hs_color:
|
||||||
if rgb is None:
|
self._color = (hs_color[0], hs_color[1], brightness / 255 * 100)
|
||||||
rgb = self._bulb.getRgb()
|
elif brightness and (hs_color is None) and self._mode != MODE_WHITE:
|
||||||
|
self._color = (self._color[0], self._color[1],
|
||||||
if white is None and self._mode == MODE_RGBW:
|
brightness / 255 * 100)
|
||||||
white = self.white_value
|
|
||||||
|
|
||||||
# handle W only mode (use brightness instead of white value)
|
# handle W only mode (use brightness instead of white value)
|
||||||
if self._mode == MODE_WHITE:
|
if self._mode == MODE_WHITE:
|
||||||
@ -315,11 +319,14 @@ class FluxLight(Light):
|
|||||||
|
|
||||||
# handle RGBW mode
|
# handle RGBW mode
|
||||||
elif self._mode == MODE_RGBW:
|
elif self._mode == MODE_RGBW:
|
||||||
self._bulb.setRgbw(*tuple(rgb), w=white, brightness=brightness)
|
if white is None:
|
||||||
|
self._bulb.setRgbw(*color_util.color_hsv_to_RGB(*self._color))
|
||||||
|
else:
|
||||||
|
self._bulb.setRgbw(w=white)
|
||||||
# handle RGB mode
|
# handle RGB mode
|
||||||
else:
|
else:
|
||||||
self._bulb.setRgb(*tuple(rgb), brightness=brightness)
|
self._bulb.setRgb(*color_util.color_hsv_to_RGB(*self._color))
|
||||||
|
return
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the specified or all lights off."""
|
"""Turn the specified or all lights off."""
|
||||||
@ -331,6 +338,10 @@ class FluxLight(Light):
|
|||||||
try:
|
try:
|
||||||
self._connect()
|
self._connect()
|
||||||
self._error_reported = False
|
self._error_reported = False
|
||||||
|
if self._bulb.getRgb() != (0, 0, 0):
|
||||||
|
color = self._bulb.getRgbw()
|
||||||
|
self._color = color_util.color_RGB_to_hsv(*color[0:3])
|
||||||
|
self._white_value = color[3]
|
||||||
except socket.error:
|
except socket.error:
|
||||||
self._disconnect()
|
self._disconnect()
|
||||||
if not self._error_reported:
|
if not self._error_reported:
|
||||||
@ -338,5 +349,8 @@ class FluxLight(Light):
|
|||||||
self._ipaddr, self._name)
|
self._ipaddr, self._name)
|
||||||
self._error_reported = True
|
self._error_reported = True
|
||||||
return
|
return
|
||||||
|
|
||||||
self._bulb.update_state(retry=2)
|
self._bulb.update_state(retry=2)
|
||||||
|
if self._bulb.getRgb() != (0, 0, 0):
|
||||||
|
color = self._bulb.getRgbw()
|
||||||
|
self._color = color_util.color_RGB_to_hsv(*color[0:3])
|
||||||
|
self._white_value = color[3]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user