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:
Sidney 2019-03-12 16:51:51 +01:00 committed by Anders Melchiorsen
parent d3bab30dbe
commit 1444a684e0

View File

@ -7,6 +7,8 @@ https://home-assistant.io/components/light.flux_led/
import logging
import socket
import random
from asyncio import sleep
from functools import partial
import voluptuous as vol
@ -170,6 +172,8 @@ class FluxLight(Light):
self._custom_effect = device[CONF_CUSTOM_EFFECT]
self._bulb = None
self._error_reported = False
self._color = (0, 0, 100)
self._white_value = 0
def _connect(self):
"""Connect to Flux light."""
@ -210,14 +214,14 @@ class FluxLight(Light):
def brightness(self):
"""Return the brightness of this light between 0..255."""
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
def hs_color(self):
"""Return the color property."""
return color_util.color_RGB_to_hs(*self._bulb.getRgb())
return self._color[0:2]
@property
def supported_features(self):
@ -233,7 +237,7 @@ class FluxLight(Light):
@property
def white_value(self):
"""Return the white value of this light between 0..255."""
return self._bulb.getRgbw()[3]
return self._white_value
@property
def effect_list(self):
@ -257,24 +261,25 @@ class FluxLight(Light):
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."""
if not self.is_on:
self._bulb.turnOn()
self._bulb.turnOn()
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)
effect = kwargs.get(ATTR_EFFECT)
white = kwargs.get(ATTR_WHITE_VALUE)
# 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"
" an effect is specified for a flux bulb")
@ -302,12 +307,11 @@ class FluxLight(Light):
if brightness is None:
brightness = self.brightness
# Preserve color on brightness/white level change
if rgb is None:
rgb = self._bulb.getRgb()
if white is None and self._mode == MODE_RGBW:
white = self.white_value
if hs_color:
self._color = (hs_color[0], hs_color[1], brightness / 255 * 100)
elif brightness and (hs_color is None) and self._mode != MODE_WHITE:
self._color = (self._color[0], self._color[1],
brightness / 255 * 100)
# handle W only mode (use brightness instead of white value)
if self._mode == MODE_WHITE:
@ -315,11 +319,14 @@ class FluxLight(Light):
# handle RGBW mode
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
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):
"""Turn the specified or all lights off."""
@ -331,6 +338,10 @@ class FluxLight(Light):
try:
self._connect()
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:
self._disconnect()
if not self._error_reported:
@ -338,5 +349,8 @@ class FluxLight(Light):
self._ipaddr, self._name)
self._error_reported = True
return
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]