Skip to content

Commit 17f9d2b

Browse files
Step 2 progress
1 parent 87ce2a4 commit 17f9d2b

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/python_testing/TC_IDM_4_3.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from chip.clusters.Attribute import AttributePath, TypedAttributePath
2828
from chip.exceptions import ChipStackError
2929
from chip.interaction_model import Status
30-
from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main
30+
from matter_testing_support import AttributeChangeCallback, MatterBaseTest, TestStep, async_test_body, default_matter_test_main, EventChangeCallback
3131
from mobly import asserts
3232

3333
'''
@@ -97,7 +97,6 @@ async def test_TC_IDM_4_3(self):
9797

9898
# Test setup
9999
node_label_attr = Clusters.BasicInformation.Attributes.NodeLabel
100-
# node_label_attr = Clusters.OnOff
101100
node_label_attr_path = [(0, node_label_attr)]
102101
TH: ChipDeviceController = self.default_controller
103102

@@ -112,12 +111,17 @@ async def test_TC_IDM_4_3(self):
112111
reportInterval=(3, 5),
113112
keepSubscriptions=False
114113
)
115-
116-
117114

118115

119116

120117

118+
# secs = 60
119+
# print(f"\n\n\n\n\nTime to sleep {secs} second(s)")
120+
# time.sleep(secs)
121+
# print(f"Rise and shine after {secs} second(s)\n\n\n\n\n")
122+
123+
124+
121125

122126

123127

@@ -135,20 +139,26 @@ async def test_TC_IDM_4_3(self):
135139
sub_th_step1a_min_interval_sec, sub_th_step1a_max_interval_sec = sub_th_step1a.GetReportingIntervalsSeconds()
136140
asserts.assert_is_not_none(sub_th_step1a_max_interval_sec, "MaxInterval field not present")
137141

138-
sub_th_step1a.Shutdown()
142+
# sub_th_step1a.Shutdown()
139143

140144
# *** Step 1b ***
141145
# Change the value of the attribute which has been subscribed on the DUT by manually changing some
142146
# settings on the device. Example: Temperature sensor may update the value of the room temperature.
143147
# Turning on/off on a light bulb.
144148
self.step("1b")
145149

146-
# # Modify attribute value
147-
# new_node_label_write = "NewNodeLabel_11001100"
148-
# await TH.WriteAttribute(
149-
# self.dut_node_id,
150-
# [(0, node_label_attr(value=new_node_label_write))]
151-
# )
150+
# Set Attribute Update Callback
151+
node_label_update_cb = AttributeChangeCallback(node_label_attr)
152+
sub_th_step1a.SetAttributeUpdateCallback(node_label_update_cb)
153+
154+
# Modify attribute value
155+
new_node_label_write = "NewNodeLabel_11001100"
156+
await TH.WriteAttribute(
157+
self.dut_node_id,
158+
[(0, node_label_attr(value=new_node_label_write))]
159+
)
160+
161+
node_label_update_cb.wait_for_report()
152162

153163

154164
if __name__ == "__main__":

src/python_testing/matter_testing_support.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
from chip import discovery
5555
from chip.ChipStack import ChipStack
5656
from chip.clusters import ClusterObjects as ClusterObjects
57-
from chip.clusters.Attribute import EventReadResult, SubscriptionTransaction
57+
from chip.clusters.Attribute import EventReadResult, SubscriptionTransaction, TypedAttributePath
5858
from chip.exceptions import ChipStackError
5959
from chip.interaction_model import InteractionModelError, Status
6060
from chip.setup_payload import SetupPayload
@@ -310,6 +310,35 @@ def wait_for_event_report(self, expected_event: ClusterObjects.ClusterEvent, tim
310310
asserts.assert_equal(res.Header.EventId, expected_event.event_id, "Expected event ID not found in event report")
311311
return res.Data
312312

313+
class AttributeChangeCallback:
314+
def __init__(self, expected_attribute: ClusterObjects.ClusterAttributeDescriptor):
315+
self._output = queue.Queue()
316+
self._expected_attribute = expected_attribute
317+
318+
def __call__(self, path: TypedAttributePath, transaction: SubscriptionTransaction):
319+
"""This is the subscription callback when an attribute is updated.
320+
It checks the passed in attribute is the same as the subscribed to attribute and
321+
then posts it into the queue for later processing."""
322+
323+
asserts.assert_equal(path.AttributeType, self._expected_attribute,
324+
f"[AttributeChangeCallback] Attribute mismatch. Expected: {self._expected_attribute}, received: {path.AttributeType}")
325+
logging.info(f"[AttributeChangeCallback] Attribute update callback for {path.AttributeType}")
326+
q = (path, transaction)
327+
self._output.put(q)
328+
329+
def wait_for_report(self):
330+
try:
331+
path, transaction = self._output.get(block=True, timeout=10)
332+
except queue.Empty:
333+
asserts.fail(f"[AttributeChangeCallback] Failed to receive a report for the {self._expected_attribute} attribute change")
334+
335+
asserts.assert_equal(path.AttributeType, self._expected_attribute,
336+
f"[AttributeChangeCallback] Received incorrect report. Expected: {self._expected_attribute}, received: {path.AttributeType}")
337+
try:
338+
attribute_value = transaction.GetAttribute(path)
339+
logging.info(f"[AttributeChangeCallback] Got attribute subscription report. Attribute {path.AttributeType}. Updated value: {attribute_value}. SubscriptionId: {transaction.subscriptionId}")
340+
except KeyError:
341+
asserts.fail("[AttributeChangeCallback] Attribute {expected_attribute} not found in returned report")
313342

314343
class InternalTestRunnerHooks(TestRunnerHooks):
315344

0 commit comments

Comments
 (0)