Skip to content

Commit 1ef7ed5

Browse files
Implement call intercept for multiple lights (#679)
* Implement call intercept for multiple lights * remove comment * skip if no eids * add comment * fix type * Fix skipped * indentation * Add logging and fix error * Fix for HA ≤2023.04 * simplify * remove unused ignores * Debug mode * Add test * Make test failing * rename switch * rename lights * Fix tests * Rename lights in tests * Remove unused dependencies * Improve tests * More tests * Remove the DEBUG_MODE * Add doc-string * Extra test * assert * extra test * Comments * fix * fix * expand light groups * more logging * sort * Revert is_proactively_adapting checks This reverts commit 39fd8f2. * simplify the mapping * Revert "Revert is_proactively_adapting checks" This reverts commit 18803e8. * test * no light groups * do not expand * Do not expand_light_groups in intercept * more logging * Fix * add comment * Add multi_light_intercept config option * Update README.md, strings.json, and services.yaml * add light group * fix platform * add simple test * turn off again * Test without take over control * improve test and fix it in one way * Fixes * add cleanup fixture * format * Update test_switch.py * add __str__ * remove unneeded call * simplify service_data construction * Generalize is_our_context * Fix multi_light_intercept: false * add comments * add docs * Update README.md, strings.json, and services.yaml * Add feature line * move function --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 4251cca commit 1ef7ed5

File tree

8 files changed

+646
-120
lines changed

8 files changed

+646
-120
lines changed

.ruff.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ignore = [
1616
"FBT002", # Boolean default value in function definition
1717
"FIX004", # Line contains HACK, consider resolving the issue
1818
"PD901", # df is a bad variable name. Be kinder to your future self.
19-
"PERF203",# `try`-`except` within a loop incurs performance overhead
19+
"PERF203", # `try`-`except` within a loop incurs performance overhead
2020
"PLR0913", # Too many arguments to function call (N > 5)
2121
"PLR2004", # Magic value used in comparison, consider replacing X with a constant variable
2222
"S101", # Use of assert detected

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ In addition to its regular mode, Adaptive Lighting also offers a "sleep mode"
2020

2121
## :bulb: Features
2222

23+
When initially turning on a light that is controlled by Adaptive Lighting, the `light.turn_on` service call is intercepted, and the light's brightness and color are automatically adjusted based on the sun's position.
24+
After that, the light's brightness and color are automatically adjusted at a regular interval.
25+
2326
Adaptive Lighting provides four switches (using "living_room" as an example component name):
2427

2528
- `switch.adaptive_lighting_living_room`: Turn Adaptive Lighting on or off and view current light settings through its attributes.
@@ -124,7 +127,8 @@ The YAML and frontend configuration methods support all of the options listed be
124127
| `send_split_delay` | Delay (ms) between `separate_turn_on_commands` for lights that don't support simultaneous brightness and color setting. ⏲️ | `0` | `int` 0-10000 |
125128
| `adapt_delay` | Wait time (seconds) between light turn on and Adaptive Lighting applying changes. Might help to avoid flickering. ⏲️ | `0` | `float > 0` |
126129
| `autoreset_control_seconds` | Automatically reset the manual control after a number of seconds. Set to 0 to disable. ⏲️ | `0` | `int` 0-31536000 |
127-
| `skip_redundant_commands` | Skip sending adaptation commands whose target state already equals the light's known state. Minimizes network traffic and improves the adaptation responsivity in some situations. Disable if physical light states get out of sync with HA's recorded state. | `False` | `bool` |
130+
| `skip_redundant_commands` | Skip sending adaptation commands whose target state already equals the light's known state. Minimizes network traffic and improves the adaptation responsivity in some situations. 📉Disable if physical light states get out of sync with HA's recorded state. | `False` | `bool` |
131+
| `multi_light_intercept` | Intercept and adapt `light.turn_on` calls that target multiple lights. ➗⚠️ This might result in splitting up a single `light.turn_on` call into multiple calls, e.g., when lights are in different switches. | `True` | `bool` |
128132

129133
<!-- END_OUTPUT -->
130134

custom_components/adaptive_lighting/adaptation_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ async def next_service_call_data(self) -> ServiceData | None:
150150
"""Return data for the next service call, or none if no more data exists."""
151151
return await anext(self.service_call_datas, None)
152152

153+
def __str__(self) -> str:
154+
"""Return a string representation of the data."""
155+
return (
156+
f"{self.__class__.__name__}("
157+
f"entity_id={self.entity_id}, "
158+
f"context_id={self.context.id}, "
159+
f"sleep_time={self.sleep_time}, "
160+
f"force={self.force}, "
161+
f"max_length={self.max_length}, "
162+
f"which={self.which}, "
163+
f"initial_sleep={self.initial_sleep}"
164+
")"
165+
)
166+
153167

154168
class NoColorOrBrightnessInServiceDataError(Exception):
155169
"""Exception raised when no color or brightness attributes are found in service data."""

custom_components/adaptive_lighting/const.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,20 @@
186186
DOCS[CONF_SKIP_REDUNDANT_COMMANDS] = (
187187
"Skip sending adaptation commands whose target state already "
188188
"equals the light's known state. Minimizes network traffic and improves the "
189-
"adaptation responsivity in some situations. "
189+
"adaptation responsivity in some situations. 📉"
190190
"Disable if physical light states get out of sync with HA's recorded state."
191191
)
192192

193+
CONF_MULTI_LIGHT_INTERCEPT, DEFAULT_MULTI_LIGHT_INTERCEPT = (
194+
"multi_light_intercept",
195+
True,
196+
)
197+
DOCS[CONF_MULTI_LIGHT_INTERCEPT] = (
198+
"Intercept and adapt `light.turn_on` calls that target multiple lights. ➗"
199+
"⚠️ This might result in splitting up a single `light.turn_on` call "
200+
"into multiple calls, e.g., when lights are in different switches."
201+
)
202+
193203
SLEEP_MODE_SWITCH = "sleep_mode_switch"
194204
ADAPT_COLOR_SWITCH = "adapt_color_switch"
195205
ADAPT_BRIGHTNESS_SWITCH = "adapt_brightness_switch"
@@ -290,6 +300,7 @@ def int_between(min_int, max_int):
290300
DEFAULT_SKIP_REDUNDANT_COMMANDS,
291301
bool,
292302
),
303+
(CONF_MULTI_LIGHT_INTERCEPT, DEFAULT_MULTI_LIGHT_INTERCEPT, bool),
293304
]
294305

295306

custom_components/adaptive_lighting/strings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"send_split_delay": "send_split_delay: Delay (ms) between `separate_turn_on_commands` for lights that don't support simultaneous brightness and color setting. ⏲️",
4949
"adapt_delay": "adapt_delay: Wait time (seconds) between light turn on and Adaptive Lighting applying changes. Might help to avoid flickering. ⏲️",
5050
"autoreset_control_seconds": "autoreset_control_seconds: Automatically reset the manual control after a number of seconds. Set to 0 to disable. ⏲️",
51-
"skip_redundant_commands": "skip_redundant_commands: Skip sending adaptation commands whose target state already equals the light's known state. Minimizes network traffic and improves the adaptation responsivity in some situations. Disable if physical light states get out of sync with HA's recorded state."
51+
"skip_redundant_commands": "skip_redundant_commands: Skip sending adaptation commands whose target state already equals the light's known state. Minimizes network traffic and improves the adaptation responsivity in some situations. 📉Disable if physical light states get out of sync with HA's recorded state.",
52+
"multi_light_intercept": "multi_light_intercept: Intercept and adapt `light.turn_on` calls that target multiple lights. ➗⚠️ This might result in splitting up a single `light.turn_on` call into multiple calls, e.g., when lights are in different switches."
5253
}
5354
}
5455
},

0 commit comments

Comments
 (0)