diff --git a/tests/test_cluster_handlers.py b/tests/test_cluster_handlers.py index ebad65a41..af7d2c78a 100644 --- a/tests/test_cluster_handlers.py +++ b/tests/test_cluster_handlers.py @@ -1242,8 +1242,8 @@ async def test_zha_send_event_from_quirk(zha_gateway: Gateway): on_off_ch.cluster_command(1, OnOff.ServerCommandDefs.on.id, []) assert on_off_ch.emit_zha_event.call_count == 2 + # attribute_updated is emitted first, then the cluster command is forwarded assert on_off_ch.emit_zha_event.mock_calls == [ - call("on", []), call( "attribute_updated", { @@ -1253,6 +1253,7 @@ async def test_zha_send_event_from_quirk(zha_gateway: Gateway): "value": t.Bool.true, }, ), + call("on", []), ] on_off_ch.emit_zha_event.reset_mock() diff --git a/zha/zigbee/cluster_handlers/general.py b/zha/zigbee/cluster_handlers/general.py index a6f36cbbe..acd0344a0 100644 --- a/zha/zigbee/cluster_handlers/general.py +++ b/zha/zigbee/cluster_handlers/general.py @@ -552,11 +552,9 @@ def on_off(self) -> bool | None: 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) + # Process cluster commands, so attribute_updated events fire first if cmd in ( OnOff.ServerCommandDefs.off.name, OnOff.ServerCommandDefs.off_with_effect.name, @@ -588,6 +586,9 @@ def cluster_command(self, tsn, command_id, args): OnOff.AttributeDefs.on_off.id, not bool(self.on_off) ) + # Emit ZHA event with cluster command + super().cluster_command(tsn, command_id, args) + def set_to_off(self, *_): """Set the state to off.""" self._off_listener = None