Skip to content

Commit

Permalink
Add read attribute command/feature (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Jun 28, 2023
1 parent af6072e commit f1e2bf0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions matter_server/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,19 @@ async def send_device_command(
interaction_timeout_ms=interaction_timeout_ms,
)

async def read_attribute(
self,
node_id: int,
attribute_path: str,
) -> Any:
"""Read a single attribute on a node."""
return await self.send_command(
APICommand.READ_ATTRIBUTE,
require_schema=4,
node_id=node_id,
attribute_path=attribute_path,
)

async def write_attribute(
self,
node_id: int,
Expand Down
1 change: 1 addition & 0 deletions matter_server/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class APICommand(str, Enum):
REMOVE_NODE = "remove_node"
GET_VENDOR_NAMES = "get_vendor_names"
SUBSCRIBE_ATTRIBUTE = "subscribe_attribute"
READ_ATTRIBUTE = "read_attribute"
WRITE_ATTRIBUTE = "write_attribute"


Expand Down
26 changes: 26 additions & 0 deletions matter_server/server/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,32 @@ async def send_device_command(
interactionTimeoutMs=interaction_timeout_ms,
)

@api_command(APICommand.READ_ATTRIBUTE)
async def read_attribute(
self,
node_id: int,
attribute_path: str,
) -> Any:
"""Read a single attribute on a node."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
node_lock = self._get_node_lock(node_id)
await self._resolve_node(node_id=node_id)
endpoint_id, cluster_id, attribute_id = parse_attribute_path(attribute_path)
attribute: Type[ClusterAttributeDescriptor] = ALL_ATTRIBUTES[cluster_id][
attribute_id
]
async with node_lock:
result: Attribute.AsyncReadTransaction.ReadResponse = (
await self.chip_controller.Read(
nodeid=node_id,
attributes=[(endpoint_id, attribute)],
fabricFiltered=False,
)
)
read_atributes = self._parse_attributes_from_read_result(result.attributes)
return read_atributes[attribute_path]

@api_command(APICommand.WRITE_ATTRIBUTE)
async def write_attribute(
self,
Expand Down

0 comments on commit f1e2bf0

Please sign in to comment.