Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 20a2fcc

Browse files
committedDec 18, 2024··
Set fabric label on interview
Instead of setting the label on commissioning only, set it during the interview process but only if necessary. This avoids the commissioning process to fail in a incomplete state when setting the label fails. The interview process has more resilience to recover from a failure during the commissioning since we have a retry mechanism in place. This also allows to set the label on devices that are already commissioned. Also (try to) unpair the device in case interviewing fails. This allows user to commission a second time without running into "Trying to add a NOC for a fabric that already exists" errors.
1 parent 6c5b87e commit 20a2fcc

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed
 

‎matter_server/server/device_controller.py

+39-9
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,6 @@ async def commission_with_code(
321321
LOGGER.info("Commissioned Node ID: %s vs %s", commissioned_node_id, node_id)
322322
if commissioned_node_id != node_id:
323323
raise RuntimeError("Returned Node ID must match requested Node ID")
324-
325-
if self._default_fabric_label:
326-
await self._chip_device_controller.send_command(
327-
node_id,
328-
0,
329-
Clusters.OperationalCredentials.Commands.UpdateFabricLabel(
330-
self._default_fabric_label
331-
),
332-
)
333324
except ChipStackError as err:
334325
raise NodeCommissionFailed(
335326
f"Commission with code failed for node {node_id}."
@@ -348,6 +339,12 @@ async def commission_with_code(
348339
await self._interview_node(node_id)
349340
except (NodeNotResolving, NodeInterviewFailed) as err:
350341
if retries <= 0:
342+
try:
343+
await self._chip_device_controller.unpair_device(node_id)
344+
except ChipStackError as err_unpair:
345+
LOGGER.warning(
346+
"Removing current fabric from device failed: %s", err_unpair
347+
)
351348
raise err
352349
retries -= 1
353350
LOGGER.warning("Unable to interview Node %s: %s", node_id, err)
@@ -580,6 +577,39 @@ async def _interview_node(self, node_id: int) -> None:
580577
except ChipStackError as err:
581578
raise NodeInterviewFailed(f"Failed to interview node {node_id}") from err
582579

580+
# Set label if specified and needed
581+
if self._default_fabric_label:
582+
cluster = read_response.attributes[0][Clusters.OperationalCredentials]
583+
fabrics: list[
584+
Clusters.OperationalCredentials.Structs.FabricDescriptorStruct
585+
] = cluster[Clusters.OperationalCredentials.Attributes.Fabrics]
586+
fabric_index = cluster[
587+
Clusters.OperationalCredentials.Attributes.CurrentFabricIndex
588+
]
589+
590+
local_fabric = next(
591+
(fabric for fabric in fabrics if fabric.fabricIndex == fabric_index),
592+
None,
593+
)
594+
if local_fabric and local_fabric.label != self._default_fabric_label:
595+
try:
596+
LOGGER.debug(
597+
"Setting fabric label for node %s to '%s'",
598+
node_id,
599+
self._default_fabric_label,
600+
)
601+
await self._chip_device_controller.send_command(
602+
node_id,
603+
0,
604+
Clusters.OperationalCredentials.Commands.UpdateFabricLabel(
605+
self._default_fabric_label
606+
),
607+
)
608+
except ChipStackError as err:
609+
LOGGER.warning(
610+
"Failed to set fabric label for node %s: %s", node_id, err
611+
)
612+
583613
is_new_node = node_id not in self._nodes
584614
existing_info = self._nodes.get(node_id)
585615
node = MatterNodeData(

0 commit comments

Comments
 (0)
Please sign in to comment.