myStrom: Add RGB support to Wifi bulbs (#13194)

This commit is contained in:
Maximilien Cuony 2018-03-15 16:45:27 +01:00 committed by Fabian Affolter
parent 1d2fd8a2e9
commit ee6d6a8859

View File

@ -11,8 +11,10 @@ import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.light import (
Light, PLATFORM_SCHEMA, ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS,
SUPPORT_EFFECT, ATTR_EFFECT, SUPPORT_FLASH)
SUPPORT_EFFECT, ATTR_EFFECT, SUPPORT_FLASH, SUPPORT_RGB_COLOR,
ATTR_RGB_COLOR)
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, STATE_UNKNOWN
from homeassistant.util.color import color_RGB_to_hsv, color_hsv_to_RGB
REQUIREMENTS = ['python-mystrom==0.3.8']
@ -20,7 +22,10 @@ _LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'myStrom bulb'
SUPPORT_MYSTROM = (SUPPORT_BRIGHTNESS | SUPPORT_EFFECT | SUPPORT_FLASH)
SUPPORT_MYSTROM = (
SUPPORT_BRIGHTNESS | SUPPORT_EFFECT | SUPPORT_FLASH |
SUPPORT_RGB_COLOR
)
EFFECT_RAINBOW = 'rainbow'
EFFECT_SUNRISE = 'sunrise'
@ -67,6 +72,8 @@ class MyStromLight(Light):
self._state = None
self._available = False
self._brightness = 0
self._color_h = 0
self._color_s = 0
@property
def name(self):
@ -83,6 +90,11 @@ class MyStromLight(Light):
"""Return the brightness of the light."""
return self._brightness
@property
def rgb_color(self):
"""Return the color of the light."""
return color_hsv_to_RGB(self._color_h, self._color_s, self._brightness)
@property
def available(self) -> bool:
"""Return True if entity is available."""
@ -105,11 +117,25 @@ class MyStromLight(Light):
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
effect = kwargs.get(ATTR_EFFECT)
if ATTR_RGB_COLOR in kwargs:
# New color, compute from RGB
color_h, color_s, brightness = color_RGB_to_hsv(
*kwargs[ATTR_RGB_COLOR]
)
brightness = brightness / 100 * 255
elif ATTR_BRIGHTNESS in kwargs:
# Brightness update, keep color
color_h, color_s = self._color_h, self._color_s
else:
color_h, color_s = 0, 0 # Back to white
try:
if not self.is_on:
self._bulb.set_on()
if brightness is not None:
self._bulb.set_color_hsv(0, 0, round(brightness * 100 / 255))
self._bulb.set_color_hsv(
int(color_h), int(color_s), round(brightness * 100 / 255)
)
if effect == EFFECT_SUNRISE:
self._bulb.set_sunrise(30)
if effect == EFFECT_RAINBOW:
@ -132,7 +158,14 @@ class MyStromLight(Light):
try:
self._state = self._bulb.get_status()
self._brightness = int(self._bulb.get_brightness()) * 255 / 100
colors = self._bulb.get_color()['color']
color_h, color_s, color_v = colors.split(';')
self._color_h = int(color_h)
self._color_s = int(color_s)
self._brightness = int(color_v) * 255 / 100
self._available = True
except MyStromConnectionError:
_LOGGER.warning("myStrom bulb not online")