From af6072e2f3f340c7a489a7fd0be370dad5aa26aa Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Wed, 28 Jun 2023 17:51:43 +0200 Subject: [PATCH] Add feature to write an attribute to a node (#341) --- matter_server/client/client.py | 17 +++++++++++++++++ matter_server/common/models.py | 1 + matter_server/server/device_controller.py | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/matter_server/client/client.py b/matter_server/client/client.py index bc0f7a0a..eb168534 100644 --- a/matter_server/client/client.py +++ b/matter_server/client/client.py @@ -35,6 +35,8 @@ SUB_WILDCARD: Final = "*" +# pylint: disable=too-many-public-methods + class MatterClient: """Manage a Matter server over WebSockets.""" @@ -231,6 +233,21 @@ async def send_device_command( interaction_timeout_ms=interaction_timeout_ms, ) + async def write_attribute( + self, + node_id: int, + attribute_path: str, + value: Any, + ) -> Any: + """Write an attribute(value) on a target node.""" + return await self.send_command( + APICommand.WRITE_ATTRIBUTE, + require_schema=4, + node_id=node_id, + attribute_path=attribute_path, + value=value, + ) + async def remove_node(self, node_id: int) -> None: """Remove a Matter node/device from the fabric.""" await self.send_command(APICommand.REMOVE_NODE, node_id=node_id) diff --git a/matter_server/common/models.py b/matter_server/common/models.py index baf701c6..40e728d7 100644 --- a/matter_server/common/models.py +++ b/matter_server/common/models.py @@ -41,6 +41,7 @@ class APICommand(str, Enum): REMOVE_NODE = "remove_node" GET_VENDOR_NAMES = "get_vendor_names" SUBSCRIBE_ATTRIBUTE = "subscribe_attribute" + WRITE_ATTRIBUTE = "write_attribute" EventCallBackType = Callable[[EventType, Any], None] diff --git a/matter_server/server/device_controller.py b/matter_server/server/device_controller.py index 852565e6..7e86d74c 100644 --- a/matter_server/server/device_controller.py +++ b/matter_server/server/device_controller.py @@ -383,6 +383,24 @@ async def send_device_command( interactionTimeoutMs=interaction_timeout_ms, ) + @api_command(APICommand.WRITE_ATTRIBUTE) + async def write_attribute( + self, + node_id: int, + attribute_path: str, + value: Any, + ) -> Any: + """Write an attribute(value) on a target node.""" + if self.chip_controller is None: + raise RuntimeError("Device Controller not initialized.") + endpoint_id, cluster_id, attribute_id = parse_attribute_path(attribute_path) + attribute = ALL_ATTRIBUTES[cluster_id][attribute_id]() + attribute.value = value + return await self.chip_controller.WriteAttribute( + nodeid=node_id, + attributes=[(endpoint_id, attribute)], + ) + @api_command(APICommand.REMOVE_NODE) async def remove_node(self, node_id: int) -> None: """Remove a Matter node/device from the fabric."""