Skip to content

Commit 6b30339

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 c955d03 commit 6b30339

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

src/controller/python/chip/ChipDeviceCtrl.py

+42-20
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(nodeId)
365+
else:
366+
self._commissioning_complete_future.set_exception(err.to_exception())
364367

365368
def HandleFabricCheck(nodeId):
366369
self.fabricCheckNodeId = nodeId
@@ -408,7 +411,7 @@ def HandlePASEEstablishmentComplete(err: PyChipError):
408411
# During Commissioning, HandlePASEEstablishmentComplete will also be called.
409412
# Only complete the future if PASE session establishment failed.
410413
if not err.is_success:
411-
self._commissioning_complete_future.set_result(err)
414+
self._commissioning_complete_future.set_exception(err.to_exception())
412415
return
413416

414417
if self._pase_establishment_complete_future is None:
@@ -533,7 +536,12 @@ def IsConnected(self):
533536
self.devCtrl)
534537
)
535538

536-
def ConnectBLE(self, discriminator: int, setupPinCode: int, nodeid: int, isShortDiscriminator: bool = False) -> PyChipError:
539+
def ConnectBLE(self, discriminator: int, setupPinCode: int, nodeid: int, isShortDiscriminator: bool = False) -> int:
540+
"""Connect to a BLE device using the given discriminator and setup pin code.
541+
542+
Returns:
543+
- Effective Node ID of the device (as defined by the assigned NOC)
544+
"""
537545
self.CheckIsActive()
538546

539547
self._commissioning_complete_future = concurrent.futures.Future()
@@ -545,11 +553,7 @@ def ConnectBLE(self, discriminator: int, setupPinCode: int, nodeid: int, isShort
545553
self.devCtrl, discriminator, isShortDiscriminator, setupPinCode, nodeid)
546554
).raise_on_error()
547555

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
556+
return self._commissioning_complete_future.result()
553557
finally:
554558
self._commissioning_complete_future = None
555559

@@ -1853,17 +1857,19 @@ def caIndex(self) -> int:
18531857
def fabricAdmin(self) -> FabricAdmin:
18541858
return self._fabricAdmin
18551859

1856-
def Commission(self, nodeid) -> PyChipError:
1860+
def Commission(self, nodeid) -> None:
18571861
'''
18581862
Start the auto-commissioning process on a node after establishing a PASE connection.
18591863
This function is intended to be used in conjunction with `EstablishPASESessionBLE` or
18601864
`EstablishPASESessionIP`. It can be called either before or after the DevicePairingDelegate
18611865
receives the OnPairingComplete call. Commissioners that want to perform simple
1862-
auto-commissioning should use the supplied "PairDevice" functions above, which will
1866+
auto-commissioning should use the supplied "CommissionWithCode" function, which will
18631867
establish the PASE connection and commission automatically.
18641868
1865-
Return:
1866-
bool: True if successful, False otherwise.
1869+
Raises a ChipStackError on failure.
1870+
1871+
Returns:
1872+
- Effective Node ID of the device (as defined by the assigned NOC)
18671873
'''
18681874
self.CheckIsActive()
18691875

@@ -1879,13 +1885,13 @@ def Commission(self, nodeid) -> PyChipError:
18791885
finally:
18801886
self._commissioning_complete_future = None
18811887

1882-
def CommissionThread(self, discriminator, setupPinCode, nodeId, threadOperationalDataset: bytes, isShortDiscriminator: bool = False) -> PyChipError:
1888+
def CommissionThread(self, discriminator, setupPinCode, nodeId, threadOperationalDataset: bytes, isShortDiscriminator: bool = False) -> int:
18831889
''' Commissions a Thread device over BLE
18841890
'''
18851891
self.SetThreadOperationalDataset(threadOperationalDataset)
18861892
return self.ConnectBLE(discriminator, setupPinCode, nodeId, isShortDiscriminator)
18871893

1888-
def CommissionWiFi(self, discriminator, setupPinCode, nodeId, ssid: str, credentials: str, isShortDiscriminator: bool = False) -> PyChipError:
1894+
def CommissionWiFi(self, discriminator, setupPinCode, nodeId, ssid: str, credentials: str, isShortDiscriminator: bool = False) -> int:
18891895
''' Commissions a Wi-Fi device over BLE.
18901896
'''
18911897
self.SetWiFiCredentials(ssid, credentials)
@@ -1988,7 +1994,7 @@ def GetFabricCheckResult(self) -> int:
19881994
return self.fabricCheckNodeId
19891995

19901996
def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
1991-
filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None, discoveryTimeoutMsec: int = 30000) -> PyChipError:
1997+
filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None, discoveryTimeoutMsec: int = 30000) -> int:
19921998
'''
19931999
Does the routine for OnNetworkCommissioning, with a filter for mDNS discovery.
19942000
Supported filters are:
@@ -2004,6 +2010,11 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
20042010
DiscoveryFilterType.COMPRESSED_FABRIC_ID
20052011
20062012
The filter can be an integer, a string or None depending on the actual type of selected filter.
2013+
2014+
Raises a ChipStackError on failure.
2015+
2016+
Returns:
2017+
- Effective Node ID of the device (as defined by the assigned NOC)
20072018
'''
20082019
self.CheckIsActive()
20092020

@@ -2023,9 +2034,14 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
20232034
finally:
20242035
self._commissioning_complete_future = None
20252036

2026-
def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: DiscoveryType = DiscoveryType.DISCOVERY_ALL) -> PyChipError:
2037+
def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: DiscoveryType = DiscoveryType.DISCOVERY_ALL) -> int:
20272038
''' Commission with the given nodeid from the setupPayload.
20282039
setupPayload may be a QR or manual code.
2040+
2041+
Raises a ChipStackError on failure.
2042+
2043+
Returns:
2044+
- Effective Node ID of the device (as defined by the assigned NOC)
20292045
'''
20302046
self.CheckIsActive()
20312047

@@ -2042,8 +2058,14 @@ def CommissionWithCode(self, setupPayload: str, nodeid: int, discoveryType: Disc
20422058
finally:
20432059
self._commissioning_complete_future = None
20442060

2045-
def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int) -> PyChipError:
2046-
""" DEPRECATED, DO NOT USE! Use `CommissionOnNetwork` or `CommissionWithCode` """
2061+
def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int) -> int:
2062+
""" DEPRECATED, DO NOT USE! Use `CommissionOnNetwork` or `CommissionWithCode`
2063+
2064+
Raises a ChipStackError on failure.
2065+
2066+
Returns:
2067+
- Effective Node ID of the device (as defined by the assigned NOC)
2068+
"""
20472069
self.CheckIsActive()
20482070

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

0 commit comments

Comments
 (0)