From c87238e8b5d3a67c035b1a97d5b5b9af8f6483bc Mon Sep 17 00:00:00 2001 From: Keaton Taylor Date: Mon, 11 Jul 2016 12:34:42 -0500 Subject: [PATCH] Clamp brightness between 0 and 255 Change to ensure that values over 255 supplied by the config will be clamed to a max value of 255. --- homeassistant/components/light/__init__.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 2b0af395d02..cef056cc693 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -42,6 +42,11 @@ ATTR_COLOR_NAME = "color_name" # int with value 0 .. 255 representing brightness of the light. ATTR_BRIGHTNESS = "brightness" +# String representing weather to turn the light on or off. +ATTR_POWER = "power" +POWER_ON = "on" +POWER_OFF = "off" + # String representing a profile (built-in ones or external defined). ATTR_PROFILE = "profile" @@ -65,22 +70,26 @@ PROP_TO_ATTR = { 'xy_color': ATTR_XY_COLOR, } +SERVICE_SET_STATE = "set_state" + # Service call validation schemas VALID_TRANSITION = vol.All(vol.Coerce(int), vol.Clamp(min=0, max=900)) +VALID_BRIGHTNESS = vol.All(vol.Coerce(int), vol.Clamp(min=0, max=255)) LIGHT_TURN_ON_SCHEMA = vol.Schema({ ATTR_ENTITY_ID: cv.entity_ids, ATTR_PROFILE: str, ATTR_TRANSITION: VALID_TRANSITION, - ATTR_BRIGHTNESS: cv.byte, + ATTR_BRIGHTNESS: VALID_BRIGHTNESS, 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)), vol.Coerce(tuple)), - ATTR_COLOR_TEMP: vol.All(int, vol.Range(min=154, max=500)), + ATTR_COLOR_TEMP: vol.All(vol.int, vol.Range(min=154, max=500)), ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]), ATTR_EFFECT: vol.In([EFFECT_COLORLOOP, EFFECT_RANDOM, EFFECT_WHITE]), + ATTR_POWER: vol.In([POWER_OFF, POWER_ON]), }) LIGHT_TURN_OFF_SCHEMA = vol.Schema({ @@ -109,7 +118,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, color_name=None): + flash=None, effect=None, color_name=None, power=None): """Turn all or specified light on.""" data = { key: value for key, value in [ @@ -123,12 +132,12 @@ def turn_on(hass, entity_id=None, transition=None, brightness=None, (ATTR_FLASH, flash), (ATTR_EFFECT, effect), (ATTR_COLOR_NAME, color_name), + (ATTR_POWER, power), ] if value is not None } hass.services.call(DOMAIN, SERVICE_TURN_ON, data) - def turn_off(hass, entity_id=None, transition=None): """Turn all or specified light off.""" data = { @@ -241,15 +250,13 @@ def setup(hass, config): hass.services.register(DOMAIN, SERVICE_TOGGLE, handle_light_service, descriptions.get(SERVICE_TOGGLE), schema=LIGHT_TOGGLE_SCHEMA) - return True class Light(ToggleEntity): """Representation of a light.""" - # pylint: disable=no-self-use, abstract-method - + # pylint: disable=no-self-use @property def brightness(self): """Return the brightness of this light between 0..255."""