From a431277de15a0d420e51b322bb65b6997493878a Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Tue, 17 May 2016 00:06:55 -0700 Subject: [PATCH] Accept human readable color names to change light colors (#2075) * Add support for providing color_name which accepts a CSS3 valid, human readable string such as red or blue * Forgot the schema validation! * ugh farcy * use html5_parse_legacy_color for more input options * Add webcolors==1.5 to setup.py * Block pylint no-member errors on tuple * add color_name_to_rgb test * whoops * revert changes to individual platforms * If color_name is set, pop it off params and set rgb_color with it * Forgot to reset wink.py * Import the legacy function as color_name_to_rgb directly * reset test_color.py * Improve light services.yaml --- homeassistant/components/light/__init__.py | 10 +++++++++- homeassistant/components/light/services.yaml | 4 ++++ homeassistant/util/color.py | 2 ++ requirements_all.txt | 1 + setup.py | 1 + 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index f78e1dbdcba..d1fe0b93f4c 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -39,6 +39,7 @@ ATTR_TRANSITION = "transition" ATTR_RGB_COLOR = "rgb_color" ATTR_XY_COLOR = "xy_color" ATTR_COLOR_TEMP = "color_temp" +ATTR_COLOR_NAME = "color_name" # int with value 0 .. 255 representing brightness of the light. ATTR_BRIGHTNESS = "brightness" @@ -87,6 +88,7 @@ LIGHT_TURN_ON_SCHEMA = vol.Schema({ ATTR_PROFILE: str, ATTR_TRANSITION: VALID_TRANSITION, ATTR_BRIGHTNESS: cv.byte, + ATTR_COLOR_NAME: str, ATTR_RGB_COLOR: vol.All(vol.ExactSequence((cv.byte, cv.byte, cv.byte)), vol.Coerce(tuple)), ATTR_XY_COLOR: vol.All(vol.ExactSequence((cv.small_float, cv.small_float)), @@ -122,7 +124,7 @@ def is_on(hass, entity_id=None): # pylint: disable=too-many-arguments def turn_on(hass, entity_id=None, transition=None, brightness=None, rgb_color=None, xy_color=None, color_temp=None, profile=None, - flash=None, effect=None): + flash=None, effect=None, color_name=None): """Turn all or specified light on.""" data = { key: value for key, value in [ @@ -135,6 +137,7 @@ def turn_on(hass, entity_id=None, transition=None, brightness=None, (ATTR_COLOR_TEMP, color_temp), (ATTR_FLASH, flash), (ATTR_EFFECT, effect), + (ATTR_COLOR_NAME, color_name), ] if value is not None } @@ -228,6 +231,11 @@ def setup(hass, config): params.setdefault(ATTR_XY_COLOR, profile[:2]) params.setdefault(ATTR_BRIGHTNESS, profile[2]) + color_name = params.pop(ATTR_COLOR_NAME, None) + + if color_name is not None: + params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name) + for light in target_lights: light.turn_on(**params) diff --git a/homeassistant/components/light/services.yaml b/homeassistant/components/light/services.yaml index 8ad2ea97a6b..392be490dc3 100644 --- a/homeassistant/components/light/services.yaml +++ b/homeassistant/components/light/services.yaml @@ -16,6 +16,10 @@ turn_on: description: Color for the light in RGB-format example: '[255, 100, 100]' + color_name: + description: A human readable color name + example: 'red' + xy_color: description: Color for the light in XY-format example: '[0.52, 0.43]' diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index 2a662b15f5e..940d435ed44 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -1,5 +1,7 @@ """Color util methods.""" import math +# pylint: disable=unused-import +from webcolors import html5_parse_legacy_color as color_name_to_rgb # noqa HASS_COLOR_MAX = 500 # mireds (inverted) HASS_COLOR_MIN = 154 diff --git a/requirements_all.txt b/requirements_all.txt index a3e2e7f2939..cb7bb18c8ec 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -6,6 +6,7 @@ pip>=7.0.0 vincenty==0.1.4 jinja2>=2.8 voluptuous==0.8.9 +webcolors==1.5 # homeassistant.components.isy994 PyISY==1.0.5 diff --git a/setup.py b/setup.py index f5efbb46e25..d315ae7d386 100755 --- a/setup.py +++ b/setup.py @@ -18,6 +18,7 @@ REQUIRES = [ 'vincenty==0.1.4', 'jinja2>=2.8', 'voluptuous==0.8.9', + 'webcolors==1.5', ] setup(