Added method in python runner to get eventNumber for TC-SMOKECO tests (#28704)

* Add method to get last event number in python

* Update TC-SMOKECO tests

* Retain event number when reading events

* Improve the method of get event number

* Restyled by autopep8

* Add variable checking

* Update name

---------

Co-authored-by: Hare <renilr0@outlook.com>
Co-authored-by: Restyled.io <commits@restyled.io>
diff --git a/examples/chip-tool/commands/common/RemoteDataModelLogger.cpp b/examples/chip-tool/commands/common/RemoteDataModelLogger.cpp
index e376199..8095ac0 100644
--- a/examples/chip-tool/commands/common/RemoteDataModelLogger.cpp
+++ b/examples/chip-tool/commands/common/RemoteDataModelLogger.cpp
@@ -21,6 +21,7 @@
 #include <lib/support/SafeInt.h>
 #include <lib/support/jsontlv/TlvJson.h>
 
+constexpr const char * kEventNumberKey    = "eventNumber";
 constexpr const char * kDataVersionKey    = "dataVersion";
 constexpr const char * kClusterIdKey      = "clusterId";
 constexpr const char * kEndpointIdKey     = "endpointId";
@@ -129,9 +130,10 @@
     VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
 
     Json::Value value;
-    value[kClusterIdKey]  = header.mPath.mClusterId;
-    value[kEndpointIdKey] = header.mPath.mEndpointId;
-    value[kEventIdKey]    = header.mPath.mEventId;
+    value[kClusterIdKey]   = header.mPath.mClusterId;
+    value[kEndpointIdKey]  = header.mPath.mEndpointId;
+    value[kEventIdKey]     = header.mPath.mEventId;
+    value[kEventNumberKey] = header.mEventNumber;
 
     chip::TLV::TLVReader reader;
     reader.Init(*data);
diff --git a/examples/chip-tool/py_matter_chip_tool_adapter/matter_chip_tool_adapter/decoder.py b/examples/chip-tool/py_matter_chip_tool_adapter/matter_chip_tool_adapter/decoder.py
index 929b805..0ee5642 100644
--- a/examples/chip-tool/py_matter_chip_tool_adapter/matter_chip_tool_adapter/decoder.py
+++ b/examples/chip-tool/py_matter_chip_tool_adapter/matter_chip_tool_adapter/decoder.py
@@ -35,6 +35,7 @@
 _CLUSTER_ERROR = 'clusterError'
 _VALUE = 'value'
 _DATA_VERSION = 'dataVersion'
+_EVENT_NUMBER = 'eventNumber'
 
 # FabricIndex is a special case where the field is added as a struct field by the SDK
 # if needed but is not part of the XML definition of the struct.
@@ -89,7 +90,7 @@
                 elif key == _EVENT_ID:
                     key = _EVENT
                     value = specs.get_event_name(payload[_CLUSTER_ID], value)
-                elif key == _VALUE or key == _ERROR or key == _CLUSTER_ERROR or key == _DATA_VERSION:
+                elif key == _VALUE or key == _ERROR or key == _CLUSTER_ERROR or key == _DATA_VERSION or key == _EVENT_NUMBER:
                     pass
                 else:
                     # Raise an error since the other fields probably needs to be translated too.
diff --git a/scripts/py_matter_yamltests/matter_yamltests/parser.py b/scripts/py_matter_yamltests/matter_yamltests/parser.py
index ef597d2..b5743b8 100644
--- a/scripts/py_matter_yamltests/matter_yamltests/parser.py
+++ b/scripts/py_matter_yamltests/matter_yamltests/parser.py
@@ -688,6 +688,10 @@
     def event_number(self):
         return self._test.event_number
 
+    @event_number.setter
+    def event_number(self, value):
+        self._test.event_number = value
+
     @property
     def pics(self):
         return self._test.pics
diff --git a/scripts/py_matter_yamltests/matter_yamltests/runner.py b/scripts/py_matter_yamltests/matter_yamltests/runner.py
index 25ba0ff..620a821 100644
--- a/scripts/py_matter_yamltests/matter_yamltests/runner.py
+++ b/scripts/py_matter_yamltests/matter_yamltests/runner.py
@@ -124,7 +124,12 @@
 class TestRunner(TestRunnerBase):
     """
     TestRunner is a default runner implementation.
+
+    last_event_number: The latest event number value after the readEvent command.
     """
+
+    last_event_number: int = 0
+
     async def start(self):
         return
 
@@ -175,6 +180,9 @@
 
             test_duration = 0
             for idx, request in enumerate(parser.tests):
+                if request.is_event and request.event_number == 'newEventsOnly':
+                    request.event_number = self.last_event_number + 1
+
                 if not request.is_pics_enabled:
                     hooks.step_skipped(request.label, request.pics)
                     continue
@@ -199,6 +207,13 @@
                 duration = round((time.time() - start) * 1000, 2)
                 test_duration += duration
 
+                if request.is_event:
+                    last_event = responses[-1]
+                    if isinstance(last_event, dict):
+                        received_event_number = last_event.get('eventNumber')
+                        if isinstance(received_event_number, int) and self.last_event_number < received_event_number:
+                            self.last_event_number = received_event_number
+
                 logger = request.post_process_response(responses)
 
                 if logger.is_failure():
diff --git a/scripts/tests/chiptest/__init__.py b/scripts/tests/chiptest/__init__.py
index e3d2f25..d148b04 100644
--- a/scripts/tests/chiptest/__init__.py
+++ b/scripts/tests/chiptest/__init__.py
@@ -135,25 +135,16 @@
         "Test_TC_PSCFG_1_1.yaml",  # Power source configuration cluster is deprecated and removed from all-clusters
         "Test_TC_PSCFG_2_1.yaml",  # Power source configuration cluster is deprecated and removed from all-clusters
         "Test_TC_PSCFG_2_2.yaml",  # Power source configuration cluster is deprecated and removed from all-clusters
-        "Test_TC_SMOKECO_2_6.yaml",          # chip-repl does not support local timeout (07/20/2023) and test assumes
-                                             # TestEventTriggersEnabled is true, which it's not in CI. Also, test
-                                             # has wrong key for eventNumber: because using the right key leads to
-                                             # codegen that does not compile.
         "Test_TC_SMOKECO_2_2.yaml",          # chip-repl does not support local timeout (07/20/2023) and test assumes
-                                             # TestEventTriggersEnabled is true, which it's not in CI. Also, test
-                                             # has wrong key for eventNumber: because using the right key leads to
-                                             # codegen that does not compile.
+                                             # TestEventTriggersEnabled is true, which it's not in CI.
         "Test_TC_SMOKECO_2_3.yaml",          # chip-repl does not support local timeout (07/20/2023) and test assumes
-                                             # TestEventTriggersEnabled is true, which it's not in CI. Also, test
-                                             # has wrong key for eventNumber: because using the right key leads to
-                                             # codegen that does not compile.
+                                             # TestEventTriggersEnabled is true, which it's not in CI.
         "Test_TC_SMOKECO_2_4.yaml",          # chip-repl does not support local timeout (07/20/2023) and test assumes
-                                             # TestEventTriggersEnabled is true, which it's not in CI. Also, test
-                                             # has wrong key for eventNumber: because using the right key leads to
-                                             # codegen that does not compile.
-        "Test_TC_SMOKECO_2_5.yaml",          # chip-repl does not support local timeout (07/20/2023) and test uses unknown
-                                             # keepSubscriptions key in the YAML. Also, test has wrong key for eventNumber:
-                                             # because using the right key leads to codegen that does not compile.
+                                             # TestEventTriggersEnabled is true, which it's not in CI.
+        "Test_TC_SMOKECO_2_5.yaml",          # chip-repl does not support local timeout (07/20/2023) and test assumes
+                                             # TestEventTriggersEnabled is true, which it's not in CI.
+        "Test_TC_SMOKECO_2_6.yaml",          # chip-repl does not support local timeout (07/20/2023) and test assumes
+                                             # TestEventTriggersEnabled is true, which it's not in CI.
     }
 
 
diff --git a/src/app/tests/suites/certification/Test_TC_SMOKECO_2_2.yaml b/src/app/tests/suites/certification/Test_TC_SMOKECO_2_2.yaml
index 7c239e0..740f0de 100644
--- a/src/app/tests/suites/certification/Test_TC_SMOKECO_2_2.yaml
+++ b/src/app/tests/suites/certification/Test_TC_SMOKECO_2_2.yaml
@@ -36,9 +36,6 @@
     TEST_EVENT_TRIGGER_SMOKE_ALARM_CLEAR:
         type: int64u
         defaultValue: "0xffffffff000000a0"
-    EVENT_NUMBER:
-        type: int64u
-        defaultValue: 0
 
 tests:
     - label: "Step 1: Commission DUT to TH"
@@ -69,6 +66,11 @@
           constraints:
               type: enum8
 
+    - label: "TH gets last event number from DUT"
+      PICS: SMOKECO.S.E0a
+      command: "readEvent"
+      event: "AllClear"
+
     - label:
           "Step 4: TH reads TestEventTriggersEnabled attribute from General
           Diagnostics Cluster"
@@ -121,7 +123,7 @@
       PICS: SMOKECO.S.E00
       command: "readEvent"
       event: "SmokeAlarm"
-      EVENT_NUMBER: EVENT_NUMBER + 1
+      eventNumber: "newEventsOnly"
       response:
           value: { AlarmSeverityLevel: 1 }
 
@@ -201,7 +203,7 @@
       PICS: SMOKECO.S.E00
       command: "readEvent"
       event: "SmokeAlarm"
-      EVENT_NUMBER: EVENT_NUMBER + 2
+      eventNumber: "newEventsOnly"
       response:
           value: { AlarmSeverityLevel: 2 }
 
@@ -246,6 +248,6 @@
       PICS: SMOKECO.S.E0a
       command: "readEvent"
       event: "AllClear"
-      EVENT_NUMBER: EVENT_NUMBER + 3
+      eventNumber: "newEventsOnly"
       response:
           value: {}
diff --git a/src/app/tests/suites/certification/Test_TC_SMOKECO_2_3.yaml b/src/app/tests/suites/certification/Test_TC_SMOKECO_2_3.yaml
index 7ce62ab..e18e494 100644
--- a/src/app/tests/suites/certification/Test_TC_SMOKECO_2_3.yaml
+++ b/src/app/tests/suites/certification/Test_TC_SMOKECO_2_3.yaml
@@ -35,9 +35,6 @@
     TEST_EVENT_TRIGGER_CO_ALARM_CLEAR:
         type: int64u
         defaultValue: "0xffffffff000000a1"
-    EVENT_NUMBER:
-        type: int64u
-        defaultValue: 0
 
 tests:
     - label: "Step 1: Commission DUT to TH"
@@ -68,6 +65,11 @@
           constraints:
               type: enum8
 
+    - label: "TH gets last event number from DUT"
+      PICS: SMOKECO.S.E0a
+      command: "readEvent"
+      event: "AllClear"
+
     - label:
           "Step 4: TH reads TestEventTriggersEnabled attribute from General
           Diagnostics Cluster"
@@ -120,7 +122,7 @@
       PICS: SMOKECO.S.E01
       command: "readEvent"
       event: "COAlarm"
-      EVENT_NUMBER: EVENT_NUMBER + 1
+      eventNumber: "newEventsOnly"
       response:
           value: { AlarmSeverityLevel: 1 }
 
@@ -200,7 +202,7 @@
       PICS: SMOKECO.S.E01
       command: "readEvent"
       event: "COAlarm"
-      EVENT_NUMBER: EVENT_NUMBER + 2
+      eventNumber: "newEventsOnly"
       response:
           value: { AlarmSeverityLevel: 2 }
 
@@ -245,6 +247,6 @@
       PICS: SMOKECO.S.E0a
       command: "readEvent"
       event: "AllClear"
-      EVENT_NUMBER: EVENT_NUMBER + 3
+      eventNumber: "newEventsOnly"
       response:
           value: {}
diff --git a/src/app/tests/suites/certification/Test_TC_SMOKECO_2_4.yaml b/src/app/tests/suites/certification/Test_TC_SMOKECO_2_4.yaml
index 29e403a..bac2511 100644
--- a/src/app/tests/suites/certification/Test_TC_SMOKECO_2_4.yaml
+++ b/src/app/tests/suites/certification/Test_TC_SMOKECO_2_4.yaml
@@ -48,9 +48,6 @@
     TEST_EVENT_TRIGGER_END_OF_SERVICE_ALERT_CLEAR:
         type: int64u
         defaultValue: "0xffffffff000000aa"
-    EVENT_NUMBER:
-        type: int64u
-        defaultValue: 0
 
 tests:
     - label: "Step 1: Commission DUT to TH"
@@ -81,6 +78,11 @@
           constraints:
               type: enum8
 
+    - label: "TH gets last event number from DUT"
+      PICS: SMOKECO.S.E0a
+      command: "readEvent"
+      event: "AllClear"
+
     - label:
           "Step 4: TH reads TestEventTriggersEnabled attribute from General
           Diagnostics Cluster"
@@ -133,7 +135,7 @@
       PICS: SMOKECO.S.E02
       command: "readEvent"
       event: "LowBattery"
-      EVENT_NUMBER: EVENT_NUMBER + 1
+      eventNumber: "newEventsOnly"
       response:
           value: { AlarmSeverityLevel: 1 }
 
@@ -178,7 +180,7 @@
       PICS: SMOKECO.S.E02
       command: "readEvent"
       event: "LowBattery"
-      EVENT_NUMBER: EVENT_NUMBER + 2
+      eventNumber: "newEventsOnly"
       response:
           value: { AlarmSeverityLevel: 2 }
 
@@ -223,7 +225,7 @@
       PICS: SMOKECO.S.E0a
       command: "readEvent"
       event: "AllClear"
-      EVENT_NUMBER: EVENT_NUMBER + 3
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -279,7 +281,7 @@
       PICS: SMOKECO.S.E03
       command: "readEvent"
       event: "HardwareFault"
-      EVENT_NUMBER: EVENT_NUMBER + 4
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -325,7 +327,7 @@
       PICS: SMOKECO.S.E0a
       command: "readEvent"
       event: "AllClear"
-      EVENT_NUMBER: EVENT_NUMBER + 5
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -381,7 +383,7 @@
       PICS: SMOKECO.S.E04
       command: "readEvent"
       event: "EndOfService"
-      EVENT_NUMBER: EVENT_NUMBER + 6
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -427,7 +429,7 @@
       PICS: SMOKECO.S.E0a
       command: "readEvent"
       event: "AllClear"
-      EVENT_NUMBER: EVENT_NUMBER + 7
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -499,7 +501,7 @@
       PICS: SMOKECO.S.E05
       command: "readEvent"
       event: "SelfTestComplete"
-      EVENT_NUMBER: EVENT_NUMBER + 8
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -516,7 +518,7 @@
       PICS: SMOKECO.S.E0a
       command: "readEvent"
       event: "AllClear"
-      EVENT_NUMBER: EVENT_NUMBER + 9
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -561,7 +563,7 @@
       PICS: SMOKECO.S.E05
       command: "readEvent"
       event: "SelfTestComplete"
-      EVENT_NUMBER: EVENT_NUMBER + 10
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -578,6 +580,6 @@
       PICS: SMOKECO.S.E0a
       command: "readEvent"
       event: "AllClear"
-      EVENT_NUMBER: EVENT_NUMBER + 11
+      eventNumber: "newEventsOnly"
       response:
           value: {}
diff --git a/src/app/tests/suites/certification/Test_TC_SMOKECO_2_5.yaml b/src/app/tests/suites/certification/Test_TC_SMOKECO_2_5.yaml
index d41f1c6..7082d25 100644
--- a/src/app/tests/suites/certification/Test_TC_SMOKECO_2_5.yaml
+++ b/src/app/tests/suites/certification/Test_TC_SMOKECO_2_5.yaml
@@ -81,9 +81,6 @@
     TTEST_EVENT_TRIGGER_SENSITIVITY_LEVEL_CLEAR:
         type: int64u
         defaultValue: "0xffffffff000000a8"
-    EVENT_NUMBER:
-        type: int64u
-        defaultValue: 0
 
 tests:
     - label: "Step 1: Commission DUT to TH"
@@ -115,6 +112,11 @@
           constraints:
               type: enum8
 
+    - label: "TH gets last event number from DUT"
+      PICS: SMOKECO.S.E0a
+      command: "readEvent"
+      event: "AllClear"
+
     - label:
           "Step 4: TH reads TestEventTriggersEnabled attribute from General
           Diagnostics Cluster"
@@ -161,7 +163,7 @@
       PICS: SMOKECO.S.A0008 && SMOKECO.S.E08
       command: "readEvent"
       event: "InterconnectSmokeAlarm"
-      EVENT_NUMBER: EVENT_NUMBER + 1
+      eventNumber: "newEventsOnly"
       response:
           value: { AlarmSeverityLevel: interconnectSmokeAlarmSeverityLevel }
 
@@ -216,7 +218,7 @@
       PICS: SMOKECO.S.A0008 && SMOKECO.S.E0a
       command: "readEvent"
       event: "AllClear"
-      EVENT_NUMBER: EVENT_NUMBER + 2
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -274,7 +276,7 @@
       PICS: SMOKECO.S.A0009 && SMOKECO.S.E09
       command: "readEvent"
       event: "InterconnectCOAlarm"
-      EVENT_NUMBER: EVENT_NUMBER + 3
+      eventNumber: "newEventsOnly"
       response:
           value: { AlarmSeverityLevel: interconnectCOAlarmSeverityLevel }
 
@@ -329,7 +331,7 @@
       PICS: SMOKECO.S.A0009 && SMOKECO.S.E0a
       command: "readEvent"
       event: "AllClear"
-      EVENT_NUMBER: EVENT_NUMBER + 4
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -687,7 +689,7 @@
       PICS: SMOKECO.S.A0004 && SMOKECO.S.F00 && SMOKECO.S.E06
       command: "readEvent"
       event: "AlarmMuted"
-      EVENT_NUMBER: EVENT_NUMBER + 5
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -724,7 +726,7 @@
       PICS: SMOKECO.S.A0004 && SMOKECO.S.F00 && SMOKECO.S.E07
       command: "readEvent"
       event: "MuteEnded"
-      EVENT_NUMBER: EVENT_NUMBER + 6
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -919,7 +921,7 @@
       PICS: SMOKECO.S.A0004 && SMOKECO.S.F01 && SMOKECO.S.E06
       command: "readEvent"
       event: "AlarmMuted"
-      EVENT_NUMBER: EVENT_NUMBER + 7
+      eventNumber: "newEventsOnly"
       response:
           value: {}
 
@@ -956,7 +958,7 @@
       PICS: SMOKECO.S.A0004 && SMOKECO.S.F01 && SMOKECO.S.E07
       command: "readEvent"
       event: "MuteEnded"
-      EVENT_NUMBER: EVENT_NUMBER + 8
+      eventNumber: "newEventsOnly"
       response:
           value: {}