@@ -321,15 +321,6 @@ async def commission_with_code(
321
321
LOGGER .info ("Commissioned Node ID: %s vs %s" , commissioned_node_id , node_id )
322
322
if commissioned_node_id != node_id :
323
323
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
- )
333
324
except ChipStackError as err :
334
325
raise NodeCommissionFailed (
335
326
f"Commission with code failed for node { node_id } ."
@@ -348,6 +339,12 @@ async def commission_with_code(
348
339
await self ._interview_node (node_id )
349
340
except (NodeNotResolving , NodeInterviewFailed ) as err :
350
341
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
+ )
351
348
raise err
352
349
retries -= 1
353
350
LOGGER .warning ("Unable to interview Node %s: %s" , node_id , err )
@@ -580,6 +577,39 @@ async def _interview_node(self, node_id: int) -> None:
580
577
except ChipStackError as err :
581
578
raise NodeInterviewFailed (f"Failed to interview node { node_id } " ) from err
582
579
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
+
583
613
is_new_node = node_id not in self ._nodes
584
614
existing_info = self ._nodes .get (node_id )
585
615
node = MatterNodeData (
0 commit comments