Repository navigation
Fix on/off-only lights reverting to off after rapid turn_off/turn_on #767
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
Merged
TheJulianJES
merged 3 commits into
zigpy:dev
from
TheJulianJES:tjj/fix-light-transitioning-flag-on-off-only
Oct 28, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2069,6 +2069,61 @@ async def test_turn_on_during_off_transition(zha_gateway: Gateway) -> None: | |
| assert bool(entity.state.on) is False | ||
|
|
||
|
|
||
| @patch( | ||
| "zigpy.zcl.clusters.general.OnOff.request", | ||
| new=AsyncMock(return_value=[sentinel.data, zcl_f.Status.SUCCESS]), | ||
| ) | ||
| async def test_on_off_only_rapid_toggle_does_not_revert( | ||
| zha_gateway: Gateway, | ||
| ) -> None: | ||
| """Test rapid turn_off then turn_on on an on/off-only light does not revert to off. | ||
|
|
||
| On/off-only lights have no brightness transition to wait for, so turn_off | ||
| must not arm the transitioning flag/timer. Otherwise a buffered on_off=False | ||
| report from the off command would override the optimistic on-state when | ||
| the timer fires after the follow-up turn_on. | ||
| """ | ||
| zigpy_device = create_mock_zigpy_device(zha_gateway, LIGHT_ON_OFF) | ||
| on_off_cluster = zigpy_device.endpoints[1].on_off | ||
| on_off_cluster.PLUGGED_ATTR_READS = {"on_off": 1} | ||
| zha_device = await join_zigpy_device(zha_gateway, zigpy_device) | ||
|
|
||
| entity = get_entity(zha_device, platform=Platform.LIGHT) | ||
| assert bool(entity.state.on) is True | ||
|
|
||
| # Turn off, then immediately turn back on (within the default ~1.5s window). | ||
| await entity.async_turn_off() | ||
| await zha_gateway.async_block_till_done() | ||
| assert bool(entity.state.on) is False | ||
| # On/off-only lights have no transition to wait for. | ||
| assert not entity.is_transitioning | ||
|
|
||
| # The device's on_off=False report from the off command arrives. | ||
| await send_attributes_report( | ||
| zha_gateway, | ||
| on_off_cluster, | ||
| {general.OnOff.AttributeDefs.on_off.id: 0}, | ||
| ) | ||
| await zha_gateway.async_block_till_done() | ||
|
|
||
| await entity.async_turn_on() | ||
| await zha_gateway.async_block_till_done() | ||
| assert bool(entity.state.on) is True | ||
|
|
||
| # The device's on_off=True report from the on command arrives. | ||
| await send_attributes_report( | ||
| zha_gateway, | ||
| on_off_cluster, | ||
| {general.OnOff.AttributeDefs.on_off.id: 1}, | ||
| ) | ||
| await zha_gateway.async_block_till_done() | ||
|
|
||
| # Wait past the old 1.5s timer window. State must remain on. | ||
| await asyncio.sleep(2) | ||
| await zha_gateway.async_block_till_done() | ||
|
Comment on lines
+2121
to
+2123
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have this elsewhere and this isn't a real 2 second wait (due to |
||
| assert bool(entity.state.on) is True | ||
|
|
||
|
|
||
| async def test_light_state_restoration(zha_gateway: Gateway) -> None: | ||
| """Test the light state restoration function.""" | ||
| device_light_3 = await device_light_3_mock(zha_gateway) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's useful to have as an explicit test but this also passes (as expected) when the reports both come in after the commands succeed (i.e. if you manage to spam the on/off toggle faster than the light reports).