Skip to content

Commit 32bb422

Browse files
committed
[Python] Make Commissioning APIs more pythonic an consistent
This commit makes the commissioning APIs more pythonic and consistent by not returning PyChipError but simply raising ChipStackError exceptions instead.
1 parent f0bf341 commit 32bb422

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/controller/python/chip/ChipDeviceCtrl.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ class CommissionableNode(discovery.CommissionableNode):
230230
def SetDeviceController(self, devCtrl: 'ChipDeviceController'):
231231
self._devCtrl = devCtrl
232232

233-
def Commission(self, nodeId: int, setupPinCode: int) -> PyChipError:
233+
def Commission(self, nodeId: int, setupPinCode: int) -> None:
234234
''' Commission the device using the device controller discovered this device.
235235
236236
nodeId: The nodeId commissioned to the device
237237
setupPinCode: The setup pin code of the device
238238
'''
239-
return self._devCtrl.CommissionOnNetwork(
239+
self._devCtrl.CommissionOnNetwork(
240240
nodeId, setupPinCode, filterType=discovery.FilterType.INSTANCE_NAME, filter=self.instanceName)
241241

242242
def __rich_repr__(self):
@@ -360,7 +360,10 @@ def HandleCommissioningComplete(nodeId: int, err: PyChipError):
360360
logging.exception("HandleCommissioningComplete called unexpectedly")
361361
return
362362

363-
self._commissioning_complete_future.set_result(err)
363+
if err.is_success:
364+
self._commissioning_complete_future.set_result(None)
365+
else:
366+
self._commissioning_complete_future.set_exception(err.to_exception())
364367

365368
def HandleFabricCheck(nodeId):
366369
self.fabricCheckNodeId = nodeId
@@ -533,7 +536,7 @@ def IsConnected(self):
533536
self.devCtrl)
534537
)
535538

536-
def ConnectBLE(self, discriminator, setupPinCode, nodeid) -> PyChipError:
539+
def ConnectBLE(self, discriminator, setupPinCode, nodeid) -> None:
537540
self.CheckIsActive()
538541

539542
self._commissioning_complete_future = concurrent.futures.Future()
@@ -545,11 +548,7 @@ def ConnectBLE(self, discriminator, setupPinCode, nodeid) -> PyChipError:
545548
self.devCtrl, discriminator, setupPinCode, nodeid)
546549
).raise_on_error()
547550

548-
# TODO: Change return None. Only returning on success is not useful.
549-
# but that is what the previous implementation did.
550-
res = self._commissioning_complete_future.result()
551-
res.raise_on_error()
552-
return res
551+
return self._commissioning_complete_future.result()
553552
finally:
554553
self._commissioning_complete_future = None
555554

@@ -1856,17 +1855,16 @@ def caIndex(self) -> int:
18561855
def fabricAdmin(self) -> FabricAdmin:
18571856
return self._fabricAdmin
18581857

1859-
def Commission(self, nodeid) -> PyChipError:
1858+
def Commission(self, nodeid) -> None:
18601859
'''
18611860
Start the auto-commissioning process on a node after establishing a PASE connection.
18621861
This function is intended to be used in conjunction with `EstablishPASESessionBLE` or
18631862
`EstablishPASESessionIP`. It can be called either before or after the DevicePairingDelegate
18641863
receives the OnPairingComplete call. Commissioners that want to perform simple
1865-
auto-commissioning should use the supplied "PairDevice" functions above, which will
1864+
auto-commissioning should use the supplied "CommissionWithCode" function, which will
18661865
establish the PASE connection and commission automatically.
18671866
1868-
Return:
1869-
bool: True if successful, False otherwise.
1867+
Raises a ChipStackError on failure.
18701868
'''
18711869
self.CheckIsActive()
18721870

@@ -1991,7 +1989,7 @@ def GetFabricCheckResult(self) -> int:
19911989
return self.fabricCheckNodeId
19921990

19931991
def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
1994-
filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None, discoveryTimeoutMsec: int = 30000) -> PyChipError:
1992+
filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None, discoveryTimeoutMsec: int = 30000) -> None:
19951993
'''
19961994
Does the routine for OnNetworkCommissioning, with a filter for mDNS discovery.
19971995
Supported filters are:
@@ -2007,6 +2005,8 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
20072005
DiscoveryFilterType.COMPRESSED_FABRIC_ID
20082006
20092007
The filter can be an integer, a string or None depending on the actual type of selected filter.
2008+
2009+
Raises a ChipStackError on failure.
20102010
'''
20112011
self.CheckIsActive()
20122012

@@ -2026,9 +2026,11 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
20262026
finally:
20272027
self._commissioning_complete_future = None
20282028

2029-
def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: DiscoveryType = DiscoveryType.DISCOVERY_ALL) -> PyChipError:
2029+
def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: DiscoveryType = DiscoveryType.DISCOVERY_ALL) -> None:
20302030
''' Commission with the given nodeid from the setupPayload.
20312031
setupPayload may be a QR or manual code.
2032+
2033+
Raises a ChipStackError on failure.
20322034
'''
20332035
self.CheckIsActive()
20342036

@@ -2047,8 +2049,11 @@ def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: Disc
20472049
finally:
20482050
self._commissioning_complete_future = None
20492051

2050-
def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int) -> PyChipError:
2051-
""" DEPRECATED, DO NOT USE! Use `CommissionOnNetwork` or `CommissionWithCode` """
2052+
def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int) -> None:
2053+
""" DEPRECATED, DO NOT USE! Use `CommissionOnNetwork` or `CommissionWithCode`
2054+
2055+
Raises a ChipStackError on failure.
2056+
"""
20522057
self.CheckIsActive()
20532058

20542059
self._commissioning_complete_future = concurrent.futures.Future()

0 commit comments

Comments
 (0)