From ab61cda80f8b9f5026980bc20636c850fa7d753b Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Fri, 25 Sep 2020 16:45:10 +0200 Subject: [PATCH] add more options to apply service --- custom_components/adaptive_lighting/const.py | 5 ++- .../adaptive_lighting/services.yaml | 12 ++++-- custom_components/adaptive_lighting/switch.py | 37 ++++++++++++++----- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 56cb1079..3c0def2c 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -34,10 +34,13 @@ CONF_SUNSET_OFFSET, DEFAULT_SUNSET_OFFSET = "sunset_offset", 0 CONF_SUNSET_TIME = "sunset_time" CONF_TRANSITION, DEFAULT_TRANSITION = "transition", 60 -SERVICE_APPLY = "apply" UNDO_UPDATE_LISTENER = "undo_update_listener" NONE_STR = "None" # TODO: use `from homeassistant.const import ENTITY_MATCH_NONE`? +SERVICE_APPLY = "apply" +CONF_COLORS_ONLY = "colors_only" +CONF_ON_LIGHTS_ONLY = "on_lights_only" + def int_between(a, b): return vol.All(vol.Coerce(int), vol.Range(min=a, max=b)) diff --git a/custom_components/adaptive_lighting/services.yaml b/custom_components/adaptive_lighting/services.yaml index b5535234..e5b2aa18 100755 --- a/custom_components/adaptive_lighting/services.yaml +++ b/custom_components/adaptive_lighting/services.yaml @@ -2,11 +2,17 @@ apply: description: Applies the current Adaptive Lighting settings to lights. fields: entity_id: - description: entity_id of the Adaptive Lighting switch + description: entity_id of the Adaptive Lighting switch. example: switch.adaptive_lighting_default lights: - description: entity_id(s) of lights + description: entity_id(s) of lights. example: light.bedroom_ceiling transition: - description: transition of the lights + description: Transition of the lights. example: 10 + colors_only: + description: Only change the color of the lights and leave the brightness as is. + example: false + on_lights_only: + description: Only adjust the lights that are already on, otherwise turn the lights on. + example: false diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 00b95565..fad60388 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -50,6 +50,7 @@ from homeassistant.util.color import ( ) from .const import ( + CONF_COLORS_ONLY, CONF_DISABLE_BRIGHTNESS_ADJUST, CONF_DISABLE_ENTITY, CONF_DISABLE_STATE, @@ -60,6 +61,7 @@ from .const import ( CONF_MAX_COLOR_TEMP, CONF_MIN_BRIGHTNESS, CONF_MIN_COLOR_TEMP, + CONF_ON_LIGHTS_ONLY, CONF_ONLY_ONCE, CONF_SLEEP_BRIGHTNESS, CONF_SLEEP_COLOR_TEMP, @@ -95,13 +97,20 @@ _LOGGER = logging.getLogger(__name__) SCAN_INTERVAL = timedelta(seconds=10) -async def handle_apply(entity, service_call): +async def handle_apply(switch, service_call): """Handle the entity service apply.""" - if not isinstance(entity, AdaptiveSwitch): + if not isinstance(switch, AdaptiveSwitch): raise ValueError("Apply can only be called for a AdaptiveSwitch.") - lights = service_call.data[CONF_LIGHTS] - transition = service_call.data.get(CONF_TRANSITION, entity._initial_transition) - tasks = [await entity._adjust_light(light, transition) for light in lights] + data = service_call.data + tasks = [ + await switch._adjust_light( + light, + data[CONF_TRANSITION], + data[CONF_COLORS_ONLY], + ) + for light in data[CONF_LIGHTS] + if not data[CONF_ON_LIGHTS_ONLY] or is_on(switch.hass, light) + ] if tasks: await asyncio.wait(tasks) @@ -120,7 +129,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities): SERVICE_APPLY, { vol.Required(CONF_LIGHTS): cv.entity_ids, - vol.Optional(CONF_TRANSITION): VALID_TRANSITION, + vol.Optional( + CONF_TRANSITION, default=self._initial_transition + ): VALID_TRANSITION, + vol.Optional(CONF_COLORS_ONLY, default=False): cv.boolean, + vol.Optional(CONF_ON_LIGHTS_ONLY, default=False): cv.boolean, }, handle_apply, ) @@ -411,7 +424,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): and self.hass.states.get(self._disable_entity).state in self._disable_state ) - async def _adjust_light(self, light, transition): + async def _adjust_light(self, light, transition, colors_only): service_data = {ATTR_ENTITY_ID: light} features = self._supported_features(light) @@ -420,7 +433,11 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): transition = self._transition service_data[ATTR_TRANSITION] = transition - if self._brightness is not None and "brightness" in features: + if ( + self._brightness is not None + and "brightness" in features + and not colors_only + ): service_data[ATTR_BRIGHTNESS_PCT] = self._brightness if "color" in features: @@ -441,8 +458,8 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): return False return True - async def _adjust_lights(self, lights, transition, force=False): - if not self._should_adjust() or not force: + async def _adjust_lights(self, lights, transition): + if not self._should_adjust(): return tasks = [ await self._adjust_light(light, transition)