Skip to content

Commit d4316c9

Browse files
authored
Use UnpairDevice() Matter SDK API on device removal (#761)
1 parent 8b74327 commit d4316c9

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

matter_server/server/device_controller.py

+2-33
Original file line numberDiff line numberDiff line change
@@ -750,41 +750,10 @@ async def remove_node(self, node_id: int) -> None:
750750
if node is None or node_id >= TEST_NODE_START:
751751
return
752752

753-
attribute_path = create_attribute_path_from_attribute(
754-
0,
755-
Clusters.OperationalCredentials.Attributes.CurrentFabricIndex,
756-
)
757-
fabric_index = node.attributes.get(attribute_path)
758-
if fabric_index is None:
759-
return
760-
result: Clusters.OperationalCredentials.Commands.NOCResponse | None = None
761753
try:
762-
result = await self._chip_device_controller.send_command(
763-
node_id=node_id,
764-
endpoint_id=0,
765-
command=Clusters.OperationalCredentials.Commands.RemoveFabric(
766-
fabricIndex=fabric_index,
767-
),
768-
)
754+
await self._chip_device_controller.unpair_device(node_id)
769755
except ChipStackError as err:
770-
LOGGER.warning(
771-
"Removing current fabric from device failed: %s",
772-
str(err) or err.__class__.__name__,
773-
# only log stacktrace if we have verbose logging enabled
774-
exc_info=err if LOGGER.isEnabledFor(VERBOSE_LOG_LEVEL) else None,
775-
)
776-
return
777-
if (
778-
result is None
779-
or result.statusCode
780-
== Clusters.OperationalCredentials.Enums.NodeOperationalCertStatusEnum.kOk
781-
):
782-
LOGGER.info("Successfully removed Home Assistant fabric from device.")
783-
else:
784-
LOGGER.warning(
785-
"Removing current fabric from device failed with status code %d.",
786-
result.statusCode,
787-
)
756+
LOGGER.warning("Removing current fabric from device failed: %s", err)
788757

789758
@api_command(APICommand.PING_NODE)
790759
async def ping_node(self, node_id: int, attempts: int = 1) -> NodePingResult:

matter_server/server/sdk.py

+10
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ async def set_thread_operational_dataset(self, dataset: str) -> None:
179179
threadOperationalDataset=bytes.fromhex(dataset),
180180
)
181181

182+
async def unpair_device(self, node_id: int) -> PyChipError:
183+
"""Remove our fabric from given node.
184+
185+
Tries to look up the device attached to our controller with the given
186+
remote node id and ask it to remove Fabric.
187+
"""
188+
return await self._call_sdk_non_reentrant(
189+
self._chip_controller.UnpairDevice, nodeid=node_id
190+
)
191+
182192
async def open_commissioning_window(
183193
self,
184194
node_id: int,

0 commit comments

Comments
 (0)