From 1dddc55b9849c11fa51f7baa6a412ab4afc37fd6 Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Sat, 29 Apr 2017 18:19:24 +0200 Subject: [PATCH 1/4] Light: add SUPPORT_OFF_STATE --- homeassistant/components/light/__init__.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 853fe4d9fb181d..6a9158d76fcc51 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -42,6 +42,7 @@ SUPPORT_TRANSITION = 32 SUPPORT_XY_COLOR = 64 SUPPORT_WHITE_VALUE = 128 +SUPPORT_OFF_STATE = 256 # Integer that represents transition time in seconds to make change. ATTR_TRANSITION = "transition" @@ -77,6 +78,8 @@ LIGHT_PROFILES_FILE = "light_profiles.csv" +ATTR_POWER_ON = "power_on" + PROP_TO_ATTR = { 'brightness': ATTR_BRIGHTNESS, 'color_temp': ATTR_COLOR_TEMP, @@ -107,6 +110,7 @@ ATTR_WHITE_VALUE: vol.All(vol.Coerce(int), vol.Range(min=0, max=255)), ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]), ATTR_EFFECT: cv.string, + vol.Optional(ATTR_POWER_ON, default=True): cv.boolean, }) LIGHT_TURN_OFF_SCHEMA = vol.Schema({ @@ -143,19 +147,20 @@ def is_on(hass, entity_id=None): def turn_on(hass, entity_id=None, transition=None, brightness=None, rgb_color=None, xy_color=None, color_temp=None, white_value=None, - profile=None, flash=None, effect=None, color_name=None): + profile=None, flash=None, effect=None, color_name=None, + power_on=None): """Turn all or specified light on.""" hass.add_job( async_turn_on, hass, entity_id, transition, brightness, rgb_color, xy_color, color_temp, white_value, - profile, flash, effect, color_name) + profile, flash, effect, color_name, power_on) @callback def async_turn_on(hass, entity_id=None, transition=None, brightness=None, rgb_color=None, xy_color=None, color_temp=None, white_value=None, profile=None, flash=None, effect=None, - color_name=None): + color_name=None, power_on=None): """Turn all or specified light on.""" data = { key: value for key, value in [ @@ -170,6 +175,7 @@ def async_turn_on(hass, entity_id=None, transition=None, brightness=None, (ATTR_FLASH, flash), (ATTR_EFFECT, effect), (ATTR_COLOR_NAME, color_name), + (ATTR_POWER_ON, power_on), ] if value is not None } From f9ec48d58b1d3a6622c30cf5dfbd0b204bfc4a15 Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Sat, 29 Apr 2017 18:21:37 +0200 Subject: [PATCH 2/4] LIFX: add SUPPORT_OFF_STATE --- homeassistant/components/light/lifx/__init__.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/light/lifx/__init__.py b/homeassistant/components/light/lifx/__init__.py index f13934011e9bc0..7a256c9a92ec6c 100644 --- a/homeassistant/components/light/lifx/__init__.py +++ b/homeassistant/components/light/lifx/__init__.py @@ -18,8 +18,8 @@ from homeassistant.components.light import ( Light, PLATFORM_SCHEMA, ATTR_BRIGHTNESS, ATTR_COLOR_NAME, ATTR_RGB_COLOR, ATTR_XY_COLOR, ATTR_COLOR_TEMP, ATTR_TRANSITION, ATTR_EFFECT, - SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR, - SUPPORT_XY_COLOR, SUPPORT_TRANSITION, SUPPORT_EFFECT) + ATTR_POWER_ON, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR, + SUPPORT_XY_COLOR, SUPPORT_TRANSITION, SUPPORT_EFFECT, SUPPORT_OFF_STATE) from homeassistant.util.color import ( color_temperature_mired_to_kelvin, color_temperature_kelvin_to_mired) from homeassistant import util @@ -47,7 +47,8 @@ SHORT_MAX = 65535 SUPPORT_LIFX = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR | - SUPPORT_XY_COLOR | SUPPORT_TRANSITION | SUPPORT_EFFECT) + SUPPORT_XY_COLOR | SUPPORT_TRANSITION | SUPPORT_EFFECT | + SUPPORT_OFF_STATE) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_SERVER, default='0.0.0.0'): cv.string, @@ -316,9 +317,12 @@ def async_turn_on(self, **kwargs): if self._power == 0: if changed_color: self.device.set_color(hsbk, None, 0) - self.device.set_power(True, None, fade) + if kwargs[ATTR_POWER_ON]: + self.device.set_power(True, None, fade) else: - self.device.set_power(True, None, 0) # racing for power status + if kwargs[ATTR_POWER_ON]: + # racing for power status, our state could be obsolete + self.device.set_power(True, None, 0) if changed_color: self.device.set_color(hsbk, None, fade) From e978423199f0eb974d8461b748b4667ef40c81b6 Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Sat, 29 Apr 2017 17:08:02 +0200 Subject: [PATCH 3/4] Flux switch: refactor xy/temp into set_lights --- homeassistant/components/switch/flux.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/switch/flux.py b/homeassistant/components/switch/flux.py index 2052ffc4c15ae1..96e1a1ce0528a7 100644 --- a/homeassistant/components/switch/flux.py +++ b/homeassistant/components/switch/flux.py @@ -59,24 +59,27 @@ }) -def set_lights_xy(hass, lights, x_val, y_val, brightness): +def set_lights(hass, lights, **kwargs): """Set color of array of lights.""" for light in lights: if is_on(hass, light): - turn_on(hass, light, - xy_color=[x_val, y_val], - brightness=brightness, - transition=30) + turn_on(hass, light, **kwargs) + + +def set_lights_xy(hass, lights, x_val, y_val, brightness): + """Set color of array of lights.""" + set_lights(hass, lights, + xy_color=[x_val, y_val], + brightness=brightness, + transition=30) def set_lights_temp(hass, lights, mired, brightness): """Set color of array of lights.""" - for light in lights: - if is_on(hass, light): - turn_on(hass, light, - color_temp=int(mired), - brightness=brightness, - transition=30) + set_lights(hass, lights, + color_temp=int(mired), + brightness=brightness, + transition=30) # pylint: disable=unused-argument From 0b65c462436560d904627bb2f9c41cb46fe51a63 Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Sat, 29 Apr 2017 18:18:27 +0200 Subject: [PATCH 4/4] Flux switch: use SUPPORT_OFF_STATE --- homeassistant/components/switch/flux.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/switch/flux.py b/homeassistant/components/switch/flux.py index 96e1a1ce0528a7..d722bcc244d52f 100644 --- a/homeassistant/components/switch/flux.py +++ b/homeassistant/components/switch/flux.py @@ -10,15 +10,17 @@ import logging import voluptuous as vol -from homeassistant.components.light import is_on, turn_on +from homeassistant.components.light import is_on, turn_on, SUPPORT_OFF_STATE from homeassistant.components.sun import next_setting, next_rising from homeassistant.components.switch import DOMAIN, SwitchDevice -from homeassistant.const import CONF_NAME, CONF_PLATFORM +from homeassistant.const import ( + CONF_NAME, CONF_PLATFORM, ATTR_SUPPORTED_FEATURES) from homeassistant.helpers.event import track_time_change from homeassistant.util.color import ( color_temperature_to_rgb, color_RGB_to_xy, color_temperature_kelvin_to_mired) from homeassistant.util.dt import now as dt_now +from homeassistant.loader import get_component import homeassistant.helpers.config_validation as cv DEPENDENCIES = ['sun', 'light'] @@ -61,9 +63,14 @@ def set_lights(hass, lights, **kwargs): """Set color of array of lights.""" - for light in lights: - if is_on(hass, light): - turn_on(hass, light, **kwargs) + entity_ids = get_component('group').expand_entity_ids(hass, lights) + for entity_id in entity_ids: + state = hass.states.get(entity_id) + + if state.attributes.get(ATTR_SUPPORTED_FEATURES) & SUPPORT_OFF_STATE: + turn_on(hass, entity_id, power_on=False, **kwargs) + elif is_on(hass, entity_id): + turn_on(hass, entity_id, **kwargs) def set_lights_xy(hass, lights, x_val, y_val, brightness):