Skip to content

Commit

Permalink
[Python] Make Commissioning APIs more pythonic an consistent
Browse files Browse the repository at this point in the history
This commit makes the commissioning APIs more pythonic and consistent
by not returning PyChipError but simply raising ChipStackError
exceptions instead.
  • Loading branch information
agners committed Jun 13, 2024
1 parent f0bf341 commit 1463fc7
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ def HandleCommissioningComplete(nodeId: int, err: PyChipError):
logging.exception("HandleCommissioningComplete called unexpectedly")
return

self._commissioning_complete_future.set_result(err)
if err.is_success:
self._commissioning_complete_future.set_result(None)
else:
self._commissioning_complete_future.set_exception(err.to_exception())

def HandleFabricCheck(nodeId):
self.fabricCheckNodeId = nodeId
Expand Down Expand Up @@ -533,7 +536,7 @@ def IsConnected(self):
self.devCtrl)
)

def ConnectBLE(self, discriminator, setupPinCode, nodeid) -> PyChipError:
def ConnectBLE(self, discriminator, setupPinCode, nodeid) -> None:
self.CheckIsActive()

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

# TODO: Change return None. Only returning on success is not useful.
# but that is what the previous implementation did.
res = self._commissioning_complete_future.result()
res.raise_on_error()
return res
return self._commissioning_complete_future.result()
finally:
self._commissioning_complete_future = None

Expand Down Expand Up @@ -1856,17 +1855,16 @@ def caIndex(self) -> int:
def fabricAdmin(self) -> FabricAdmin:
return self._fabricAdmin

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

Expand Down Expand Up @@ -1991,7 +1989,7 @@ def GetFabricCheckResult(self) -> int:
return self.fabricCheckNodeId

def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None, discoveryTimeoutMsec: int = 30000) -> PyChipError:
filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None, discoveryTimeoutMsec: int = 30000) -> None:
'''
Does the routine for OnNetworkCommissioning, with a filter for mDNS discovery.
Supported filters are:
Expand All @@ -2007,6 +2005,8 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int,
DiscoveryFilterType.COMPRESSED_FABRIC_ID
The filter can be an integer, a string or None depending on the actual type of selected filter.
Raises a ChipStackError on failure.
'''
self.CheckIsActive()

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

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

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

def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int) -> PyChipError:
""" DEPRECATED, DO NOT USE! Use `CommissionOnNetwork` or `CommissionWithCode` """
def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int) -> None:
""" DEPRECATED, DO NOT USE! Use `CommissionOnNetwork` or `CommissionWithCode`
Raises a ChipStackError on failure.
"""
self.CheckIsActive()

self._commissioning_complete_future = concurrent.futures.Future()
Expand Down

0 comments on commit 1463fc7

Please sign in to comment.