Skip to content

Commit a90ae1a

Browse files
committed
[Python] Make Commissioning APIs more pythonic and consistent
This commit makes the commissioning APIs more pythonic and consistent by not returning PyChipError but simply raising ChipStackError exceptions on errors instead. The return value instead returns the effectively assigned node ID as defined by the NOC. If the SDK ends up generating that NOC, it will use the thing passed to PairDevice, so those will match with what is provided when calling the commissioning API.
1 parent ab2907b commit a90ae1a

File tree

1 file changed

+54
-41
lines changed

1 file changed

+54
-41
lines changed

src/controller/python/chip/ChipDeviceCtrl.py

+54-41
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,14 @@ 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) -> int:
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
238+
239+
Returns:
240+
- Effective Node ID of the device (as defined by the assigned NOC)
238241
'''
239242
return self._devCtrl.CommissionOnNetwork(
240243
nodeId, setupPinCode, filterType=discovery.FilterType.INSTANCE_NAME, filter=self.instanceName)
@@ -360,7 +363,10 @@ def HandleCommissioningComplete(nodeId: int, err: PyChipError):
360363
logging.exception("HandleCommissioningComplete called unexpectedly")
361364
return
362365

363-
self._commissioning_complete_future.set_result(err)
366+
if err.is_success:
367+
self._commissioning_complete_future.set_result(nodeId)
368+
else:
369+
self._commissioning_complete_future.set_exception(err.to_exception())
364370

365371
def HandleFabricCheck(nodeId):
366372
self.fabricCheckNodeId = nodeId
@@ -408,14 +414,17 @@ def HandlePASEEstablishmentComplete(err: PyChipError):
408414
# During Commissioning, HandlePASEEstablishmentComplete will also be called.
409415
# Only complete the future if PASE session establishment failed.
410416
if not err.is_success:
411-
self._commissioning_complete_future.set_result(err)
417+
self._commissioning_complete_future.set_exception(err.to_exception())
412418
return
413419

414420
if self._pase_establishment_complete_future is None:
415421
logging.exception("HandlePASEEstablishmentComplete called unexpectedly")
416422
return
417423

418-
self._pase_establishment_complete_future.set_result(err)
424+
if err.is_success:
425+
self._pase_establishment_complete_future.set_result(None)
426+
else:
427+
self._pase_establishment_complete_future.set_exception(err.to_exception())
419428

420429
self.pairingDelegate = pairingDelegate
421430
self.devCtrl = devCtrl
@@ -533,7 +542,12 @@ def IsConnected(self):
533542
self.devCtrl)
534543
)
535544

536-
def ConnectBLE(self, discriminator: int, setupPinCode: int, nodeid: int, isShortDiscriminator: bool = False) -> PyChipError:
545+
def ConnectBLE(self, discriminator: int, setupPinCode: int, nodeid: int, isShortDiscriminator: bool = False) -> int:
546+
"""Connect to a BLE device using the given discriminator and setup pin code.
547+
548+
Returns:
549+
- Effective Node ID of the device (as defined by the assigned NOC)
550+
"""
537551
self.CheckIsActive()
538552

539553
self._commissioning_complete_future = concurrent.futures.Future()
@@ -545,11 +559,7 @@ def ConnectBLE(self, discriminator: int, setupPinCode: int, nodeid: int, isShort
545559
self.devCtrl, discriminator, isShortDiscriminator, setupPinCode, nodeid)
546560
).raise_on_error()
547561

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
562+
return self._commissioning_complete_future.result()
553563
finally:
554564
self._commissioning_complete_future = None
555565

@@ -595,7 +605,7 @@ def CloseSession(self, nodeid):
595605
self.devCtrl, nodeid)
596606
).raise_on_error()
597607

598-
def EstablishPASESessionBLE(self, setupPinCode: int, discriminator: int, nodeid: int):
608+
def EstablishPASESessionBLE(self, setupPinCode: int, discriminator: int, nodeid: int) -> None:
599609
self.CheckIsActive()
600610

601611
self._pase_establishment_complete_future = concurrent.futures.Future()
@@ -606,16 +616,11 @@ def EstablishPASESessionBLE(self, setupPinCode: int, discriminator: int, nodeid:
606616
self.devCtrl, setupPinCode, discriminator, nodeid)
607617
).raise_on_error()
608618

609-
# TODO: This is a bit funky, but what the API returned with the previous
610-
# implementation. We should revisit this.
611-
err = self._pase_establishment_complete_future.result()
612-
if not err.is_success:
613-
return err.to_exception()
614-
return None
619+
self._pase_establishment_complete_future.result()
615620
finally:
616621
self._pase_establishment_complete_future = None
617622

618-
def EstablishPASESessionIP(self, ipaddr: str, setupPinCode: int, nodeid: int, port: int = 0):
623+
def EstablishPASESessionIP(self, ipaddr: str, setupPinCode: int, nodeid: int, port: int = 0) -> None:
619624
self.CheckIsActive()
620625

621626
self._pase_establishment_complete_future = concurrent.futures.Future()
@@ -626,16 +631,11 @@ def EstablishPASESessionIP(self, ipaddr: str, setupPinCode: int, nodeid: int, po
626631
self.devCtrl, ipaddr.encode("utf-8"), setupPinCode, nodeid, port)
627632
).raise_on_error()
628633

629-
# TODO: This is a bit funky, but what the API returned with the previous
630-
# implementation. We should revisit this.
631-
err = self._pase_establishment_complete_future.result()
632-
if not err.is_success:
633-
return err.to_exception()
634-
return None
634+
self._pase_establishment_complete_future.result()
635635
finally:
636636
self._pase_establishment_complete_future = None
637637

638-
def EstablishPASESession(self, setUpCode: str, nodeid: int):
638+
def EstablishPASESession(self, setUpCode: str, nodeid: int) -> None:
639639
self.CheckIsActive()
640640

641641
self._pase_establishment_complete_future = concurrent.futures.Future()
@@ -646,12 +646,7 @@ def EstablishPASESession(self, setUpCode: str, nodeid: int):
646646
self.devCtrl, setUpCode.encode("utf-8"), nodeid)
647647
).raise_on_error()
648648

649-
# TODO: This is a bit funky, but what the API returned with the previous
650-
# implementation. We should revisit this.
651-
err = self._pase_establishment_complete_future.result()
652-
if not err.is_success:
653-
return err.to_exception()
654-
return None
649+
self._pase_establishment_complete_future.result()
655650
finally:
656651
self._pase_establishment_complete_future = None
657652

@@ -1853,17 +1848,19 @@ def caIndex(self) -> int:
18531848
def fabricAdmin(self) -> FabricAdmin:
18541849
return self._fabricAdmin
18551850

1856-
def Commission(self, nodeid) -> PyChipError:
1851+
def Commission(self, nodeid) -> int:
18571852
'''
18581853
Start the auto-commissioning process on a node after establishing a PASE connection.
18591854
This function is intended to be used in conjunction with `EstablishPASESessionBLE` or
18601855
`EstablishPASESessionIP`. It can be called either before or after the DevicePairingDelegate
18611856
receives the OnPairingComplete call. Commissioners that want to perform simple
1862-
auto-commissioning should use the supplied "PairDevice" functions above, which will
1857+
auto-commissioning should use the supplied "CommissionWithCode" function, which will
18631858
establish the PASE connection and commission automatically.
18641859
1865-
Return:
1866-
bool: True if successful, False otherwise.
1860+
Raises a ChipStackError on failure.
1861+
1862+
Returns:
1863+
- Effective Node ID of the device (as defined by the assigned NOC)
18671864
'''
18681865
self.CheckIsActive()
18691866

@@ -1879,13 +1876,13 @@ def Commission(self, nodeid) -> PyChipError:
18791876
finally:
18801877
self._commissioning_complete_future = None
18811878

1882-
def CommissionThread(self, discriminator, setupPinCode, nodeId, threadOperationalDataset: bytes, isShortDiscriminator: bool = False) -> PyChipError:
1879+
def CommissionThread(self, discriminator, setupPinCode, nodeId, threadOperationalDataset: bytes, isShortDiscriminator: bool = False) -> int:
18831880
''' Commissions a Thread device over BLE
18841881
'''
18851882
self.SetThreadOperationalDataset(threadOperationalDataset)
18861883
return self.ConnectBLE(discriminator, setupPinCode, nodeId, isShortDiscriminator)
18871884

1888-
def CommissionWiFi(self, discriminator, setupPinCode, nodeId, ssid: str, credentials: str, isShortDiscriminator: bool = False) -> PyChipError:
1885+
def CommissionWiFi(self, discriminator, setupPinCode, nodeId, ssid: str, credentials: str, isShortDiscriminator: bool = False) -> int:
18891886
''' Commissions a Wi-Fi device over BLE.
18901887
'''
18911888
self.SetWiFiCredentials(ssid, credentials)
@@ -1988,7 +1985,7 @@ def GetFabricCheckResult(self) -> int:
19881985
return self.fabricCheckNodeId
19891986

19901987
def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
1991-
filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None, discoveryTimeoutMsec: int = 30000) -> PyChipError:
1988+
filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None, discoveryTimeoutMsec: int = 30000) -> int:
19921989
'''
19931990
Does the routine for OnNetworkCommissioning, with a filter for mDNS discovery.
19941991
Supported filters are:
@@ -2004,6 +2001,11 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
20042001
DiscoveryFilterType.COMPRESSED_FABRIC_ID
20052002
20062003
The filter can be an integer, a string or None depending on the actual type of selected filter.
2004+
2005+
Raises a ChipStackError on failure.
2006+
2007+
Returns:
2008+
- Effective Node ID of the device (as defined by the assigned NOC)
20072009
'''
20082010
self.CheckIsActive()
20092011

@@ -2023,9 +2025,14 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
20232025
finally:
20242026
self._commissioning_complete_future = None
20252027

2026-
def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: DiscoveryType = DiscoveryType.DISCOVERY_ALL) -> PyChipError:
2028+
def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: DiscoveryType = DiscoveryType.DISCOVERY_ALL) -> int:
20272029
''' Commission with the given nodeid from the setupPayload.
20282030
setupPayload may be a QR or manual code.
2031+
2032+
Raises a ChipStackError on failure.
2033+
2034+
Returns:
2035+
- Effective Node ID of the device (as defined by the assigned NOC)
20292036
'''
20302037
self.CheckIsActive()
20312038

@@ -2042,8 +2049,14 @@ def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: Disc
20422049
finally:
20432050
self._commissioning_complete_future = None
20442051

2045-
def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int) -> PyChipError:
2046-
""" DEPRECATED, DO NOT USE! Use `CommissionOnNetwork` or `CommissionWithCode` """
2052+
def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int) -> int:
2053+
""" DEPRECATED, DO NOT USE! Use `CommissionOnNetwork` or `CommissionWithCode`
2054+
2055+
Raises a ChipStackError on failure.
2056+
2057+
Returns:
2058+
- Effective Node ID of the device (as defined by the assigned NOC)
2059+
"""
20472060
self.CheckIsActive()
20482061

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

0 commit comments

Comments
 (0)