Skip to content

Commit b7e1236

Browse files
Harshith-GRLrestyled-commitscjandhyala
authored
Return python object intead of bool value as Response for commissioning with device (#38787)
* Return python object intead of bool value as Response for commissioning with device * added docstrings PairingStatus class * Restyled by autopep8 * set the PairingStatus to False when exception occurs * Updated PairingStatus class to evaluate to true without success parameter to make it easier to use * bool expression validation is made directly instead of storing in variable and if success do not return empty string * added new line to re-start CI job --------- Co-authored-by: Restyled.io <[email protected]> Co-authored-by: cjandhyala <[email protected]>
1 parent 1b86467 commit b7e1236

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/python_testing/matter_testing_infrastructure/chip/testing/commissioning.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,27 @@ class CustomCommissioningParameters:
9999
randomDiscriminator: int
100100

101101

102+
class PairingStatus:
103+
"""
104+
This class is used to store the pairing status of a commissioning process with a device.
105+
if the commissioning Process is unsuccessful then we pass the exception to the class which sets the PairingStatus
106+
to False. If we do not pass any exception when creating the Pairing status then the commissioning status
107+
is set to true to indicate that the commissioning process has succeeded.
108+
"""
109+
110+
def __init__(self, exception: Optional[Exception] = None):
111+
self.exception = exception
112+
113+
def __bool__(self):
114+
return self.exception is None
115+
116+
def __str__(self):
117+
return str(self.exception) if self.exception else "Pairing Successful"
118+
119+
102120
async def commission_device(
103121
dev_ctrl: ChipDeviceCtrl.ChipDeviceController, node_id: int, info: SetupPayloadInfo, commissioning_info: CommissioningInfo
104-
) -> bool:
122+
) -> PairingStatus:
105123
"""
106124
Starts the commissioning process of a chip device.
107125
@@ -115,8 +133,10 @@ async def commission_device(
115133
commissioning_info: Specifies the type of commissioning method to use.
116134
117135
Returns:
118-
True if the commissioning process completes successfully. False otherwise,
119-
except in case of an error which logs the exception details.
136+
PairingStatus object which can evaluated in conditional statements
137+
if the commissioning process completes successfully PairingStatus is evaluated to True else False along
138+
with storing the reason for pairing failure by storing the exception raised during commissioning process.
139+
120140
"""
121141

122142
if commissioning_info.tc_version_to_simulate is not None and commissioning_info.tc_user_response_to_simulate is not None:
@@ -130,10 +150,10 @@ async def commission_device(
130150
await dev_ctrl.CommissionOnNetwork(
131151
nodeId=node_id, setupPinCode=info.passcode, filterType=info.filter_type, filter=info.filter_value
132152
)
133-
return True
153+
return PairingStatus()
134154
except ChipStackError as e:
135155
logging.error("Commissioning failed: %s" % e)
136-
return False
156+
return PairingStatus(exception=e)
137157
elif commissioning_info.commissioning_method == "ble-wifi":
138158
try:
139159
await dev_ctrl.CommissionWiFi(
@@ -144,10 +164,10 @@ async def commission_device(
144164
commissioning_info.wifi_passphrase,
145165
isShortDiscriminator=(info.filter_type == DiscoveryFilterType.SHORT_DISCRIMINATOR),
146166
)
147-
return True
167+
return PairingStatus()
148168
except ChipStackError as e:
149169
logging.error("Commissioning failed: %s" % e)
150-
return False
170+
return PairingStatus(exception=e)
151171
elif commissioning_info.commissioning_method == "ble-thread":
152172
try:
153173
await dev_ctrl.CommissionThread(
@@ -157,10 +177,10 @@ async def commission_device(
157177
commissioning_info.thread_operational_dataset,
158178
isShortDiscriminator=(info.filter_type == DiscoveryFilterType.SHORT_DISCRIMINATOR),
159179
)
160-
return True
180+
return PairingStatus()
161181
except ChipStackError as e:
162182
logging.error("Commissioning failed: %s" % e)
163-
return False
183+
return PairingStatus(exception=e)
164184
else:
165185
raise ValueError("Invalid commissioning method %s!" % commissioning_info.commissioning_method)
166186

0 commit comments

Comments
 (0)