Skip to content

Commit

Permalink
Accept human readable color names to change light colors (home-assist…
Browse files Browse the repository at this point in the history
…ant#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
  • Loading branch information
robbiet480 committed May 17, 2016
1 parent 7208ff5 commit a431277
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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"
Expand Down Expand Up @@ -87,6 +88,7 @@
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)),
Expand Down Expand Up @@ -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 [
Expand All @@ -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
}

Expand Down Expand Up @@ -228,6 +231,11 @@ def handle_light_service(service):
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)

Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/light/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/util/color.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'vincenty==0.1.4',
'jinja2>=2.8',
'voluptuous==0.8.9',
'webcolors==1.5',
]

setup(
Expand Down

0 comments on commit a431277

Please sign in to comment.