Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement call intercept for multiple lights #679

Merged
merged 73 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
3b4602c
Implement call intercept for multiple lights
basnijholt Jul 29, 2023
25d77ce
remove comment
basnijholt Jul 29, 2023
d07ca3d
skip if no eids
basnijholt Jul 29, 2023
60c8c6d
add comment
basnijholt Jul 29, 2023
d03613d
fix type
basnijholt Jul 29, 2023
c8a3e9a
Fix skipped
basnijholt Jul 29, 2023
3261e9f
indentation
basnijholt Jul 29, 2023
28fbd60
Add logging and fix error
basnijholt Jul 30, 2023
e98c5b1
Fix for HA ≤2023.04
basnijholt Jul 30, 2023
bed6163
simplify
basnijholt Jul 30, 2023
8e30574
remove unused ignores
basnijholt Jul 30, 2023
a31ffe2
Debug mode
basnijholt Jul 30, 2023
6c08ae3
Add test
basnijholt Jul 30, 2023
6a7e1c3
Make test failing
basnijholt Jul 30, 2023
8516e70
rename switch
basnijholt Jul 30, 2023
9006c6d
rename lights
basnijholt Jul 30, 2023
164b978
Fix tests
basnijholt Jul 30, 2023
c6a4801
Rename lights in tests
basnijholt Jul 30, 2023
da57df8
Remove unused dependencies
basnijholt Jul 30, 2023
82c7523
Merge branch 'test-rename-lights' into multi-light-intercept
basnijholt Jul 30, 2023
7abb469
Merge remote-tracking branch 'origin/main' into multi-light-intercept
basnijholt Jul 30, 2023
a680881
Improve tests
basnijholt Jul 30, 2023
a38b51f
More tests
basnijholt Jul 30, 2023
8e4ff80
Remove the DEBUG_MODE
basnijholt Jul 30, 2023
462ab0a
Merge remote-tracking branch 'origin/main' into multi-light-intercept
basnijholt Jul 30, 2023
50ecf54
Add doc-string
basnijholt Jul 30, 2023
76a9e09
Merge remote-tracking branch 'origin/main' into multi-light-intercept
basnijholt Jul 31, 2023
f04c475
Extra test
basnijholt Jul 31, 2023
6a14d71
assert
basnijholt Jul 31, 2023
155c6f8
extra test
basnijholt Jul 31, 2023
645b262
Merge remote-tracking branch 'origin/main' into multi-light-intercept
basnijholt Aug 1, 2023
8a16c9f
Comments
basnijholt Aug 1, 2023
1959278
fix
basnijholt Aug 1, 2023
1c6c274
fix
basnijholt Aug 1, 2023
74387ac
expand light groups
basnijholt Aug 1, 2023
2a78cce
more logging
basnijholt Aug 1, 2023
8ff125f
sort
basnijholt Aug 1, 2023
18803e8
Revert is_proactively_adapting checks
basnijholt Aug 1, 2023
462b1b6
simplify the mapping
basnijholt Aug 1, 2023
d22a05c
Revert "Revert is_proactively_adapting checks"
basnijholt Aug 1, 2023
eb814cd
test
basnijholt Aug 1, 2023
216ec2d
no light groups
basnijholt Aug 1, 2023
48ad8f7
do not expand
basnijholt Aug 1, 2023
a2479d4
Do not expand_light_groups in intercept
basnijholt Aug 2, 2023
4a149f0
more logging
basnijholt Aug 2, 2023
8f312bf
Fix
basnijholt Aug 2, 2023
c2ba878
add comment
basnijholt Aug 2, 2023
fa2c270
Add multi_light_intercept config option
basnijholt Aug 2, 2023
414630f
Merge fa2c27022156182e2773d88b8949bee2615627ff into e5133936536cb02d6…
basnijholt Aug 2, 2023
07f40eb
Update README.md, strings.json, and services.yaml
github-actions[bot] Aug 2, 2023
d12d58d
add light group
basnijholt Aug 2, 2023
585544a
fix platform
basnijholt Aug 2, 2023
abb4d05
add simple test
basnijholt Aug 2, 2023
26d338e
turn off again
basnijholt Aug 2, 2023
5f50cef
Test without take over control
basnijholt Aug 2, 2023
7ca3d92
improve test and fix it in one way
basnijholt Aug 2, 2023
0a06c6e
Fixes
basnijholt Aug 3, 2023
3e8933f
add cleanup fixture
basnijholt Aug 3, 2023
aea72ea
format
basnijholt Aug 3, 2023
38c858e
Update test_switch.py
basnijholt Aug 3, 2023
ebc3302
Merge remote-tracking branch 'origin/main' into multi-light-intercept
basnijholt Aug 3, 2023
d772761
Merge remote-tracking branch 'origin/main' into multi-light-intercept
basnijholt Aug 3, 2023
644ab1d
add __str__
basnijholt Aug 3, 2023
57240cb
remove unneeded call
basnijholt Aug 3, 2023
5d3627b
simplify service_data construction
basnijholt Aug 3, 2023
fcd1139
Generalize is_our_context
basnijholt Aug 3, 2023
645ee26
Fix multi_light_intercept: false
basnijholt Aug 4, 2023
7102f71
add comments
basnijholt Aug 4, 2023
2c577f3
add docs
basnijholt Aug 4, 2023
ac13435
Merge 2c577f3be288e8f3bd8e72853b9b5c4ae1092474 into b80b253fa1b840c63…
basnijholt Aug 4, 2023
d5a18f8
Update README.md, strings.json, and services.yaml
github-actions[bot] Aug 4, 2023
0f13c59
Add feature line
basnijholt Aug 4, 2023
8c3bb2d
move function
basnijholt Aug 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
basnijholt committed Jul 30, 2023
commit 164b9782c3dce1820c9a89a0edf26f1f3d883e9e
27 changes: 16 additions & 11 deletions custom_components/adaptive_lighting/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2000,6 +2000,7 @@ async def _service_interceptor_turn_on_handler( # noqa: PLR0912
# Don't adapt our own service calls
if is_our_context(call.context):
return
data_copy = data.copy()
_LOGGER.debug(
"_service_interceptor_turn_on_handler: call='%s', data='%s'",
call,
Expand Down Expand Up @@ -2054,8 +2055,10 @@ def modify_service_data(service_data, entity_ids):
skipped.append(entity_id)
else:
filtered_entity_ids.append(entity_id)

if not filtered_entity_ids:
continue

transition = data[CONF_PARAMS].get(
ATTR_TRANSITION,
switch.initial_transition,
Expand All @@ -2066,37 +2069,39 @@ def modify_service_data(service_data, entity_ids):
switch=switch,
transition=transition,
call=call,
data=modify_service_data(data.copy(), filtered_entity_ids),
data=modify_service_data(data, filtered_entity_ids),
)
has_intercepted = True
continue
await switch._update_attrs_and_maybe_adapt_lights(
context=switch.create_context("intercept", call.context),
lights=filtered_entity_ids,
transition=transition,
force=True,
)

for eid in filtered_entity_ids:
await switch._adapt_light(
light=eid,
# Must add a new context otherwise _adapt_light will bail out
context=switch.create_context("intercept"),
transition=transition,
)

if skipped:
if not has_intercepted:
modify_service_data(data, skipped)
return # The call will be intercepted with the original data
# Modify the service data inplace
modify_service_data(data, skipped)
# Call light turn_on service for skipped entities
_LOGGER.debug(
"_service_interceptor_turn_on_handler: calling `light.turn_on` with skipped='%s', data: '%s'",
skipped,
data,
)
service_data = data.copy()
service_data = modify_service_data(data_copy, skipped)
service_data.update(service_data.pop(CONF_PARAMS, {}))
service_data.pop(ATTR_COLOR_TEMP, None)
await self.hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
service_data,
blocking=True,
context=call.context,
# Must add a new context to avoid infinite recursion
context=switch.create_context("skipped"),
)

async def _service_interceptor_turn_on_single_light_handler(
Expand Down
1 change: 0 additions & 1 deletion tests/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,6 @@ async def test_proactive_multiple_lights(hass):
assert hass.states.get(lights[0]).state == STATE_ON, lights[0]
assert hass.states.get(lights[1]).state == STATE_ON, lights[1]
assert hass.states.get(lights[2]).state == STATE_ON, lights[2]
assert 0


async def test_two_switches_for_single_light(hass):
Expand Down