Skip to content

Commit 8b7bb36

Browse files
authored
Uniform the result of read_attribute to always return a dict (#590)
1 parent e36b950 commit 8b7bb36

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

matter_server/client/client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -445,24 +445,26 @@ async def read_attribute(
445445
self,
446446
node_id: int,
447447
attribute_path: str,
448-
) -> Any:
449-
"""Read attribute(s) on a node."""
450-
return await self.send_command(
448+
) -> dict[str, Any]:
449+
"""Read one or more attribute(s) on a node by specifying an attributepath."""
450+
updated_values = await self.send_command(
451451
APICommand.READ_ATTRIBUTE,
452452
require_schema=4,
453453
node_id=node_id,
454454
attribute_path=attribute_path,
455455
)
456+
if not isinstance(updated_values, dict):
457+
# can happen is the server is running schema < 8
458+
return {attribute_path: updated_values}
459+
return cast(dict[str, Any], updated_values)
456460

457461
async def refresh_attribute(
458462
self,
459463
node_id: int,
460464
attribute_path: str,
461-
) -> Any:
465+
) -> None:
462466
"""Read attribute(s) on a node and store the updated value(s)."""
463467
updated_values = await self.read_attribute(node_id, attribute_path)
464-
if not isinstance(updated_values, dict):
465-
updated_values = {attribute_path: updated_values}
466468
for attr_path, value in updated_values.items():
467469
self._nodes[node_id].update_attribute(attr_path, value)
468470

matter_server/server/device_controller.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,8 @@ async def send_device_command(
545545
@api_command(APICommand.READ_ATTRIBUTE)
546546
async def read_attribute(
547547
self, node_id: int, attribute_path: str, fabric_filtered: bool = False
548-
) -> Any:
549-
"""Read a single attribute (or Cluster) on a node."""
548+
) -> dict[str, Any]:
549+
"""Read one or more attribute(s) on a node by specifying an attributepath."""
550550
if self.chip_controller is None:
551551
raise RuntimeError("Device Controller not initialized.")
552552
if (node := self._nodes.get(node_id)) is None or not node.available:
@@ -576,9 +576,7 @@ async def read_attribute(
576576
# update cached info in node attributes
577577
self._nodes[node_id].attributes.update(read_atributes)
578578
self._write_node_state(node_id)
579-
if len(read_atributes) > 1:
580-
return read_atributes
581-
return read_atributes.get(attribute_path, None)
579+
return read_atributes
582580

583581
@api_command(APICommand.WRITE_ATTRIBUTE)
584582
async def write_attribute(

0 commit comments

Comments
 (0)