Skip to content

Commit

Permalink
[Python] Make Commissioning APIs more pythonic and 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 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.
  • Loading branch information
agners committed Jun 17, 2024
1 parent c955d03 commit 456785a
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ class CommissionableNode(discovery.CommissionableNode):
def SetDeviceController(self, devCtrl: 'ChipDeviceController'):
self._devCtrl = devCtrl

def Commission(self, nodeId: int, setupPinCode: int) -> PyChipError:
def Commission(self, nodeId: int, setupPinCode: int) -> None:
''' Commission the device using the device controller discovered this device.
nodeId: The nodeId commissioned to the device
setupPinCode: The setup pin code of the device
'''
return self._devCtrl.CommissionOnNetwork(
self._devCtrl.CommissionOnNetwork(
nodeId, setupPinCode, filterType=discovery.FilterType.INSTANCE_NAME, filter=self.instanceName)

def __rich_repr__(self):
Expand Down 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(nodeId)
else:
self._commissioning_complete_future.set_exception(err.to_exception())

def HandleFabricCheck(nodeId):
self.fabricCheckNodeId = nodeId
Expand Down Expand Up @@ -408,7 +411,7 @@ def HandlePASEEstablishmentComplete(err: PyChipError):
# During Commissioning, HandlePASEEstablishmentComplete will also be called.
# Only complete the future if PASE session establishment failed.
if not err.is_success:
self._commissioning_complete_future.set_result(err)
self._commissioning_complete_future.set_exception(err.to_exception())
return

if self._pase_establishment_complete_future is None:
Expand Down Expand Up @@ -533,7 +536,12 @@ def IsConnected(self):
self.devCtrl)
)

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

self._commissioning_complete_future = concurrent.futures.Future()
Expand All @@ -545,11 +553,7 @@ def ConnectBLE(self, discriminator: int, setupPinCode: int, nodeid: int, isShort
self.devCtrl, discriminator, isShortDiscriminator, 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 @@ -1853,17 +1857,19 @@ 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.
Returns:
- Effective Node ID of the device (as defined by the assigned NOC)
'''
self.CheckIsActive()

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

def CommissionThread(self, discriminator, setupPinCode, nodeId, threadOperationalDataset: bytes, isShortDiscriminator: bool = False) -> PyChipError:
def CommissionThread(self, discriminator, setupPinCode, nodeId, threadOperationalDataset: bytes, isShortDiscriminator: bool = False) -> int:
''' Commissions a Thread device over BLE
'''
self.SetThreadOperationalDataset(threadOperationalDataset)
return self.ConnectBLE(discriminator, setupPinCode, nodeId, isShortDiscriminator)

def CommissionWiFi(self, discriminator, setupPinCode, nodeId, ssid: str, credentials: str, isShortDiscriminator: bool = False) -> PyChipError:
def CommissionWiFi(self, discriminator, setupPinCode, nodeId, ssid: str, credentials: str, isShortDiscriminator: bool = False) -> int:
''' Commissions a Wi-Fi device over BLE.
'''
self.SetWiFiCredentials(ssid, credentials)
Expand Down Expand Up @@ -1988,7 +1994,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) -> int:
'''
Does the routine for OnNetworkCommissioning, with a filter for mDNS discovery.
Supported filters are:
Expand All @@ -2004,6 +2010,11 @@ 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.
Returns:
- Effective Node ID of the device (as defined by the assigned NOC)
'''
self.CheckIsActive()

Expand All @@ -2023,9 +2034,14 @@ 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) -> int:
''' Commission with the given nodeid from the setupPayload.
setupPayload may be a QR or manual code.
Raises a ChipStackError on failure.
Returns:
- Effective Node ID of the device (as defined by the assigned NOC)
'''
self.CheckIsActive()

Expand All @@ -2042,8 +2058,14 @@ 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) -> int:
""" DEPRECATED, DO NOT USE! Use `CommissionOnNetwork` or `CommissionWithCode`
Raises a ChipStackError on failure.
Returns:
- Effective Node ID of the device (as defined by the assigned NOC)
"""
self.CheckIsActive()

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

0 comments on commit 456785a

Please sign in to comment.