From 312c594b25beb4f61cb3d91ab099d607bac15830 Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Mon, 3 Apr 2023 17:50:41 -0500 Subject: [PATCH 1/3] first commit --- custom_components/adaptive_lighting/const.py | 13 +++++++ custom_components/adaptive_lighting/switch.py | 34 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index bb0c07cd..1d9e66ca 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -35,6 +35,18 @@ DOCS[CONF_DETECT_NON_HA_CHANGES] = ( "Requires `take_over_control`. 🕵️" ) +( + CONF_IGNORE_LIGHT_TURNON_WITH_CUSTOM_DATA, + DEFAULT_IGNORE_LIGHT_TURNON_WITH_CUSTOM_DATA, +) = ( + "ignore_turnon_with_custom_data", + False, +) +DOCS[CONF_IGNORE_LIGHT_TURNON_WITH_CUSTOM_DATA] = ( + "Ignores any lights turned off -> on with custom brightness/color data." + " Only a few lights are supported at this time." +) + CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, DEFAULT_INCLUDE_CONFIG_IN_ATTRIBUTES = ( "include_config_in_attributes", False, @@ -224,6 +236,7 @@ def int_between(min_int, max_int): VALIDATION_TUPLES = [ (CONF_LIGHTS, DEFAULT_LIGHTS, cv.entity_ids), (CONF_PREFER_RGB_COLOR, DEFAULT_PREFER_RGB_COLOR, bool), + (CONF_IGNORE_LIGHT_TURNON_WITH_CUSTOM_DATA, bool), (CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, DEFAULT_INCLUDE_CONFIG_IN_ATTRIBUTES, bool), (CONF_INITIAL_TRANSITION, DEFAULT_INITIAL_TRANSITION, VALID_TRANSITION), (CONF_SLEEP_TRANSITION, DEFAULT_SLEEP_TRANSITION, VALID_TRANSITION), diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 031396fe..910ee85b 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -99,6 +99,7 @@ from .const import ( CONF_ADAPT_UNTIL_SLEEP, CONF_AUTORESET_CONTROL, CONF_DETECT_NON_HA_CHANGES, + CONF_IGNORE_LIGHT_TURNON_WITH_CUSTOM_DATA, CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, CONF_INITIAL_TRANSITION, CONF_INTERVAL, @@ -797,6 +798,9 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): attrdata[k] = v.total_seconds() self._config.update(attrdata) + self._ignore_light_turnon_with_custom_data = data[ + CONF_IGNORE_LIGHT_TURNON_WITH_CUSTOM_DATA + ] self._initial_transition = data[CONF_INITIAL_TRANSITION] self._sleep_transition = data[CONF_SLEEP_TRANSITION] self._only_once = data[CONF_ONLY_ONCE] @@ -1671,6 +1675,7 @@ class TurnOnOffListener: entity_ids, event.context.id, ) + for eid in entity_ids: task = self.sleep_tasks.get(eid) if task is not None: @@ -1685,6 +1690,35 @@ class TurnOnOffListener: # Restart the auto reset timer timer.start() + # Do not adapt any lights turned on with custom brightness/color_temp/rgb_color + # Probably doesn't work for most lights. + # Check if the option is enabled first. + switches = _get_switches_with_lights(self.hass, entity_ids) + found = False + for switch in switches: + if switch._ignore_light_turnon_with_custom_data: + found = True + # Option not enabled on any of the light's switches. + if not found: + return + # Check for custom data in service data. + if ( + ATTR_COLOR_TEMP_KELVIN in service_data + or ATTR_RGB_COLOR in service_data + or ATTR_BRIGHTNESS in service_data + or ATTR_XY_COLOR in service_data + or ATTR_HS_COLOR in service_data + or "color_temp" in service_data + ): + _LOGGER.info( + "%s: Light %s was turned on with custom settings." + "Setting as manually controlled.", + self._name, + entity_id, + ) + self.mark_as_manual_control(entity_ids) + _fire_manual_control_event(switch, entity_ids, event.context.id) + async def state_changed_event_listener(self, event: Event) -> None: """Track 'state_changed' events.""" entity_id = event.data.get(ATTR_ENTITY_ID, "") From a2e12ae6fcf64210ccff59429c6a92caa7a41d2f Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Mon, 3 Apr 2023 17:52:33 -0500 Subject: [PATCH 2/3] unpack list `entity_ids` --- custom_components/adaptive_lighting/switch.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 910ee85b..7824e777 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1716,8 +1716,9 @@ class TurnOnOffListener: self._name, entity_id, ) - self.mark_as_manual_control(entity_ids) - _fire_manual_control_event(switch, entity_ids, event.context.id) + for eid in entity_ids: + self.mark_as_manual_control(eid) + _fire_manual_control_event(switch, eid, event.context.id) async def state_changed_event_listener(self, event: Event) -> None: """Track 'state_changed' events.""" From f73032c045be914a1ae1c330af77cf50bb97c5fa Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Mon, 3 Apr 2023 18:00:45 -0500 Subject: [PATCH 3/3] Update const.py --- custom_components/adaptive_lighting/const.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 1d9e66ca..64c3db10 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -236,7 +236,11 @@ def int_between(min_int, max_int): VALIDATION_TUPLES = [ (CONF_LIGHTS, DEFAULT_LIGHTS, cv.entity_ids), (CONF_PREFER_RGB_COLOR, DEFAULT_PREFER_RGB_COLOR, bool), - (CONF_IGNORE_LIGHT_TURNON_WITH_CUSTOM_DATA, bool), + ( + CONF_IGNORE_LIGHT_TURNON_WITH_CUSTOM_DATA, + DEFAULT_IGNORE_LIGHT_TURNON_WITH_CUSTOM_DATA, + bool, + ), (CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, DEFAULT_INCLUDE_CONFIG_IN_ATTRIBUTES, bool), (CONF_INITIAL_TRANSITION, DEFAULT_INITIAL_TRANSITION, VALID_TRANSITION), (CONF_SLEEP_TRANSITION, DEFAULT_SLEEP_TRANSITION, VALID_TRANSITION),