-
Notifications
You must be signed in to change notification settings - Fork 61
Fix OnOff client cluster commands not updating attribute cache
#635
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
puddly
merged 9 commits into
zigpy:dev
from
TheJulianJES:tjj/onoff_cluster_handler_client_fix
Jan 29, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
585bbad
Copy `OnOff` `cluster_command` logic to `OnOffClientClusterHandler`
TheJulianJES f58991c
Modify remote test in test_cluster_handlers.py to also expect attribu…
TheJulianJES 17a1bf3
Add test for new behavior to test_cluster_handlers.py
TheJulianJES d92fbcf
Move test to test_binary_sensor.py
TheJulianJES 3443f94
Remove `cluster_command` logic from (server) `OnOffClusterHandler`
TheJulianJES e4ce5c3
Also test toggle command
TheJulianJES b9cf191
Also test `on_with_timed_off` cancelling existing timer
TheJulianJES 05e5579
Replace `create_mock_zigpy_device` with device from IKEA motion senso…
TheJulianJES 538d715
Use `make_zcl_header`
TheJulianJES 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
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
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 |
|---|---|---|
|
|
@@ -540,6 +540,59 @@ class MultistateValueClusterHandler(ClusterHandler): | |
| class OnOffClientClusterHandler(ClientClusterHandler): | ||
| """OnOff client cluster handler.""" | ||
|
|
||
| def __init__(self, cluster: zigpy.zcl.Cluster, endpoint: Endpoint) -> None: | ||
| """Initialize OnOffClientClusterHandler.""" | ||
| super().__init__(cluster, endpoint) | ||
| self._off_listener: asyncio.TimerHandle | None = None | ||
|
|
||
| @property | ||
| def on_off(self) -> bool | None: | ||
| """Return cached value of on/off attribute.""" | ||
| return self.cluster.get(OnOff.AttributeDefs.on_off.name) | ||
|
|
||
| def cluster_command(self, tsn, command_id, args): | ||
| """Handle commands received to this cluster.""" | ||
| # for emitting ZHA event | ||
| super().cluster_command(tsn, command_id, args) | ||
|
|
||
| cmd = parse_and_log_command(self, tsn, command_id, args) | ||
|
|
||
| if cmd in ( | ||
| OnOff.ServerCommandDefs.off.name, | ||
| OnOff.ServerCommandDefs.off_with_effect.name, | ||
| ): | ||
| self.cluster.update_attribute(OnOff.AttributeDefs.on_off.id, t.Bool.false) | ||
| elif cmd in ( | ||
| OnOff.ServerCommandDefs.on.name, | ||
| OnOff.ServerCommandDefs.on_with_recall_global_scene.name, | ||
| ): | ||
| self.cluster.update_attribute(OnOff.AttributeDefs.on_off.id, t.Bool.true) | ||
| elif cmd == OnOff.ServerCommandDefs.on_with_timed_off.name: | ||
| should_accept = args[0] | ||
| on_time = args[1] | ||
| # 0 is always accept 1 is only accept when already on | ||
| if should_accept == 0 or (should_accept == 1 and bool(self.on_off)): | ||
| if self._off_listener is not None: | ||
| self._off_listener.cancel() | ||
| self._off_listener = None | ||
| self.cluster.update_attribute( | ||
| OnOff.AttributeDefs.on_off.id, t.Bool.true | ||
| ) | ||
| if on_time > 0: | ||
| self._off_listener = asyncio.get_running_loop().call_later( | ||
| (on_time / 10), # value is in 10ths of a second | ||
| self.set_to_off, | ||
| ) | ||
| elif cmd == "toggle": | ||
| self.cluster.update_attribute( | ||
| OnOff.AttributeDefs.on_off.id, not bool(self.on_off) | ||
| ) | ||
|
|
||
| def set_to_off(self, *_): | ||
| """Set the state to off.""" | ||
| self._off_listener = None | ||
| self.cluster.update_attribute(OnOff.AttributeDefs.on_off.id, t.Bool.false) | ||
|
|
||
|
|
||
| @registries.BINDABLE_CLUSTERS.register(OnOff.cluster_id) | ||
| @registries.CLUSTER_HANDLER_REGISTRY.register(OnOff.cluster_id) | ||
|
|
@@ -558,7 +611,6 @@ class OnOffClusterHandler(ClusterHandler): | |
| def __init__(self, cluster: zigpy.zcl.Cluster, endpoint: Endpoint) -> None: | ||
| """Initialize OnOffClusterHandler.""" | ||
| super().__init__(cluster, endpoint) | ||
| self._off_listener: asyncio.TimerHandle | None = None | ||
|
|
||
| if TUYA_PLUG_ONOFF in endpoint.device.exposes_features: | ||
| self.ZCL_INIT_ATTRS = self.ZCL_INIT_ATTRS.copy() | ||
|
|
@@ -594,46 +646,6 @@ async def turn_off(self) -> None: | |
| raise ZHAException(f"Failed to turn off: {result[1]}") | ||
| self.cluster.update_attribute(OnOff.AttributeDefs.on_off.id, t.Bool.false) | ||
|
|
||
| def cluster_command(self, tsn, command_id, args): | ||
| """Handle commands received to this cluster.""" | ||
| cmd = parse_and_log_command(self, tsn, command_id, args) | ||
|
|
||
| if cmd in ( | ||
| OnOff.ServerCommandDefs.off.name, | ||
| OnOff.ServerCommandDefs.off_with_effect.name, | ||
| ): | ||
| self.cluster.update_attribute(OnOff.AttributeDefs.on_off.id, t.Bool.false) | ||
| elif cmd in ( | ||
| OnOff.ServerCommandDefs.on.name, | ||
| OnOff.ServerCommandDefs.on_with_recall_global_scene.name, | ||
| ): | ||
| self.cluster.update_attribute(OnOff.AttributeDefs.on_off.id, t.Bool.true) | ||
| elif cmd == OnOff.ServerCommandDefs.on_with_timed_off.name: | ||
| should_accept = args[0] | ||
| on_time = args[1] | ||
| # 0 is always accept 1 is only accept when already on | ||
| if should_accept == 0 or (should_accept == 1 and bool(self.on_off)): | ||
| if self._off_listener is not None: | ||
| self._off_listener.cancel() | ||
| self._off_listener = None | ||
| self.cluster.update_attribute( | ||
| OnOff.AttributeDefs.on_off.id, t.Bool.true | ||
| ) | ||
| if on_time > 0: | ||
| self._off_listener = asyncio.get_running_loop().call_later( | ||
| (on_time / 10), # value is in 10ths of a second | ||
| self.set_to_off, | ||
| ) | ||
| elif cmd == "toggle": | ||
| self.cluster.update_attribute( | ||
| OnOff.AttributeDefs.on_off.id, not bool(self.on_off) | ||
| ) | ||
|
|
||
| def set_to_off(self, *_): | ||
| """Set the state to off.""" | ||
| self._off_listener = None | ||
| self.cluster.update_attribute(OnOff.AttributeDefs.on_off.id, t.Bool.false) | ||
|
Comment on lines
-597
to
-635
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. I've removed the entire I don't think it can be used at all for the server handler, so should be fine to remove. |
||
|
|
||
| async def async_update(self): | ||
| """Initialize cluster handler.""" | ||
| if self.cluster.is_client: | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.