From 9c44d1cf7f1fcafecb17ad8658ebb1688e3f4035 Mon Sep 17 00:00:00 2001
From: Stefan Agner <stefan@agner.ch>
Date: Tue, 18 Jun 2024 21:34:58 +0200
Subject: [PATCH 1/2] [Python] Reset chip error in test commissioner

Make sure to reset the chip error in test commissioner on reset. This
avoid spurious errors. The Python side reads the error as soon as
mTestCommissionerUsed is set, which happens unconditionally. Hence
clearing the error is necessary.
---
 src/controller/python/OpCredsBinding.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/controller/python/OpCredsBinding.cpp b/src/controller/python/OpCredsBinding.cpp
index 427ee9be46ba3c..f5815922cc14eb 100644
--- a/src/controller/python/OpCredsBinding.cpp
+++ b/src/controller/python/OpCredsBinding.cpp
@@ -259,6 +259,7 @@ class TestCommissioner : public chip::Controller::AutoCommissioner
         mPrematureCompleteAfter = chip::Controller::CommissioningStage::kError;
         mReadCommissioningInfo  = chip::Controller::ReadCommissioningInfo();
         mNeedsDST               = false;
+        mCompletionError        = CHIP_NO_ERROR;
     }
     bool GetTestCommissionerUsed() { return mTestCommissionerUsed; }
     void OnCommissioningSuccess(chip::PeerId peerId) { mReceivedCommissioningSuccess = true; }

From 2ff04f7a3c49e0d884aa31192108d761bef47bbc Mon Sep 17 00:00:00 2001
From: Stefan Agner <stefan@agner.ch>
Date: Tue, 18 Jun 2024 23:27:13 +0200
Subject: [PATCH 2/2] [Python] Remove unexpected exception in TC_CGEN_2_4.py

With the test commissioner properly resetting the error code the
spurious exception is no longer thrown. Remove the exception handling
from the test.
---
 src/python_testing/TC_CGEN_2_4.py | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/python_testing/TC_CGEN_2_4.py b/src/python_testing/TC_CGEN_2_4.py
index fcfe2aaf044d3d..e9ab9d85c28e73 100644
--- a/src/python_testing/TC_CGEN_2_4.py
+++ b/src/python_testing/TC_CGEN_2_4.py
@@ -104,12 +104,9 @@ async def test_TC_CGEN_2_4(self):
         logging.info('Step 16 - TH2 fully commissions the DUT')
         self.th2.ResetTestCommissioner()
 
-        ctx = asserts.assert_raises(ChipStackError)
-        with ctx:
-            self.th2.CommissionOnNetwork(
-                nodeId=self.dut_node_id, setupPinCode=params.setupPinCode,
-                filterType=ChipDeviceCtrl.DiscoveryFilterType.LONG_DISCRIMINATOR, filter=self.discriminator)
-        asserts.assert_true(ctx.exception.chip_error.sdk_code == 0x02, 'Unexpected error code returned from CommissioningComplete')
+        self.th2.CommissionOnNetwork(
+            nodeId=self.dut_node_id, setupPinCode=params.setupPinCode,
+            filterType=ChipDeviceCtrl.DiscoveryFilterType.LONG_DISCRIMINATOR, filter=self.discriminator)
         logging.info('Commissioning complete done.')
 
         logging.info('Step 17 - TH1 sends an arm failsafe')