From 998c3cc500baef2642985c0f847b0517ca1e2a0d Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Wed, 29 Jan 2025 20:18:53 +0100 Subject: [PATCH 01/22] extract utilities to separate module and mark them in original module as deprecated --- .../chip/testing/matter_testing.py | 138 ++++++------------ .../chip/testing/utilities.py | 108 ++++++++++++++ 2 files changed, 149 insertions(+), 97 deletions(-) create mode 100644 src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 1054463ea64489..c3b6184f652667 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -32,16 +32,27 @@ import time import typing import uuid -from binascii import hexlify, unhexlify +import warnings +from binascii import unhexlify from dataclasses import asdict as dataclass_asdict from dataclasses import dataclass, field from datetime import datetime, timedelta, timezone from enum import Enum, IntFlag -from functools import partial +from functools import partial, wraps from itertools import chain from typing import Any, Iterable, List, Optional, Tuple -from chip.tlv import float32, uint +from chip.tlv import uint +from utilities import bytes_from_hex as _bytes_from_hex +from utilities import cluster_id_str as _cluster_id_str +from utilities import compare_time as _compare_time +from utilities import get_wait_seconds_from_set_time as _get_wait_seconds_from_set_time +from utilities import hex_from_bytes as _hex_from_bytes +from utilities import id_str as _id_str +from utilities import type_matches as _type_matches +from utilities import utc_datetime_from_matter_epoch_us as _utc_datetime_from_matter_epoch_us +from utilities import utc_datetime_from_posix_time_ms as _utc_datetime_from_posix_time_ms +from utilities import utc_time_in_matter_epoch as _utc_time_in_matter_epoch # isort: off @@ -94,6 +105,23 @@ class TestRunnerHooks: _DEFAULT_DUT_NODE_ID = 0x12344321 _DEFAULT_TRUST_ROOT_INDEX = 1 +# Deprecation wrappers + + +def _deprecated(module_name): + def decorator(func): + @wraps(func) + def wrapper(*args, **kwargs): + warnings.warn( + f"{func.__name__} is deprecated; import from {module_name} instead.", + DeprecationWarning, + stacklevel=2 + ) + return func(*args, **kwargs) + return wrapper + return decorator + + # Mobly cannot deal with user config passing of ctypes objects, # so we use this dict of uuid -> object to recover items stashed # by reference. @@ -149,76 +177,12 @@ def get_default_paa_trust_store(root_path: pathlib.Path) -> pathlib.Path: return pathlib.Path.cwd() -def type_matches(received_value, desired_type): - """ Checks if the value received matches the expected type. - - Handles unpacking Nullable and Optional types and - compares list value types for non-empty lists. - """ - if typing.get_origin(desired_type) == typing.Union: - return any(type_matches(received_value, t) for t in typing.get_args(desired_type)) - elif typing.get_origin(desired_type) == list: - if isinstance(received_value, list): - # Assume an empty list is of the correct type - return True if received_value == [] else any(type_matches(received_value[0], t) for t in typing.get_args(desired_type)) - else: - return False - elif desired_type == uint: - return isinstance(received_value, int) and received_value >= 0 - elif desired_type == float32: - return isinstance(received_value, float) - else: - return isinstance(received_value, desired_type) - -# TODO(#31177): Need to add unit tests for all time conversion methods. - - -def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): - """ Returns the time in matter epoch in us. - - If desired_datetime is None, it will return the current time. - """ - if desired_datetime is None: - utc_native = datetime.now(tz=timezone.utc) - else: - utc_native = desired_datetime - # Matter epoch is 0 hours, 0 minutes, 0 seconds on Jan 1, 2000 UTC - utc_th_delta = utc_native - datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc) - utc_th_us = int(utc_th_delta.total_seconds() * 1000000) - return utc_th_us - - -matter_epoch_us_from_utc_datetime = utc_time_in_matter_epoch - - -def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: - """Returns the given Matter epoch time as a usable Python datetime in UTC.""" - delta_from_epoch = timedelta(microseconds=matter_epoch_us) - matter_epoch = datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc) - - return matter_epoch + delta_from_epoch - - -def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: - millis = posix_time_ms % 1000 - seconds = posix_time_ms // 1000 - return datetime.fromtimestamp(seconds, timezone.utc) + timedelta(milliseconds=millis) - - -def compare_time(received: int, offset: timedelta = timedelta(), utc: Optional[int] = None, tolerance: timedelta = timedelta(seconds=5)) -> None: - if utc is None: - utc = utc_time_in_matter_epoch() - - # total seconds includes fractional for microseconds - expected = utc + offset.total_seconds() * 1000000 - delta_us = abs(expected - received) - delta = timedelta(microseconds=delta_us) - asserts.assert_less_equal(delta, tolerance, "Received time is out of tolerance") - - -def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): - seconds_passed = (utc_time_in_matter_epoch() - set_time_matter_us) // 1000000 - return wait_seconds - seconds_passed +type_matches = _deprecated("utilities")(_type_matches) +utc_time_in_matter_epoch = _deprecated("utilities")(_utc_time_in_matter_epoch) +utc_datetime_from_matter_epoch_us = _deprecated("utilities")(_utc_datetime_from_matter_epoch_us) +utc_datetime_from_posix_time_ms = _deprecated("utilities")(_utc_datetime_from_posix_time_ms) +compare_time = _deprecated("utilities")(_compare_time) +get_wait_seconds_from_set_time = _deprecated("utilities")(_get_wait_seconds_from_set_time) class SimpleEventCallback: @@ -708,19 +672,8 @@ def get_attribute_string(self, cluster_id: int, attribute_id) -> str: return f"Attribute {attribute_name} ({attribute_id}, 0x{attribute_id:04X})" -def id_str(id): - return f'{id} (0x{id:02x})' - - -def cluster_id_str(id): - if id in Clusters.ClusterObjects.ALL_CLUSTERS.keys(): - s = Clusters.ClusterObjects.ALL_CLUSTERS[id].__name__ - else: - s = "Unknown cluster" - try: - return f'{id_str(id)} {s}' - except TypeError: - return 'HERE IS THE PROBLEM' +id_str = _deprecated("utilities")(_id_str) +cluster_id_str = _deprecated("utilities")(_cluster_id_str) @dataclass @@ -930,17 +883,8 @@ def stack(self) -> ChipStack: return builtins.chipStack -def bytes_from_hex(hex: str) -> bytes: - """Converts any `hex` string representation including `01:ab:cd` to bytes - - Handles any whitespace including newlines, which are all stripped. - """ - return unhexlify("".join(hex.replace(":", "").replace(" ", "").split())) - - -def hex_from_bytes(b: bytes) -> str: - """Converts a bytes object `b` into a hex string (reverse of bytes_from_hex)""" - return hexlify(b).decode("utf-8") +bytes_from_hex = _deprecated("utilities")(_bytes_from_hex) +hex_from_bytes = _deprecated("utilities")(_hex_from_bytes) @dataclass diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py b/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py new file mode 100644 index 00000000000000..7eb3177f034544 --- /dev/null +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py @@ -0,0 +1,108 @@ +import typing +from binascii import hexlify, unhexlify +from datetime import datetime, timedelta, timezone +from typing import Optional + +import chip.clusters as Clusters +from chip.tlv import float32, uint +from mobly import asserts + + +def type_matches(received_value, desired_type): + """ Checks if the value received matches the expected type. + + Handles unpacking Nullable and Optional types and + compares list value types for non-empty lists. + """ + if typing.get_origin(desired_type) == typing.Union: + return any(type_matches(received_value, t) for t in typing.get_args(desired_type)) + elif typing.get_origin(desired_type) == list: + if isinstance(received_value, list): + # Assume an empty list is of the correct type + return True if received_value == [] else any(type_matches(received_value[0], t) for t in typing.get_args(desired_type)) + else: + return False + elif desired_type == uint: + return isinstance(received_value, int) and received_value >= 0 + elif desired_type == float32: + return isinstance(received_value, float) + else: + return isinstance(received_value, desired_type) + +# TODO(#31177): Need to add unit tests for all time conversion methods. + + +def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): + """ Returns the time in matter epoch in us. + + If desired_datetime is None, it will return the current time. + """ + if desired_datetime is None: + utc_native = datetime.now(tz=timezone.utc) + else: + utc_native = desired_datetime + # Matter epoch is 0 hours, 0 minutes, 0 seconds on Jan 1, 2000 UTC + utc_th_delta = utc_native - datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc) + utc_th_us = int(utc_th_delta.total_seconds() * 1000000) + return utc_th_us + + +matter_epoch_us_from_utc_datetime = utc_time_in_matter_epoch + + +def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: + """Returns the given Matter epoch time as a usable Python datetime in UTC.""" + delta_from_epoch = timedelta(microseconds=matter_epoch_us) + matter_epoch = datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc) + + return matter_epoch + delta_from_epoch + + +def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: + millis = posix_time_ms % 1000 + seconds = posix_time_ms // 1000 + return datetime.fromtimestamp(seconds, timezone.utc) + timedelta(milliseconds=millis) + + +def compare_time(received: int, offset: timedelta = timedelta(), utc: Optional[int] = None, tolerance: timedelta = timedelta(seconds=5)) -> None: + if utc is None: + utc = utc_time_in_matter_epoch() + + # total seconds includes fractional for microseconds + expected = utc + offset.total_seconds() * 1000000 + delta_us = abs(expected - received) + delta = timedelta(microseconds=delta_us) + asserts.assert_less_equal(delta, tolerance, "Received time is out of tolerance") + + +def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): + seconds_passed = (utc_time_in_matter_epoch() - set_time_matter_us) // 1000000 + return wait_seconds - seconds_passed + + +def bytes_from_hex(hex: str) -> bytes: + """Converts any `hex` string representation including `01:ab:cd` to bytes + + Handles any whitespace including newlines, which are all stripped. + """ + return unhexlify("".join(hex.replace(":", "").replace(" ", "").split())) + + +def hex_from_bytes(b: bytes) -> str: + """Converts a bytes object `b` into a hex string (reverse of bytes_from_hex)""" + return hexlify(b).decode("utf-8") + + +def id_str(id): + return f'{id} (0x{id:02x})' + + +def cluster_id_str(id): + if id in Clusters.ClusterObjects.ALL_CLUSTERS.keys(): + s = Clusters.ClusterObjects.ALL_CLUSTERS[id].__name__ + else: + s = "Unknown cluster" + try: + return f'{id_str(id)} {s}' + except TypeError: + return 'HERE IS THE PROBLEM' From 54bbb3c9b9cb5b3075ade39600c2cba75e7869f2 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Wed, 29 Jan 2025 20:19:22 +0100 Subject: [PATCH 02/22] update build file to include utilities --- .../matter_testing_infrastructure/BUILD.gn | 1 + .../chip/testing/matter_testing.py | 20 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/BUILD.gn b/src/python_testing/matter_testing_infrastructure/BUILD.gn index 28269a3df1fbed..339c257a6b0c7e 100644 --- a/src/python_testing/matter_testing_infrastructure/BUILD.gn +++ b/src/python_testing/matter_testing_infrastructure/BUILD.gn @@ -47,6 +47,7 @@ pw_python_package("chip-testing-module") { "chip/testing/spec_parsing.py", "chip/testing/taglist_and_topology_test.py", "chip/testing/tasks.py", + "chip/testing/utilities.py", ] tests = [ "chip/testing/test_metadata.py", diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index c3b6184f652667..3c8028c21f5b06 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -43,16 +43,16 @@ from typing import Any, Iterable, List, Optional, Tuple from chip.tlv import uint -from utilities import bytes_from_hex as _bytes_from_hex -from utilities import cluster_id_str as _cluster_id_str -from utilities import compare_time as _compare_time -from utilities import get_wait_seconds_from_set_time as _get_wait_seconds_from_set_time -from utilities import hex_from_bytes as _hex_from_bytes -from utilities import id_str as _id_str -from utilities import type_matches as _type_matches -from utilities import utc_datetime_from_matter_epoch_us as _utc_datetime_from_matter_epoch_us -from utilities import utc_datetime_from_posix_time_ms as _utc_datetime_from_posix_time_ms -from utilities import utc_time_in_matter_epoch as _utc_time_in_matter_epoch +from chip.testing.utilities import bytes_from_hex as _bytes_from_hex +from chip.testing.utilities import cluster_id_str as _cluster_id_str +from chip.testing.utilities import compare_time as _compare_time +from chip.testing.utilities import get_wait_seconds_from_set_time as _get_wait_seconds_from_set_time +from chip.testing.utilities import hex_from_bytes as _hex_from_bytes +from chip.testing.utilities import id_str as _id_str +from chip.testing.utilities import type_matches as _type_matches +from chip.testing.utilities import utc_datetime_from_matter_epoch_us as _utc_datetime_from_matter_epoch_us +from chip.testing.utilities import utc_datetime_from_posix_time_ms as _utc_datetime_from_posix_time_ms +from chip.testing.utilities import utc_time_in_matter_epoch as _utc_time_in_matter_epoch # isort: off From 5e486da94755cd025533c5fbacb66c5da4aab165 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Wed, 29 Jan 2025 20:40:29 +0100 Subject: [PATCH 03/22] Use UserWarning instead of DeprecationWarning; Add type hint decorators for IDE hover visibility --- .../chip/testing/matter_testing.py | 92 ++++++++++++++----- 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 3c8028c21f5b06..351f73ec63988a 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -40,19 +40,19 @@ from enum import Enum, IntFlag from functools import partial, wraps from itertools import chain -from typing import Any, Iterable, List, Optional, Tuple +from typing import Any, Iterable, List, Optional, Tuple, Callable from chip.tlv import uint -from chip.testing.utilities import bytes_from_hex as _bytes_from_hex -from chip.testing.utilities import cluster_id_str as _cluster_id_str -from chip.testing.utilities import compare_time as _compare_time -from chip.testing.utilities import get_wait_seconds_from_set_time as _get_wait_seconds_from_set_time -from chip.testing.utilities import hex_from_bytes as _hex_from_bytes -from chip.testing.utilities import id_str as _id_str -from chip.testing.utilities import type_matches as _type_matches -from chip.testing.utilities import utc_datetime_from_matter_epoch_us as _utc_datetime_from_matter_epoch_us -from chip.testing.utilities import utc_datetime_from_posix_time_ms as _utc_datetime_from_posix_time_ms -from chip.testing.utilities import utc_time_in_matter_epoch as _utc_time_in_matter_epoch +from chip.testing.utilities import bytes_from_hex as new_bytes_from_hex +from chip.testing.utilities import cluster_id_str as new_cluster_id_str +from chip.testing.utilities import compare_time as new_compare_time +from chip.testing.utilities import get_wait_seconds_from_set_time as new_get_wait_seconds_from_set_time +from chip.testing.utilities import hex_from_bytes as new_hex_from_bytes +from chip.testing.utilities import id_str as new_id_str +from chip.testing.utilities import type_matches as new_type_matches +from chip.testing.utilities import utc_datetime_from_matter_epoch_us as new_utc_datetime_from_matter_epoch_us +from chip.testing.utilities import utc_datetime_from_posix_time_ms as new_utc_datetime_from_posix_time_ms +from chip.testing.utilities import utc_time_in_matter_epoch as new_utc_time_in_matter_epoch # isort: off @@ -108,13 +108,13 @@ class TestRunnerHooks: # Deprecation wrappers -def _deprecated(module_name): - def decorator(func): +def _deprecated(module_name: str) -> Callable[[Callable], Callable]: + def decorator(func: Callable) -> Callable: @wraps(func) def wrapper(*args, **kwargs): warnings.warn( f"{func.__name__} is deprecated; import from {module_name} instead.", - DeprecationWarning, + UserWarning, stacklevel=2 ) return func(*args, **kwargs) @@ -177,12 +177,40 @@ def get_default_paa_trust_store(root_path: pathlib.Path) -> pathlib.Path: return pathlib.Path.cwd() -type_matches = _deprecated("utilities")(_type_matches) -utc_time_in_matter_epoch = _deprecated("utilities")(_utc_time_in_matter_epoch) -utc_datetime_from_matter_epoch_us = _deprecated("utilities")(_utc_datetime_from_matter_epoch_us) -utc_datetime_from_posix_time_ms = _deprecated("utilities")(_utc_datetime_from_posix_time_ms) -compare_time = _deprecated("utilities")(_compare_time) -get_wait_seconds_from_set_time = _deprecated("utilities")(_get_wait_seconds_from_set_time) +@_deprecated("utilities") +def type_matches(received_value, desired_type): + """DEPRECATED: Use utilities.type_matches instead""" + return new_type_matches(received_value, desired_type) + + +@_deprecated("utilities") +def utc_time_in_matter_epoch(posix_time_ms: int) -> datetime: + """DEPRECATED: Use utilities.utc_time_in_matter_epoch instead""" + return new_utc_time_in_matter_epoch(posix_time_ms) + + +@_deprecated("utilities") +def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: + """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" + return new_utc_datetime_from_matter_epoch_us(matter_epoch_us) + + +@_deprecated("utilities") +def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: + """DEPRECATED: Use utilities.utc_datetime_from_posix_time_ms instead""" + return new_utc_datetime_from_posix_time_ms(posix_time_ms) + + +@_deprecated("utilities") +def compare_time(time1: datetime, time2: datetime) -> bool: + """DEPRECATED: Use utilities.compare_time instead""" + return new_compare_time(time1, time2) + + +@_deprecated("utilities") +def get_wait_seconds_from_set_time(set_time: datetime) -> float: + """DEPRECATED: Use utilities.get_wait_seconds_from_set_time instead""" + return new_get_wait_seconds_from_set_time(set_time) class SimpleEventCallback: @@ -672,8 +700,16 @@ def get_attribute_string(self, cluster_id: int, attribute_id) -> str: return f"Attribute {attribute_name} ({attribute_id}, 0x{attribute_id:04X})" -id_str = _deprecated("utilities")(_id_str) -cluster_id_str = _deprecated("utilities")(_cluster_id_str) +@_deprecated("utilities") +def id_str(id: int) -> str: + """DEPRECATED: Use utilities.id_str instead""" + return new_id_str(id) + + +@_deprecated("utilities") +def cluster_id_str(id: int) -> str: + """DEPRECATED: Use utilities.cluster_id_str instead""" + return new_cluster_id_str(id) @dataclass @@ -883,8 +919,16 @@ def stack(self) -> ChipStack: return builtins.chipStack -bytes_from_hex = _deprecated("utilities")(_bytes_from_hex) -hex_from_bytes = _deprecated("utilities")(_hex_from_bytes) +@_deprecated("utilities") +def bytes_from_hex(hex: str) -> bytes: + """DEPRECATED: Use utilities.bytes_from_hex instead""" + return new_bytes_from_hex + + +@_deprecated("utilities") +def hex_from_bytes(b: bytes) -> str: + """DEPRECATED: Use utilities.hex_from_bytes instead""" + return new_hex_from_bytes(bytes) @dataclass From ffb910b0e8e9fa7fdf52bf258ccd05f8e6e8cd44 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Wed, 29 Jan 2025 20:41:56 +0100 Subject: [PATCH 04/22] lint code --- .../chip/testing/matter_testing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 351f73ec63988a..43042de47bb33d 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -40,9 +40,8 @@ from enum import Enum, IntFlag from functools import partial, wraps from itertools import chain -from typing import Any, Iterable, List, Optional, Tuple, Callable +from typing import Any, Callable, Iterable, List, Optional, Tuple -from chip.tlv import uint from chip.testing.utilities import bytes_from_hex as new_bytes_from_hex from chip.testing.utilities import cluster_id_str as new_cluster_id_str from chip.testing.utilities import compare_time as new_compare_time @@ -53,6 +52,7 @@ from chip.testing.utilities import utc_datetime_from_matter_epoch_us as new_utc_datetime_from_matter_epoch_us from chip.testing.utilities import utc_datetime_from_posix_time_ms as new_utc_datetime_from_posix_time_ms from chip.testing.utilities import utc_time_in_matter_epoch as new_utc_time_in_matter_epoch +from chip.tlv import uint # isort: off From bf7f0db7b180141b3ea1a31a7a67fb140c1923dd Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 30 Jan 2025 16:20:23 +0100 Subject: [PATCH 05/22] fix incorrect signatures and return values --- .../chip/testing/matter_testing.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 43042de47bb33d..2f24111b8251e0 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -184,9 +184,9 @@ def type_matches(received_value, desired_type): @_deprecated("utilities") -def utc_time_in_matter_epoch(posix_time_ms: int) -> datetime: +def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): """DEPRECATED: Use utilities.utc_time_in_matter_epoch instead""" - return new_utc_time_in_matter_epoch(posix_time_ms) + return new_utc_time_in_matter_epoch(desired_datetime) @_deprecated("utilities") @@ -202,15 +202,15 @@ def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: @_deprecated("utilities") -def compare_time(time1: datetime, time2: datetime) -> bool: +def compare_time(received: int, offset: timedelta = timedelta(), utc: Optional[int] = None, tolerance: timedelta = timedelta(seconds=5)) -> None: """DEPRECATED: Use utilities.compare_time instead""" - return new_compare_time(time1, time2) + return new_compare_time(received, offset, utc, tolerance) @_deprecated("utilities") -def get_wait_seconds_from_set_time(set_time: datetime) -> float: +def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): """DEPRECATED: Use utilities.get_wait_seconds_from_set_time instead""" - return new_get_wait_seconds_from_set_time(set_time) + return new_get_wait_seconds_from_set_time(set_time_matter_us, wait_seconds) class SimpleEventCallback: @@ -701,13 +701,13 @@ def get_attribute_string(self, cluster_id: int, attribute_id) -> str: @_deprecated("utilities") -def id_str(id: int) -> str: +def id_str(id): """DEPRECATED: Use utilities.id_str instead""" return new_id_str(id) @_deprecated("utilities") -def cluster_id_str(id: int) -> str: +def cluster_id_str(id): """DEPRECATED: Use utilities.cluster_id_str instead""" return new_cluster_id_str(id) @@ -922,13 +922,13 @@ def stack(self) -> ChipStack: @_deprecated("utilities") def bytes_from_hex(hex: str) -> bytes: """DEPRECATED: Use utilities.bytes_from_hex instead""" - return new_bytes_from_hex + return new_bytes_from_hex(hex) @_deprecated("utilities") def hex_from_bytes(b: bytes) -> str: """DEPRECATED: Use utilities.hex_from_bytes instead""" - return new_hex_from_bytes(bytes) + return new_hex_from_bytes(b) @dataclass From 5276e70fbd2eb965d58f9b3c52f1de34308c60d9 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 30 Jan 2025 18:26:00 +0100 Subject: [PATCH 06/22] adding missing alias and docstrings to utility methods --- .../chip/testing/matter_testing.py | 6 + .../chip/testing/utilities.py | 126 ++++++++++++++++-- 2 files changed, 123 insertions(+), 9 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 2f24111b8251e0..47de2700c94a36 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -195,6 +195,12 @@ def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: return new_utc_datetime_from_matter_epoch_us(matter_epoch_us) +@_deprecated("utilities") +def matter_epoch_us_from_utc_datetime(matter_epoch_us: int) -> datetime: + """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" + return new_utc_datetime_from_matter_epoch_us(matter_epoch_us) + + @_deprecated("utilities") def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: """DEPRECATED: Use utilities.utc_datetime_from_posix_time_ms instead""" diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py b/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py index 7eb3177f034544..1b5d18ff416560 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py @@ -9,10 +9,18 @@ def type_matches(received_value, desired_type): - """ Checks if the value received matches the expected type. + """ Checks if a received value matches an expected type. - Handles unpacking Nullable and Optional types and - compares list value types for non-empty lists. + Handles unpacking Nullable and Optional types and + compares list value types for non-empty lists. + + Args: + received_value: The value to type check + desired_type: The expected type specification (can be a basic type, Union, + Optional, or List type) + + Returns: + bool: True if the received_value matches the desired_type specification """ if typing.get_origin(desired_type) == typing.Union: return any(type_matches(received_value, t) for t in typing.get_args(desired_type)) @@ -33,9 +41,18 @@ def type_matches(received_value, desired_type): def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): - """ Returns the time in matter epoch in us. + """ Converts a datetime to microseconds since Matter epoch. + + The Matter epoch is defined as January 1, 2000, 00:00:00 UTC. This function + calculates the number of microseconds between the input datetime and the + Matter epoch. If no datetime is provided, the current UTC time is used. - If desired_datetime is None, it will return the current time. + Args: + desired_datetime: Optional datetime to convert, uses current UTC if None. + Should be timezone-aware. + + Returns: + int: Microseconds since Matter epoch """ if desired_datetime is None: utc_native = datetime.now(tz=timezone.utc) @@ -51,7 +68,18 @@ def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: - """Returns the given Matter epoch time as a usable Python datetime in UTC.""" + """ Converts microseconds since Matter epoch to UTC datetime. + + Inverse of utc_time_in_matter_epoch(). + It converts a microsecond timestamp relative to the Matter epoch + (Jan 1, 2000, 00:00:00 UTC) into a timezone-aware Python datetime object. + + Args: + matter_epoch_us: int, microseconds since Matter epoch + + Returns: + datetime: UTC datetime + """ delta_from_epoch = timedelta(microseconds=matter_epoch_us) matter_epoch = datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc) @@ -59,12 +87,35 @@ def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: + """ Converts POSIX timestamp in milliseconds to UTC datetime. + + This function converts a POSIX timestamp (milliseconds since January 1, 1970, 00:00:00 UTC) + to a timezone-aware Python datetime object. + + Args: + posix_time_ms: int, Unix timestamp in milliseconds since Jan 1, 1970 + + Returns: + datetime: UTC datetime + """ millis = posix_time_ms % 1000 seconds = posix_time_ms // 1000 return datetime.fromtimestamp(seconds, timezone.utc) + timedelta(milliseconds=millis) def compare_time(received: int, offset: timedelta = timedelta(), utc: Optional[int] = None, tolerance: timedelta = timedelta(seconds=5)) -> None: + """ Validates a Matter timestamp against expected time within tolerance. + + Args: + received: int, Matter timestamp in microseconds + offset: timedelta, Optional offset from reference time (default: 0s) + utc: Optional[int], Reference time in Matter microseconds (default: current time) + tolerance: timedelta, Maximum allowed time difference (default: 5s) + + Raises: + AssertionError: If the received time differs from the expected time by + more than the specified tolerance + """ if utc is None: utc = utc_time_in_matter_epoch() @@ -76,28 +127,85 @@ def compare_time(received: int, offset: timedelta = timedelta(), utc: Optional[i def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): + """ Calculates remaining wait time based on a previously set timestamp. + + This function determines how many seconds remain from an original wait duration, + accounting for time that has already elapsed since a Matter timestamp. Useful + for implementing timeouts or delays that need to account for already elapsed time. + + Args: + set_time_matter_us: int, Start time in Matter microseconds + wait_seconds: int, Total seconds to wait from start time + + Returns: + int: Remaining seconds (negative if period expired) + + Examples: + >>> start_time = utc_time_in_matter_epoch() + >>> # After 2 seconds... + >>> get_wait_seconds_from_set_time(start_time, 5) + 3 # Returns remaining 3 seconds from original 5 second wait + """ seconds_passed = (utc_time_in_matter_epoch() - set_time_matter_us) // 1000000 return wait_seconds - seconds_passed def bytes_from_hex(hex: str) -> bytes: - """Converts any `hex` string representation including `01:ab:cd` to bytes + """ Converts hex string to bytes, handling various formats (colons, spaces, newlines). - Handles any whitespace including newlines, which are all stripped. + Examples: + "01:ab:cd" -> b'\x01\xab\xcd' + "01 ab cd" -> b'\x01\xab\xcd' """ return unhexlify("".join(hex.replace(":", "").replace(" ", "").split())) def hex_from_bytes(b: bytes) -> str: - """Converts a bytes object `b` into a hex string (reverse of bytes_from_hex)""" + """ Converts a bytes object to a hexadecimal string. + + This function performs the inverse operation of bytes_from_hex(). It converts + a bytes object into a continuous hexadecimal string without any separators. + + Args: + b: bytes, the bytes object to convert to hexadecimal + + Returns: + str: A string containing the hexadecimal representation of the bytes, + using lowercase letters a-f for hex digits + + Example: b'\x01\xab\xcd' -> '01abcd' + """ return hexlify(b).decode("utf-8") def id_str(id): + """ Formats a numeric ID as both decimal and hex. + + Creates a string representation of an ID showing both its decimal value + and its hex representation in parentheses. + + Args: + id: int, the numeric identifier to format + + Returns: + str: A formatted string like "123 (0x7b)" + """ return f'{id} (0x{id:02x})' def cluster_id_str(id): + """ Formats a Matter cluster ID with its name and numeric representation. + + Uses id_str() for numeric formatting and looks up cluster name from registry. + Falls back to "Unknown cluster" if ID not recognized. + + Args: + id: int, the Matter cluster identifier + + Returns: + str: A formatted string containing the ID and cluster name, like + "6 (0x06) OnOff", or "Unknown cluster" if ID not recognized + """ if id in Clusters.ClusterObjects.ALL_CLUSTERS.keys(): s = Clusters.ClusterObjects.ALL_CLUSTERS[id].__name__ else: From 39cf68e9d2bf6d8f823ce431dd952a639786bd98 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 30 Jan 2025 20:49:20 +0100 Subject: [PATCH 07/22] fix alias issue --- .../chip/testing/matter_testing.py | 6 +++--- .../matter_testing_infrastructure/chip/testing/utilities.py | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 47de2700c94a36..1096881d56f91e 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -190,13 +190,13 @@ def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): @_deprecated("utilities") -def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: +def matter_epoch_us_from_utc_datetime(desired_datetime: Optional[datetime] = None): """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" - return new_utc_datetime_from_matter_epoch_us(matter_epoch_us) + return new_utc_time_in_matter_epoch(desired_datetime) @_deprecated("utilities") -def matter_epoch_us_from_utc_datetime(matter_epoch_us: int) -> datetime: +def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" return new_utc_datetime_from_matter_epoch_us(matter_epoch_us) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py b/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py index 1b5d18ff416560..1f72c2fc3c6831 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py @@ -64,9 +64,6 @@ def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): return utc_th_us -matter_epoch_us_from_utc_datetime = utc_time_in_matter_epoch - - def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: """ Converts microseconds since Matter epoch to UTC datetime. From 1bd9b00fcf1a911c4f986e0cde349e7d191cc76e Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Fri, 31 Jan 2025 14:01:27 +0100 Subject: [PATCH 08/22] just update all the places where moved functiones are used --- src/python_testing/TC_DA_1_2.py | 4 +- src/python_testing/TC_DA_1_5.py | 3 +- src/python_testing/TC_DA_1_7.py | 4 +- src/python_testing/TC_DEMTestBase.py | 2 +- src/python_testing/TC_DGGEN_2_4.py | 7 +- src/python_testing/TC_TIMESYNC_2_1.py | 3 +- src/python_testing/TC_TIMESYNC_2_10.py | 9 +- src/python_testing/TC_TIMESYNC_2_11.py | 4 +- src/python_testing/TC_TIMESYNC_2_12.py | 4 +- src/python_testing/TC_TIMESYNC_2_2.py | 4 +- src/python_testing/TC_TIMESYNC_2_4.py | 4 +- src/python_testing/TC_TIMESYNC_2_5.py | 3 +- src/python_testing/TC_TIMESYNC_2_7.py | 4 +- src/python_testing/TC_TIMESYNC_2_8.py | 4 +- src/python_testing/TC_TIMESYNC_2_9.py | 4 +- src/python_testing/TC_VALCC_4_4.py | 4 +- .../TestCommissioningTimeSync.py | 3 +- .../TestMatterTestingSupport.py | 5 +- .../chip/testing/matter_testing.py | 123 +++++++----------- 19 files changed, 84 insertions(+), 114 deletions(-) diff --git a/src/python_testing/TC_DA_1_2.py b/src/python_testing/TC_DA_1_2.py index 5c0b6dbf154d7c..19f2ee37c42dd9 100644 --- a/src/python_testing/TC_DA_1_2.py +++ b/src/python_testing/TC_DA_1_2.py @@ -43,8 +43,8 @@ import chip.clusters as Clusters from chip.interaction_model import InteractionModelError, Status from chip.testing.basic_composition import BasicCompositionTests -from chip.testing.matter_testing import (MatterBaseTest, TestStep, async_test_body, default_matter_test_main, hex_from_bytes, - type_matches) +from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main +from chip.testing.utilities import hex_from_bytes, type_matches from chip.tlv import TLVReader from cryptography import x509 from cryptography.exceptions import InvalidSignature diff --git a/src/python_testing/TC_DA_1_5.py b/src/python_testing/TC_DA_1_5.py index e8e6ce773c6fec..157659f12eb742 100644 --- a/src/python_testing/TC_DA_1_5.py +++ b/src/python_testing/TC_DA_1_5.py @@ -40,7 +40,8 @@ import chip.clusters as Clusters from chip import ChipDeviceCtrl from chip.interaction_model import InteractionModelError, Status -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, hex_from_bytes, type_matches +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.utilities import hex_from_bytes, type_matches from chip.tlv import TLVReader from cryptography import x509 from cryptography.hazmat.primitives import hashes diff --git a/src/python_testing/TC_DA_1_7.py b/src/python_testing/TC_DA_1_7.py index 6678080a600d6b..c738e3a896334f 100644 --- a/src/python_testing/TC_DA_1_7.py +++ b/src/python_testing/TC_DA_1_7.py @@ -41,8 +41,8 @@ from typing import List, Optional import chip.clusters as Clusters -from chip.testing.matter_testing import (MatterBaseTest, TestStep, async_test_body, bytes_from_hex, default_matter_test_main, - hex_from_bytes) +from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main +from chip.testing.utilities import bytes_from_hex, hex_from_bytes from cryptography.exceptions import InvalidSignature from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import ec diff --git a/src/python_testing/TC_DEMTestBase.py b/src/python_testing/TC_DEMTestBase.py index 49c8c6a6375a3e..49becfc3061277 100644 --- a/src/python_testing/TC_DEMTestBase.py +++ b/src/python_testing/TC_DEMTestBase.py @@ -19,7 +19,7 @@ import chip.clusters as Clusters from chip.interaction_model import InteractionModelError, Status -from chip.testing.matter_testing import utc_time_in_matter_epoch +from chip.testing.utilities import utc_time_in_matter_epoch from mobly import asserts logger = logging.getLogger(__name__) diff --git a/src/python_testing/TC_DGGEN_2_4.py b/src/python_testing/TC_DGGEN_2_4.py index 17b068452cdae6..2d8173b324c704 100644 --- a/src/python_testing/TC_DGGEN_2_4.py +++ b/src/python_testing/TC_DGGEN_2_4.py @@ -40,9 +40,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matter_testing import (MatterBaseTest, async_test_body, default_matter_test_main, - matter_epoch_us_from_utc_datetime, utc_datetime_from_matter_epoch_us, - utc_datetime_from_posix_time_ms) +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.utilities import utc_datetime_from_matter_epoch_us, utc_datetime_from_posix_time_ms, utc_time_in_matter_epoch from mobly import asserts logger = logging.getLogger(__name__) @@ -104,7 +103,7 @@ async def test_TC_DGGEN_2_4(self): self.print_step("1b", "Write current time to DUT") # Get current time in the correct format to set via command. - th_utc = matter_epoch_us_from_utc_datetime(desired_datetime=None) + th_utc = utc_time_in_matter_epoch(desired_datetime=None) await self.set_time_in_timesync(th_utc) diff --git a/src/python_testing/TC_TIMESYNC_2_1.py b/src/python_testing/TC_TIMESYNC_2_1.py index cb2df9d66a87e3..2e9888e09bf729 100644 --- a/src/python_testing/TC_TIMESYNC_2_1.py +++ b/src/python_testing/TC_TIMESYNC_2_1.py @@ -42,7 +42,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.testing.matter_testing import (MatterBaseTest, default_matter_test_main, has_attribute, has_cluster, - run_if_endpoint_matches, utc_time_in_matter_epoch) + run_if_endpoint_matches) +from chip.testing.utilities import utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_10.py b/src/python_testing/TC_TIMESYNC_2_10.py index 99ca9f3ac0194b..fa754c61509260 100644 --- a/src/python_testing/TC_TIMESYNC_2_10.py +++ b/src/python_testing/TC_TIMESYNC_2_10.py @@ -43,17 +43,12 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matter_testing import (MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main, - utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main +from chip.testing.utilities import get_wait_seconds_from_set_time, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts -def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): - seconds_passed = int((utc_time_in_matter_epoch() - set_time_matter_us)/1000000) - return wait_seconds - seconds_passed - - class TC_TIMESYNC_2_10(MatterBaseTest): async def send_set_time_zone_cmd(self, tz: typing.List[Clusters.Objects.TimeSynchronization.Structs.TimeZoneStruct]) -> Clusters.Objects.TimeSynchronization.Commands.SetTimeZoneResponse: ret = await self.send_single_cmd(cmd=Clusters.Objects.TimeSynchronization.Commands.SetTimeZone(timeZone=tz), endpoint=self.endpoint) diff --git a/src/python_testing/TC_TIMESYNC_2_11.py b/src/python_testing/TC_TIMESYNC_2_11.py index 0865bc87f4d1cc..979e315aef7c44 100644 --- a/src/python_testing/TC_TIMESYNC_2_11.py +++ b/src/python_testing/TC_TIMESYNC_2_11.py @@ -43,8 +43,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matter_testing import (MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main, - get_wait_seconds_from_set_time, type_matches, utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main +from chip.testing.utilities import get_wait_seconds_from_set_time, type_matches, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_12.py b/src/python_testing/TC_TIMESYNC_2_12.py index 7677dcf184c0b2..d8cbb2d12121fa 100644 --- a/src/python_testing/TC_TIMESYNC_2_12.py +++ b/src/python_testing/TC_TIMESYNC_2_12.py @@ -43,8 +43,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matter_testing import (MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main, - get_wait_seconds_from_set_time, type_matches, utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main +from chip.testing.utilities import get_wait_seconds_from_set_time, type_matches, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_2.py b/src/python_testing/TC_TIMESYNC_2_2.py index 74f935aac9714e..9f96dd91a5322e 100644 --- a/src/python_testing/TC_TIMESYNC_2_2.py +++ b/src/python_testing/TC_TIMESYNC_2_2.py @@ -40,8 +40,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matter_testing import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main, - utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.utilities import compare_time, utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_4.py b/src/python_testing/TC_TIMESYNC_2_4.py index 76fe9074bec56f..764de9532b2a9a 100644 --- a/src/python_testing/TC_TIMESYNC_2_4.py +++ b/src/python_testing/TC_TIMESYNC_2_4.py @@ -40,8 +40,8 @@ import chip.clusters as Clusters from chip.interaction_model import InteractionModelError, Status -from chip.testing.matter_testing import (MatterBaseTest, async_test_body, default_matter_test_main, type_matches, - utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.utilities import type_matches, utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_5.py b/src/python_testing/TC_TIMESYNC_2_5.py index f363f03ba688d8..7948d51a88e351 100644 --- a/src/python_testing/TC_TIMESYNC_2_5.py +++ b/src/python_testing/TC_TIMESYNC_2_5.py @@ -40,7 +40,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError, Status -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, utc_time_in_matter_epoch +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.utilities import utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_7.py b/src/python_testing/TC_TIMESYNC_2_7.py index 1c6cea2023d4de..b5d09e6ed9862e 100644 --- a/src/python_testing/TC_TIMESYNC_2_7.py +++ b/src/python_testing/TC_TIMESYNC_2_7.py @@ -42,8 +42,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matter_testing import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main, type_matches, - utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.utilities import compare_time, type_matches, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_8.py b/src/python_testing/TC_TIMESYNC_2_8.py index 82a77c4c886d17..e9c0199577a049 100644 --- a/src/python_testing/TC_TIMESYNC_2_8.py +++ b/src/python_testing/TC_TIMESYNC_2_8.py @@ -42,8 +42,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matter_testing import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main, type_matches, - utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.utilities import compare_time, type_matches, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_9.py b/src/python_testing/TC_TIMESYNC_2_9.py index 4ce83f81ddbd4b..441af3c580a269 100644 --- a/src/python_testing/TC_TIMESYNC_2_9.py +++ b/src/python_testing/TC_TIMESYNC_2_9.py @@ -41,8 +41,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matter_testing import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main, type_matches, - utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.utilities import compare_time, type_matches, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_VALCC_4_4.py b/src/python_testing/TC_VALCC_4_4.py index 8915bd9044734e..9356e532362658 100644 --- a/src/python_testing/TC_VALCC_4_4.py +++ b/src/python_testing/TC_VALCC_4_4.py @@ -37,8 +37,8 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError, Status -from chip.testing.matter_testing import (MatterBaseTest, TestStep, async_test_body, default_matter_test_main, - utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main +from chip.testing.utilities import utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TestCommissioningTimeSync.py b/src/python_testing/TestCommissioningTimeSync.py index d4033ea58ba8b6..e38cfa22f06f02 100644 --- a/src/python_testing/TestCommissioningTimeSync.py +++ b/src/python_testing/TestCommissioningTimeSync.py @@ -20,7 +20,8 @@ from chip import ChipDeviceCtrl from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError, Status -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, utc_time_in_matter_epoch +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.utilities import utc_time_in_matter_epoch from mobly import asserts # We don't have a good pipe between the c++ enums in CommissioningDelegate and python diff --git a/src/python_testing/TestMatterTestingSupport.py b/src/python_testing/TestMatterTestingSupport.py index f111250481032d..eeac20422dfce8 100644 --- a/src/python_testing/TestMatterTestingSupport.py +++ b/src/python_testing/TestMatterTestingSupport.py @@ -22,13 +22,12 @@ import chip.clusters as Clusters from chip.clusters.Types import Nullable, NullValue -from chip.testing.matter_testing import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main, - get_wait_seconds_from_set_time, parse_matter_test_args, type_matches, - utc_time_in_matter_epoch) +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, parse_matter_test_args from chip.testing.pics import parse_pics, parse_pics_xml from chip.testing.taglist_and_topology_test import (TagProblem, create_device_type_list_for_root, create_device_type_lists, find_tag_list_problems, find_tree_roots, flat_list_ok, get_all_children, get_direct_children_of_root, parts_list_cycles, separate_endpoint_types) +from chip.testing.utilities import compare_time, get_wait_seconds_from_set_time, type_matches, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts, signals diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 1096881d56f91e..5c6ca20cfe222b 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -32,26 +32,16 @@ import time import typing import uuid -import warnings from binascii import unhexlify from dataclasses import asdict as dataclass_asdict from dataclasses import dataclass, field from datetime import datetime, timedelta, timezone from enum import Enum, IntFlag -from functools import partial, wraps +from functools import partial from itertools import chain -from typing import Any, Callable, Iterable, List, Optional, Tuple - -from chip.testing.utilities import bytes_from_hex as new_bytes_from_hex -from chip.testing.utilities import cluster_id_str as new_cluster_id_str -from chip.testing.utilities import compare_time as new_compare_time -from chip.testing.utilities import get_wait_seconds_from_set_time as new_get_wait_seconds_from_set_time -from chip.testing.utilities import hex_from_bytes as new_hex_from_bytes -from chip.testing.utilities import id_str as new_id_str -from chip.testing.utilities import type_matches as new_type_matches -from chip.testing.utilities import utc_datetime_from_matter_epoch_us as new_utc_datetime_from_matter_epoch_us -from chip.testing.utilities import utc_datetime_from_posix_time_ms as new_utc_datetime_from_posix_time_ms -from chip.testing.utilities import utc_time_in_matter_epoch as new_utc_time_in_matter_epoch +from typing import Any, Iterable, List, Optional, Tuple + +from chip.testing.utilities import cluster_id_str, id_str, type_matches from chip.tlv import uint # isort: off @@ -105,23 +95,6 @@ class TestRunnerHooks: _DEFAULT_DUT_NODE_ID = 0x12344321 _DEFAULT_TRUST_ROOT_INDEX = 1 -# Deprecation wrappers - - -def _deprecated(module_name: str) -> Callable[[Callable], Callable]: - def decorator(func: Callable) -> Callable: - @wraps(func) - def wrapper(*args, **kwargs): - warnings.warn( - f"{func.__name__} is deprecated; import from {module_name} instead.", - UserWarning, - stacklevel=2 - ) - return func(*args, **kwargs) - return wrapper - return decorator - - # Mobly cannot deal with user config passing of ctypes objects, # so we use this dict of uuid -> object to recover items stashed # by reference. @@ -177,46 +150,46 @@ def get_default_paa_trust_store(root_path: pathlib.Path) -> pathlib.Path: return pathlib.Path.cwd() -@_deprecated("utilities") -def type_matches(received_value, desired_type): - """DEPRECATED: Use utilities.type_matches instead""" - return new_type_matches(received_value, desired_type) +# @_deprecated("utilities") +# def type_matches(received_value, desired_type): +# """DEPRECATED: Use utilities.type_matches instead""" +# return new_type_matches(received_value, desired_type) -@_deprecated("utilities") -def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): - """DEPRECATED: Use utilities.utc_time_in_matter_epoch instead""" - return new_utc_time_in_matter_epoch(desired_datetime) +# @_deprecated("utilities") +# def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): +# """DEPRECATED: Use utilities.utc_time_in_matter_epoch instead""" +# return new_utc_time_in_matter_epoch(desired_datetime) -@_deprecated("utilities") -def matter_epoch_us_from_utc_datetime(desired_datetime: Optional[datetime] = None): - """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" - return new_utc_time_in_matter_epoch(desired_datetime) +# @_deprecated("utilities") +# def matter_epoch_us_from_utc_datetime(desired_datetime: Optional[datetime] = None): +# """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" +# return new_utc_time_in_matter_epoch(desired_datetime) -@_deprecated("utilities") -def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: - """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" - return new_utc_datetime_from_matter_epoch_us(matter_epoch_us) +# @_deprecated("utilities") +# def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: +# """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" +# return new_utc_datetime_from_matter_epoch_us(matter_epoch_us) -@_deprecated("utilities") -def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: - """DEPRECATED: Use utilities.utc_datetime_from_posix_time_ms instead""" - return new_utc_datetime_from_posix_time_ms(posix_time_ms) +# @_deprecated("utilities") +# def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: +# """DEPRECATED: Use utilities.utc_datetime_from_posix_time_ms instead""" +# return new_utc_datetime_from_posix_time_ms(posix_time_ms) -@_deprecated("utilities") -def compare_time(received: int, offset: timedelta = timedelta(), utc: Optional[int] = None, tolerance: timedelta = timedelta(seconds=5)) -> None: - """DEPRECATED: Use utilities.compare_time instead""" - return new_compare_time(received, offset, utc, tolerance) +# @_deprecated("utilities") +# def compare_time(received: int, offset: timedelta = timedelta(), utc: Optional[int] = None, tolerance: timedelta = timedelta(seconds=5)) -> None: +# """DEPRECATED: Use utilities.compare_time instead""" +# return new_compare_time(received, offset, utc, tolerance) -@_deprecated("utilities") -def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): - """DEPRECATED: Use utilities.get_wait_seconds_from_set_time instead""" - return new_get_wait_seconds_from_set_time(set_time_matter_us, wait_seconds) +# @_deprecated("utilities") +# def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): +# """DEPRECATED: Use utilities.get_wait_seconds_from_set_time instead""" +# return new_get_wait_seconds_from_set_time(set_time_matter_us, wait_seconds) class SimpleEventCallback: @@ -706,16 +679,16 @@ def get_attribute_string(self, cluster_id: int, attribute_id) -> str: return f"Attribute {attribute_name} ({attribute_id}, 0x{attribute_id:04X})" -@_deprecated("utilities") -def id_str(id): - """DEPRECATED: Use utilities.id_str instead""" - return new_id_str(id) +# @_deprecated("utilities") +# def id_str(id): +# """DEPRECATED: Use utilities.id_str instead""" +# return new_id_str(id) -@_deprecated("utilities") -def cluster_id_str(id): - """DEPRECATED: Use utilities.cluster_id_str instead""" - return new_cluster_id_str(id) +# @_deprecated("utilities") +# def cluster_id_str(id): +# """DEPRECATED: Use utilities.cluster_id_str instead""" +# return new_cluster_id_str(id) @dataclass @@ -925,16 +898,16 @@ def stack(self) -> ChipStack: return builtins.chipStack -@_deprecated("utilities") -def bytes_from_hex(hex: str) -> bytes: - """DEPRECATED: Use utilities.bytes_from_hex instead""" - return new_bytes_from_hex(hex) +# @_deprecated("utilities") +# def bytes_from_hex(hex: str) -> bytes: +# """DEPRECATED: Use utilities.bytes_from_hex instead""" +# return new_bytes_from_hex(hex) -@_deprecated("utilities") -def hex_from_bytes(b: bytes) -> str: - """DEPRECATED: Use utilities.hex_from_bytes instead""" - return new_hex_from_bytes(b) +# @_deprecated("utilities") +# def hex_from_bytes(b: bytes) -> str: +# """DEPRECATED: Use utilities.hex_from_bytes instead""" +# return new_hex_from_bytes(b) @dataclass From 12e836ba01aef57a0b66d94904898b419ede49bc Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Fri, 31 Jan 2025 14:04:39 +0100 Subject: [PATCH 09/22] remove commented out code --- .../chip/testing/matter_testing.py | 66 ------------------- 1 file changed, 66 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 5c6ca20cfe222b..e7df25344bc49d 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -150,48 +150,6 @@ def get_default_paa_trust_store(root_path: pathlib.Path) -> pathlib.Path: return pathlib.Path.cwd() -# @_deprecated("utilities") -# def type_matches(received_value, desired_type): -# """DEPRECATED: Use utilities.type_matches instead""" -# return new_type_matches(received_value, desired_type) - - -# @_deprecated("utilities") -# def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): -# """DEPRECATED: Use utilities.utc_time_in_matter_epoch instead""" -# return new_utc_time_in_matter_epoch(desired_datetime) - - -# @_deprecated("utilities") -# def matter_epoch_us_from_utc_datetime(desired_datetime: Optional[datetime] = None): -# """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" -# return new_utc_time_in_matter_epoch(desired_datetime) - - -# @_deprecated("utilities") -# def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: -# """DEPRECATED: Use utilities.utc_datetime_from_matter_epoch_us instead""" -# return new_utc_datetime_from_matter_epoch_us(matter_epoch_us) - - -# @_deprecated("utilities") -# def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: -# """DEPRECATED: Use utilities.utc_datetime_from_posix_time_ms instead""" -# return new_utc_datetime_from_posix_time_ms(posix_time_ms) - - -# @_deprecated("utilities") -# def compare_time(received: int, offset: timedelta = timedelta(), utc: Optional[int] = None, tolerance: timedelta = timedelta(seconds=5)) -> None: -# """DEPRECATED: Use utilities.compare_time instead""" -# return new_compare_time(received, offset, utc, tolerance) - - -# @_deprecated("utilities") -# def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): -# """DEPRECATED: Use utilities.get_wait_seconds_from_set_time instead""" -# return new_get_wait_seconds_from_set_time(set_time_matter_us, wait_seconds) - - class SimpleEventCallback: def __init__(self, name: str, expected_cluster_id: int, expected_event_id: int, output_queue: queue.SimpleQueue): self._name = name @@ -679,18 +637,6 @@ def get_attribute_string(self, cluster_id: int, attribute_id) -> str: return f"Attribute {attribute_name} ({attribute_id}, 0x{attribute_id:04X})" -# @_deprecated("utilities") -# def id_str(id): -# """DEPRECATED: Use utilities.id_str instead""" -# return new_id_str(id) - - -# @_deprecated("utilities") -# def cluster_id_str(id): -# """DEPRECATED: Use utilities.cluster_id_str instead""" -# return new_cluster_id_str(id) - - @dataclass class CustomCommissioningParameters: commissioningParameters: CommissioningParameters @@ -898,18 +844,6 @@ def stack(self) -> ChipStack: return builtins.chipStack -# @_deprecated("utilities") -# def bytes_from_hex(hex: str) -> bytes: -# """DEPRECATED: Use utilities.bytes_from_hex instead""" -# return new_bytes_from_hex(hex) - - -# @_deprecated("utilities") -# def hex_from_bytes(b: bytes) -> str: -# """DEPRECATED: Use utilities.hex_from_bytes instead""" -# return new_hex_from_bytes(b) - - @dataclass class TestStep: test_plan_number: typing.Union[int, str] From 5a1a4fe8c56543ef76c98af3bfcea6a588f04881 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Mon, 3 Feb 2025 12:34:40 +0100 Subject: [PATCH 10/22] using direct aliases as requested in review --- .../chip/testing/matter_testing.py | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index e7df25344bc49d..024f4e6cb3ffda 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -41,7 +41,16 @@ from itertools import chain from typing import Any, Iterable, List, Optional, Tuple -from chip.testing.utilities import cluster_id_str, id_str, type_matches +from chip.testing.utilities import bytes_from_hex as bytes_from_hex +from chip.testing.utilities import cluster_id_str as cluster_id_str +from chip.testing.utilities import compare_time as compare_time +from chip.testing.utilities import get_wait_seconds_from_set_time as get_wait_seconds_from_set_time +from chip.testing.utilities import hex_from_bytes as hex_from_bytes +from chip.testing.utilities import id_str as id_str +from chip.testing.utilities import type_matches as type_matches +from chip.testing.utilities import utc_datetime_from_matter_epoch_us as utc_datetime_from_matter_epoch_us +from chip.testing.utilities import utc_datetime_from_posix_time_ms as utc_datetime_from_posix_time_ms +from chip.testing.utilities import utc_time_in_matter_epoch as utc_time_in_matter_epoch from chip.tlv import uint # isort: off @@ -2479,3 +2488,15 @@ def run_tests(test_class: MatterBaseTest, matter_test_config: MatterTestConfig, if not run_tests_no_exit(test_class, matter_test_config, runner.get_loop(), hooks, default_controller, external_stack): sys.exit(1) + + +type_matches = type_matches +utc_time_in_matter_epoch = utc_time_in_matter_epoch +utc_datetime_from_matter_epoch_us = utc_datetime_from_matter_epoch_us +utc_datetime_from_posix_time_ms = utc_datetime_from_posix_time_ms +compare_time = compare_time +get_wait_seconds_from_set_time = get_wait_seconds_from_set_time +bytes_from_hex = bytes_from_hex +hex_from_bytes = hex_from_bytes +id_str = id_str +cluster_id_str = cluster_id_str From a816491be272b81cea209825db4f59ce8708e5a8 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 6 Feb 2025 18:01:21 +0100 Subject: [PATCH 11/22] solit utilities to less general modules --- src/python_testing/TC_DA_1_2.py | 3 +- src/python_testing/TC_DA_1_5.py | 3 +- src/python_testing/TC_DA_1_7.py | 2 +- src/python_testing/TC_DEMTestBase.py | 2 +- src/python_testing/TC_DGGEN_2_4.py | 2 +- src/python_testing/TC_TIMESYNC_2_1.py | 2 +- src/python_testing/TC_TIMESYNC_2_10.py | 2 +- src/python_testing/TC_TIMESYNC_2_11.py | 3 +- src/python_testing/TC_TIMESYNC_2_12.py | 3 +- src/python_testing/TC_TIMESYNC_2_2.py | 2 +- src/python_testing/TC_TIMESYNC_2_4.py | 3 +- src/python_testing/TC_TIMESYNC_2_5.py | 2 +- src/python_testing/TC_TIMESYNC_2_7.py | 3 +- src/python_testing/TC_TIMESYNC_2_8.py | 3 +- src/python_testing/TC_TIMESYNC_2_9.py | 3 +- src/python_testing/TC_VALCC_4_4.py | 2 +- .../TestCommissioningTimeSync.py | 2 +- .../TestMatterTestingSupport.py | 3 +- .../matter_testing_infrastructure/BUILD.gn | 4 +- .../chip/testing/conversions.py | 69 ++++++++++++ .../chip/testing/matter_testing.py | 20 ++-- .../{utilities.py => timeoperations.py} | 100 ------------------ .../chip/testing/types.py | 34 ++++++ 23 files changed, 143 insertions(+), 129 deletions(-) create mode 100644 src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py rename src/python_testing/matter_testing_infrastructure/chip/testing/{utilities.py => timeoperations.py} (56%) create mode 100644 src/python_testing/matter_testing_infrastructure/chip/testing/types.py diff --git a/src/python_testing/TC_DA_1_2.py b/src/python_testing/TC_DA_1_2.py index 19f2ee37c42dd9..b4216fbdb7ba70 100644 --- a/src/python_testing/TC_DA_1_2.py +++ b/src/python_testing/TC_DA_1_2.py @@ -44,7 +44,8 @@ from chip.interaction_model import InteractionModelError, Status from chip.testing.basic_composition import BasicCompositionTests from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main -from chip.testing.utilities import hex_from_bytes, type_matches +from chip.testing.conversions import hex_from_bytes +from chip.testing.types import type_matches from chip.tlv import TLVReader from cryptography import x509 from cryptography.exceptions import InvalidSignature diff --git a/src/python_testing/TC_DA_1_5.py b/src/python_testing/TC_DA_1_5.py index 157659f12eb742..b5b8fb4b9b0c10 100644 --- a/src/python_testing/TC_DA_1_5.py +++ b/src/python_testing/TC_DA_1_5.py @@ -41,7 +41,8 @@ from chip import ChipDeviceCtrl from chip.interaction_model import InteractionModelError, Status from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main -from chip.testing.utilities import hex_from_bytes, type_matches +from chip.testing.conversions import hex_from_bytes +from chip.testing.types import type_matches from chip.tlv import TLVReader from cryptography import x509 from cryptography.hazmat.primitives import hashes diff --git a/src/python_testing/TC_DA_1_7.py b/src/python_testing/TC_DA_1_7.py index c738e3a896334f..f1c5235cd74347 100644 --- a/src/python_testing/TC_DA_1_7.py +++ b/src/python_testing/TC_DA_1_7.py @@ -42,7 +42,7 @@ import chip.clusters as Clusters from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main -from chip.testing.utilities import bytes_from_hex, hex_from_bytes +from chip.testing.conversions import bytes_from_hex, hex_from_bytes from cryptography.exceptions import InvalidSignature from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import ec diff --git a/src/python_testing/TC_DEMTestBase.py b/src/python_testing/TC_DEMTestBase.py index 49becfc3061277..64c27916b420c4 100644 --- a/src/python_testing/TC_DEMTestBase.py +++ b/src/python_testing/TC_DEMTestBase.py @@ -19,7 +19,7 @@ import chip.clusters as Clusters from chip.interaction_model import InteractionModelError, Status -from chip.testing.utilities import utc_time_in_matter_epoch +from chip.testing.timeoperations import utc_time_in_matter_epoch from mobly import asserts logger = logging.getLogger(__name__) diff --git a/src/python_testing/TC_DGGEN_2_4.py b/src/python_testing/TC_DGGEN_2_4.py index 2d8173b324c704..12a7f01a55c0c4 100644 --- a/src/python_testing/TC_DGGEN_2_4.py +++ b/src/python_testing/TC_DGGEN_2_4.py @@ -41,7 +41,7 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main -from chip.testing.utilities import utc_datetime_from_matter_epoch_us, utc_datetime_from_posix_time_ms, utc_time_in_matter_epoch +from chip.testing.timeoperations import utc_datetime_from_matter_epoch_us, utc_datetime_from_posix_time_ms, utc_time_in_matter_epoch from mobly import asserts logger = logging.getLogger(__name__) diff --git a/src/python_testing/TC_TIMESYNC_2_1.py b/src/python_testing/TC_TIMESYNC_2_1.py index 2e9888e09bf729..e272d2c0a592cd 100644 --- a/src/python_testing/TC_TIMESYNC_2_1.py +++ b/src/python_testing/TC_TIMESYNC_2_1.py @@ -43,7 +43,7 @@ from chip.clusters.Types import NullValue from chip.testing.matter_testing import (MatterBaseTest, default_matter_test_main, has_attribute, has_cluster, run_if_endpoint_matches) -from chip.testing.utilities import utc_time_in_matter_epoch +from chip.testing.timeoperations import utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_10.py b/src/python_testing/TC_TIMESYNC_2_10.py index fa754c61509260..ee815b33a57fed 100644 --- a/src/python_testing/TC_TIMESYNC_2_10.py +++ b/src/python_testing/TC_TIMESYNC_2_10.py @@ -44,7 +44,7 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main -from chip.testing.utilities import get_wait_seconds_from_set_time, utc_time_in_matter_epoch +from chip.testing.timeoperations import get_wait_seconds_from_set_time, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_11.py b/src/python_testing/TC_TIMESYNC_2_11.py index 979e315aef7c44..7eaeef74965daa 100644 --- a/src/python_testing/TC_TIMESYNC_2_11.py +++ b/src/python_testing/TC_TIMESYNC_2_11.py @@ -44,7 +44,8 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main -from chip.testing.utilities import get_wait_seconds_from_set_time, type_matches, utc_time_in_matter_epoch +from chip.testing.timeoperations import get_wait_seconds_from_set_time, utc_time_in_matter_epoch +from chip.testing.types import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_12.py b/src/python_testing/TC_TIMESYNC_2_12.py index d8cbb2d12121fa..86d4527cd72b77 100644 --- a/src/python_testing/TC_TIMESYNC_2_12.py +++ b/src/python_testing/TC_TIMESYNC_2_12.py @@ -44,7 +44,8 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main -from chip.testing.utilities import get_wait_seconds_from_set_time, type_matches, utc_time_in_matter_epoch +from chip.testing.timeoperations import get_wait_seconds_from_set_time, utc_time_in_matter_epoch +from chip.testing.types import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_2.py b/src/python_testing/TC_TIMESYNC_2_2.py index 9f96dd91a5322e..2053bf0e24e98c 100644 --- a/src/python_testing/TC_TIMESYNC_2_2.py +++ b/src/python_testing/TC_TIMESYNC_2_2.py @@ -41,7 +41,7 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main -from chip.testing.utilities import compare_time, utc_time_in_matter_epoch +from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_4.py b/src/python_testing/TC_TIMESYNC_2_4.py index 764de9532b2a9a..55019520c91867 100644 --- a/src/python_testing/TC_TIMESYNC_2_4.py +++ b/src/python_testing/TC_TIMESYNC_2_4.py @@ -41,7 +41,8 @@ import chip.clusters as Clusters from chip.interaction_model import InteractionModelError, Status from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main -from chip.testing.utilities import type_matches, utc_time_in_matter_epoch +from chip.testing.timeoperations import utc_time_in_matter_epoch +from chip.testing.types import type_matches from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_5.py b/src/python_testing/TC_TIMESYNC_2_5.py index 7948d51a88e351..9705511236d781 100644 --- a/src/python_testing/TC_TIMESYNC_2_5.py +++ b/src/python_testing/TC_TIMESYNC_2_5.py @@ -41,7 +41,7 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError, Status from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main -from chip.testing.utilities import utc_time_in_matter_epoch +from chip.testing.timeoperations import utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_7.py b/src/python_testing/TC_TIMESYNC_2_7.py index b5d09e6ed9862e..a192dd392146ca 100644 --- a/src/python_testing/TC_TIMESYNC_2_7.py +++ b/src/python_testing/TC_TIMESYNC_2_7.py @@ -43,7 +43,8 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main -from chip.testing.utilities import compare_time, type_matches, utc_time_in_matter_epoch +from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch +from chip.testing.types import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_8.py b/src/python_testing/TC_TIMESYNC_2_8.py index e9c0199577a049..741adf75be328a 100644 --- a/src/python_testing/TC_TIMESYNC_2_8.py +++ b/src/python_testing/TC_TIMESYNC_2_8.py @@ -43,7 +43,8 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main -from chip.testing.utilities import compare_time, type_matches, utc_time_in_matter_epoch +from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch +from chip.testing.types import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_9.py b/src/python_testing/TC_TIMESYNC_2_9.py index 441af3c580a269..9607ae482ead47 100644 --- a/src/python_testing/TC_TIMESYNC_2_9.py +++ b/src/python_testing/TC_TIMESYNC_2_9.py @@ -42,7 +42,8 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main -from chip.testing.utilities import compare_time, type_matches, utc_time_in_matter_epoch +from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch +from chip.testing.types import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_VALCC_4_4.py b/src/python_testing/TC_VALCC_4_4.py index 9356e532362658..3dc1b93efd3e04 100644 --- a/src/python_testing/TC_VALCC_4_4.py +++ b/src/python_testing/TC_VALCC_4_4.py @@ -38,7 +38,7 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError, Status from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main -from chip.testing.utilities import utc_time_in_matter_epoch +from chip.testing.timeoperations import utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TestCommissioningTimeSync.py b/src/python_testing/TestCommissioningTimeSync.py index e38cfa22f06f02..51d33f2f67344d 100644 --- a/src/python_testing/TestCommissioningTimeSync.py +++ b/src/python_testing/TestCommissioningTimeSync.py @@ -21,7 +21,7 @@ from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError, Status from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main -from chip.testing.utilities import utc_time_in_matter_epoch +from chip.testing.timeoperations import utc_time_in_matter_epoch from mobly import asserts # We don't have a good pipe between the c++ enums in CommissioningDelegate and python diff --git a/src/python_testing/TestMatterTestingSupport.py b/src/python_testing/TestMatterTestingSupport.py index eeac20422dfce8..6b6fbcb82c01bb 100644 --- a/src/python_testing/TestMatterTestingSupport.py +++ b/src/python_testing/TestMatterTestingSupport.py @@ -27,7 +27,8 @@ from chip.testing.taglist_and_topology_test import (TagProblem, create_device_type_list_for_root, create_device_type_lists, find_tag_list_problems, find_tree_roots, flat_list_ok, get_all_children, get_direct_children_of_root, parts_list_cycles, separate_endpoint_types) -from chip.testing.utilities import compare_time, get_wait_seconds_from_set_time, type_matches, utc_time_in_matter_epoch +from chip.testing.timeoperations import compare_time, get_wait_seconds_from_set_time, utc_time_in_matter_epoch +from chip.testing.types import type_matches from chip.tlv import uint from mobly import asserts, signals diff --git a/src/python_testing/matter_testing_infrastructure/BUILD.gn b/src/python_testing/matter_testing_infrastructure/BUILD.gn index 339c257a6b0c7e..859b2181518a7e 100644 --- a/src/python_testing/matter_testing_infrastructure/BUILD.gn +++ b/src/python_testing/matter_testing_infrastructure/BUILD.gn @@ -38,6 +38,7 @@ pw_python_package("chip-testing-module") { "chip/testing/basic_composition.py", "chip/testing/choice_conformance.py", "chip/testing/conformance.py", + "chip/testing/conversions.py", "chip/testing/global_attribute_ids.py", "chip/testing/matter_asserts.py", "chip/testing/matter_testing.py", @@ -47,7 +48,8 @@ pw_python_package("chip-testing-module") { "chip/testing/spec_parsing.py", "chip/testing/taglist_and_topology_test.py", "chip/testing/tasks.py", - "chip/testing/utilities.py", + "chip/testing/timeoperations.py", + "chip/testing/types.py", ] tests = [ "chip/testing/test_metadata.py", diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py b/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py new file mode 100644 index 00000000000000..878f6df6f7d234 --- /dev/null +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py @@ -0,0 +1,69 @@ +from binascii import hexlify, unhexlify + +import chip.clusters as Clusters + + +def bytes_from_hex(hex: str) -> bytes: + """ Converts hex string to bytes, handling various formats (colons, spaces, newlines). + + Examples: + "01:ab:cd" -> b'\x01\xab\xcd' + "01 ab cd" -> b'\x01\xab\xcd' + """ + return unhexlify("".join(hex.replace(":", "").replace(" ", "").split())) + + +def hex_from_bytes(b: bytes) -> str: + """ Converts a bytes object to a hexadecimal string. + + This function performs the inverse operation of bytes_from_hex(). It converts + a bytes object into a continuous hexadecimal string without any separators. + + Args: + b: bytes, the bytes object to convert to hexadecimal + + Returns: + str: A string containing the hexadecimal representation of the bytes, + using lowercase letters a-f for hex digits + + Example: b'\x01\xab\xcd' -> '01abcd' + """ + return hexlify(b).decode("utf-8") + + +def format_decimal_and_hex(number): + """ Formats a number showing both decimal and hexadecimal representations. + + Creates a string representation of a number showing both its decimal value + and its hex representation in parentheses. + + Args: + number: int, the number to format + + Returns: + str: A formatted string like "123 (0x7b)" + """ + return f'{number} (0x{number:02x})' + + +def cluster_id_with_name(id): + """ Formats a Matter cluster ID with its name and numeric representation. + + Uses id_str() for numeric formatting and looks up cluster name from registry. + Falls back to "Unknown cluster" if ID not recognized. + + Args: + id: int, the Matter cluster identifier + + Returns: + str: A formatted string containing the ID and cluster name, like + "6 (0x06) OnOff", or "Unknown cluster" if ID not recognized + """ + if id in Clusters.ClusterObjects.ALL_CLUSTERS.keys(): + s = Clusters.ClusterObjects.ALL_CLUSTERS[id].__name__ + else: + s = "Unknown cluster" + try: + return f'{format_decimal_and_hex(id)} {s}' + except TypeError: + return 'HERE IS THE PROBLEM' diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 024f4e6cb3ffda..aedcda05139d01 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -41,16 +41,16 @@ from itertools import chain from typing import Any, Iterable, List, Optional, Tuple -from chip.testing.utilities import bytes_from_hex as bytes_from_hex -from chip.testing.utilities import cluster_id_str as cluster_id_str -from chip.testing.utilities import compare_time as compare_time -from chip.testing.utilities import get_wait_seconds_from_set_time as get_wait_seconds_from_set_time -from chip.testing.utilities import hex_from_bytes as hex_from_bytes -from chip.testing.utilities import id_str as id_str -from chip.testing.utilities import type_matches as type_matches -from chip.testing.utilities import utc_datetime_from_matter_epoch_us as utc_datetime_from_matter_epoch_us -from chip.testing.utilities import utc_datetime_from_posix_time_ms as utc_datetime_from_posix_time_ms -from chip.testing.utilities import utc_time_in_matter_epoch as utc_time_in_matter_epoch +from chip.testing.conversions import bytes_from_hex as bytes_from_hex +from chip.testing.conversions import cluster_id_with_name as cluster_id_str +from chip.testing.timeoperations import compare_time as compare_time +from chip.testing.timeoperations import get_wait_seconds_from_set_time as get_wait_seconds_from_set_time +from chip.testing.conversions import hex_from_bytes as hex_from_bytes +from chip.testing.conversions import format_decimal_and_hex as id_str +from chip.testing.types import type_matches as type_matches +from chip.testing.timeoperations import utc_datetime_from_matter_epoch_us as utc_datetime_from_matter_epoch_us +from chip.testing.timeoperations import utc_datetime_from_posix_time_ms as utc_datetime_from_posix_time_ms +from chip.testing.timeoperations import utc_time_in_matter_epoch as utc_time_in_matter_epoch from chip.tlv import uint # isort: off diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py b/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py similarity index 56% rename from src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py rename to src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py index 1f72c2fc3c6831..15e0f824632642 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/utilities.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py @@ -1,42 +1,8 @@ -import typing -from binascii import hexlify, unhexlify from datetime import datetime, timedelta, timezone from typing import Optional -import chip.clusters as Clusters -from chip.tlv import float32, uint from mobly import asserts - -def type_matches(received_value, desired_type): - """ Checks if a received value matches an expected type. - - Handles unpacking Nullable and Optional types and - compares list value types for non-empty lists. - - Args: - received_value: The value to type check - desired_type: The expected type specification (can be a basic type, Union, - Optional, or List type) - - Returns: - bool: True if the received_value matches the desired_type specification - """ - if typing.get_origin(desired_type) == typing.Union: - return any(type_matches(received_value, t) for t in typing.get_args(desired_type)) - elif typing.get_origin(desired_type) == list: - if isinstance(received_value, list): - # Assume an empty list is of the correct type - return True if received_value == [] else any(type_matches(received_value[0], t) for t in typing.get_args(desired_type)) - else: - return False - elif desired_type == uint: - return isinstance(received_value, int) and received_value >= 0 - elif desired_type == float32: - return isinstance(received_value, float) - else: - return isinstance(received_value, desired_type) - # TODO(#31177): Need to add unit tests for all time conversion methods. @@ -145,69 +111,3 @@ def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): """ seconds_passed = (utc_time_in_matter_epoch() - set_time_matter_us) // 1000000 return wait_seconds - seconds_passed - - -def bytes_from_hex(hex: str) -> bytes: - """ Converts hex string to bytes, handling various formats (colons, spaces, newlines). - - Examples: - "01:ab:cd" -> b'\x01\xab\xcd' - "01 ab cd" -> b'\x01\xab\xcd' - """ - return unhexlify("".join(hex.replace(":", "").replace(" ", "").split())) - - -def hex_from_bytes(b: bytes) -> str: - """ Converts a bytes object to a hexadecimal string. - - This function performs the inverse operation of bytes_from_hex(). It converts - a bytes object into a continuous hexadecimal string without any separators. - - Args: - b: bytes, the bytes object to convert to hexadecimal - - Returns: - str: A string containing the hexadecimal representation of the bytes, - using lowercase letters a-f for hex digits - - Example: b'\x01\xab\xcd' -> '01abcd' - """ - return hexlify(b).decode("utf-8") - - -def id_str(id): - """ Formats a numeric ID as both decimal and hex. - - Creates a string representation of an ID showing both its decimal value - and its hex representation in parentheses. - - Args: - id: int, the numeric identifier to format - - Returns: - str: A formatted string like "123 (0x7b)" - """ - return f'{id} (0x{id:02x})' - - -def cluster_id_str(id): - """ Formats a Matter cluster ID with its name and numeric representation. - - Uses id_str() for numeric formatting and looks up cluster name from registry. - Falls back to "Unknown cluster" if ID not recognized. - - Args: - id: int, the Matter cluster identifier - - Returns: - str: A formatted string containing the ID and cluster name, like - "6 (0x06) OnOff", or "Unknown cluster" if ID not recognized - """ - if id in Clusters.ClusterObjects.ALL_CLUSTERS.keys(): - s = Clusters.ClusterObjects.ALL_CLUSTERS[id].__name__ - else: - s = "Unknown cluster" - try: - return f'{id_str(id)} {s}' - except TypeError: - return 'HERE IS THE PROBLEM' diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/types.py b/src/python_testing/matter_testing_infrastructure/chip/testing/types.py new file mode 100644 index 00000000000000..8b71dfab75dff9 --- /dev/null +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/types.py @@ -0,0 +1,34 @@ +from chip.tlv import float32, uint + + +import typing + + +def type_matches(received_value, desired_type): + """ Checks if a received value matches an expected type. + + Handles unpacking Nullable and Optional types and + compares list value types for non-empty lists. + + Args: + received_value: The value to type check + desired_type: The expected type specification (can be a basic type, Union, + Optional, or List type) + + Returns: + bool: True if the received_value matches the desired_type specification + """ + if typing.get_origin(desired_type) == typing.Union: + return any(type_matches(received_value, t) for t in typing.get_args(desired_type)) + elif typing.get_origin(desired_type) == list: + if isinstance(received_value, list): + # Assume an empty list is of the correct type + return True if received_value == [] else any(type_matches(received_value[0], t) for t in typing.get_args(desired_type)) + else: + return False + elif desired_type == uint: + return isinstance(received_value, int) and received_value >= 0 + elif desired_type == float32: + return isinstance(received_value, float) + else: + return isinstance(received_value, desired_type) From 86a464a383b273d6dc2fcad9dd63c92461cc6eeb Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 6 Feb 2025 18:03:01 +0100 Subject: [PATCH 12/22] isort --- .../chip/testing/matter_testing.py | 6 +++--- .../matter_testing_infrastructure/chip/testing/types.py | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index aedcda05139d01..0d62bf0ee3262d 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -43,14 +43,14 @@ from chip.testing.conversions import bytes_from_hex as bytes_from_hex from chip.testing.conversions import cluster_id_with_name as cluster_id_str +from chip.testing.conversions import format_decimal_and_hex as id_str +from chip.testing.conversions import hex_from_bytes as hex_from_bytes from chip.testing.timeoperations import compare_time as compare_time from chip.testing.timeoperations import get_wait_seconds_from_set_time as get_wait_seconds_from_set_time -from chip.testing.conversions import hex_from_bytes as hex_from_bytes -from chip.testing.conversions import format_decimal_and_hex as id_str -from chip.testing.types import type_matches as type_matches from chip.testing.timeoperations import utc_datetime_from_matter_epoch_us as utc_datetime_from_matter_epoch_us from chip.testing.timeoperations import utc_datetime_from_posix_time_ms as utc_datetime_from_posix_time_ms from chip.testing.timeoperations import utc_time_in_matter_epoch as utc_time_in_matter_epoch +from chip.testing.types import type_matches as type_matches from chip.tlv import uint # isort: off diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/types.py b/src/python_testing/matter_testing_infrastructure/chip/testing/types.py index 8b71dfab75dff9..cbb204389493df 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/types.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/types.py @@ -1,8 +1,7 @@ -from chip.tlv import float32, uint - - import typing +from chip.tlv import float32, uint + def type_matches(received_value, desired_type): """ Checks if a received value matches an expected type. From 1480b6ea2eb2303f8369e45179f32a6cd5439999 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 6 Feb 2025 18:11:47 +0100 Subject: [PATCH 13/22] rename types.py to matchers.py --- src/python_testing/TC_DA_1_2.py | 2 +- src/python_testing/TC_DA_1_5.py | 2 +- src/python_testing/TC_TIMESYNC_2_11.py | 2 +- src/python_testing/TC_TIMESYNC_2_12.py | 2 +- src/python_testing/TC_TIMESYNC_2_4.py | 2 +- src/python_testing/TC_TIMESYNC_2_7.py | 2 +- src/python_testing/TC_TIMESYNC_2_8.py | 2 +- src/python_testing/TC_TIMESYNC_2_9.py | 2 +- src/python_testing/TestMatterTestingSupport.py | 2 +- src/python_testing/matter_testing_infrastructure/BUILD.gn | 2 +- .../chip/testing/{types.py => matchers.py} | 0 .../chip/testing/matter_testing.py | 2 +- 12 files changed, 11 insertions(+), 11 deletions(-) rename src/python_testing/matter_testing_infrastructure/chip/testing/{types.py => matchers.py} (100%) diff --git a/src/python_testing/TC_DA_1_2.py b/src/python_testing/TC_DA_1_2.py index b4216fbdb7ba70..28ada0ec48834b 100644 --- a/src/python_testing/TC_DA_1_2.py +++ b/src/python_testing/TC_DA_1_2.py @@ -45,7 +45,7 @@ from chip.testing.basic_composition import BasicCompositionTests from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main from chip.testing.conversions import hex_from_bytes -from chip.testing.types import type_matches +from chip.testing.matchers import type_matches from chip.tlv import TLVReader from cryptography import x509 from cryptography.exceptions import InvalidSignature diff --git a/src/python_testing/TC_DA_1_5.py b/src/python_testing/TC_DA_1_5.py index b5b8fb4b9b0c10..535f2507e987bc 100644 --- a/src/python_testing/TC_DA_1_5.py +++ b/src/python_testing/TC_DA_1_5.py @@ -42,7 +42,7 @@ from chip.interaction_model import InteractionModelError, Status from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.conversions import hex_from_bytes -from chip.testing.types import type_matches +from chip.testing.matchers import type_matches from chip.tlv import TLVReader from cryptography import x509 from cryptography.hazmat.primitives import hashes diff --git a/src/python_testing/TC_TIMESYNC_2_11.py b/src/python_testing/TC_TIMESYNC_2_11.py index 7eaeef74965daa..e96a6f3109cdbf 100644 --- a/src/python_testing/TC_TIMESYNC_2_11.py +++ b/src/python_testing/TC_TIMESYNC_2_11.py @@ -45,7 +45,7 @@ from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main from chip.testing.timeoperations import get_wait_seconds_from_set_time, utc_time_in_matter_epoch -from chip.testing.types import type_matches +from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_12.py b/src/python_testing/TC_TIMESYNC_2_12.py index 86d4527cd72b77..6915cc46887574 100644 --- a/src/python_testing/TC_TIMESYNC_2_12.py +++ b/src/python_testing/TC_TIMESYNC_2_12.py @@ -45,7 +45,7 @@ from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main from chip.testing.timeoperations import get_wait_seconds_from_set_time, utc_time_in_matter_epoch -from chip.testing.types import type_matches +from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_4.py b/src/python_testing/TC_TIMESYNC_2_4.py index 55019520c91867..167b72739e3f54 100644 --- a/src/python_testing/TC_TIMESYNC_2_4.py +++ b/src/python_testing/TC_TIMESYNC_2_4.py @@ -42,7 +42,7 @@ from chip.interaction_model import InteractionModelError, Status from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.timeoperations import utc_time_in_matter_epoch -from chip.testing.types import type_matches +from chip.testing.matchers import type_matches from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_7.py b/src/python_testing/TC_TIMESYNC_2_7.py index a192dd392146ca..11c09929e71661 100644 --- a/src/python_testing/TC_TIMESYNC_2_7.py +++ b/src/python_testing/TC_TIMESYNC_2_7.py @@ -44,7 +44,7 @@ from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch -from chip.testing.types import type_matches +from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_8.py b/src/python_testing/TC_TIMESYNC_2_8.py index 741adf75be328a..9041601c935084 100644 --- a/src/python_testing/TC_TIMESYNC_2_8.py +++ b/src/python_testing/TC_TIMESYNC_2_8.py @@ -44,7 +44,7 @@ from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch -from chip.testing.types import type_matches +from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_9.py b/src/python_testing/TC_TIMESYNC_2_9.py index 9607ae482ead47..f101f344a87d5f 100644 --- a/src/python_testing/TC_TIMESYNC_2_9.py +++ b/src/python_testing/TC_TIMESYNC_2_9.py @@ -43,7 +43,7 @@ from chip.interaction_model import InteractionModelError from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch -from chip.testing.types import type_matches +from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TestMatterTestingSupport.py b/src/python_testing/TestMatterTestingSupport.py index 6b6fbcb82c01bb..ea094e432e46fa 100644 --- a/src/python_testing/TestMatterTestingSupport.py +++ b/src/python_testing/TestMatterTestingSupport.py @@ -28,7 +28,7 @@ find_tag_list_problems, find_tree_roots, flat_list_ok, get_all_children, get_direct_children_of_root, parts_list_cycles, separate_endpoint_types) from chip.testing.timeoperations import compare_time, get_wait_seconds_from_set_time, utc_time_in_matter_epoch -from chip.testing.types import type_matches +from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts, signals diff --git a/src/python_testing/matter_testing_infrastructure/BUILD.gn b/src/python_testing/matter_testing_infrastructure/BUILD.gn index 859b2181518a7e..41bac215facf0e 100644 --- a/src/python_testing/matter_testing_infrastructure/BUILD.gn +++ b/src/python_testing/matter_testing_infrastructure/BUILD.gn @@ -33,6 +33,7 @@ pw_python_package("chip-testing-module") { inputs = [ "env_test.yaml" ] sources = [ + "chip.testing/matchers.py", "chip/testing/__init__.py", "chip/testing/apps.py", "chip/testing/basic_composition.py", @@ -49,7 +50,6 @@ pw_python_package("chip-testing-module") { "chip/testing/taglist_and_topology_test.py", "chip/testing/tasks.py", "chip/testing/timeoperations.py", - "chip/testing/types.py", ] tests = [ "chip/testing/test_metadata.py", diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/types.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py similarity index 100% rename from src/python_testing/matter_testing_infrastructure/chip/testing/types.py rename to src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 0d62bf0ee3262d..ff785d892eddfb 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -50,7 +50,7 @@ from chip.testing.timeoperations import utc_datetime_from_matter_epoch_us as utc_datetime_from_matter_epoch_us from chip.testing.timeoperations import utc_datetime_from_posix_time_ms as utc_datetime_from_posix_time_ms from chip.testing.timeoperations import utc_time_in_matter_epoch as utc_time_in_matter_epoch -from chip.testing.types import type_matches as type_matches +from chip.testing.matchers import type_matches as type_matches from chip.tlv import uint # isort: off From 7505489ca76ce7af899feeda3837be184c3f2670 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 6 Feb 2025 18:22:23 +0100 Subject: [PATCH 14/22] rename type_matches to is_type --- .../matter_testing_infrastructure/chip/testing/matchers.py | 6 +++--- .../chip/testing/matter_testing.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py index cbb204389493df..924d7351a328c2 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py @@ -3,7 +3,7 @@ from chip.tlv import float32, uint -def type_matches(received_value, desired_type): +def is_type(received_value, desired_type): """ Checks if a received value matches an expected type. Handles unpacking Nullable and Optional types and @@ -18,11 +18,11 @@ def type_matches(received_value, desired_type): bool: True if the received_value matches the desired_type specification """ if typing.get_origin(desired_type) == typing.Union: - return any(type_matches(received_value, t) for t in typing.get_args(desired_type)) + return any(is_type(received_value, t) for t in typing.get_args(desired_type)) elif typing.get_origin(desired_type) == list: if isinstance(received_value, list): # Assume an empty list is of the correct type - return True if received_value == [] else any(type_matches(received_value[0], t) for t in typing.get_args(desired_type)) + return True if received_value == [] else any(is_type(received_value[0], t) for t in typing.get_args(desired_type)) else: return False elif desired_type == uint: diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index ff785d892eddfb..65137cb77ca275 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -50,7 +50,7 @@ from chip.testing.timeoperations import utc_datetime_from_matter_epoch_us as utc_datetime_from_matter_epoch_us from chip.testing.timeoperations import utc_datetime_from_posix_time_ms as utc_datetime_from_posix_time_ms from chip.testing.timeoperations import utc_time_in_matter_epoch as utc_time_in_matter_epoch -from chip.testing.matchers import type_matches as type_matches +from chip.testing.matchers import is_type as type_matches from chip.tlv import uint # isort: off From e093a0b4de1a0b69c97b1d1eb5ab65b6f6e746a1 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 6 Feb 2025 18:35:52 +0100 Subject: [PATCH 15/22] added docstrings and copyright to packages --- .../matter_testing_infrastructure/BUILD.gn | 2 +- .../chip/testing/conversions.py | 23 ++++++++++++++++++ .../chip/testing/matchers.py | 22 +++++++++++++++++ .../chip/testing/matter_testing.py | 2 +- .../chip/testing/timeoperations.py | 24 +++++++++++++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/BUILD.gn b/src/python_testing/matter_testing_infrastructure/BUILD.gn index 41bac215facf0e..6012235f4c664d 100644 --- a/src/python_testing/matter_testing_infrastructure/BUILD.gn +++ b/src/python_testing/matter_testing_infrastructure/BUILD.gn @@ -33,7 +33,6 @@ pw_python_package("chip-testing-module") { inputs = [ "env_test.yaml" ] sources = [ - "chip.testing/matchers.py", "chip/testing/__init__.py", "chip/testing/apps.py", "chip/testing/basic_composition.py", @@ -41,6 +40,7 @@ pw_python_package("chip-testing-module") { "chip/testing/conformance.py", "chip/testing/conversions.py", "chip/testing/global_attribute_ids.py", + "chip/testing/matchers.py", "chip/testing/matter_asserts.py", "chip/testing/matter_testing.py", "chip/testing/metadata.py", diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py b/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py index 878f6df6f7d234..5cb127af61a773 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py @@ -1,3 +1,26 @@ +# +# Copyright (c) 2025 Project CHIP Authors +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""Conversion utilities for Matter testing infrastructure. + +This module provides utility functions for converting between different data +representations commonly used in Matter testing. +""" + from binascii import hexlify, unhexlify import chip.clusters as Clusters diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py index 924d7351a328c2..8c4d86178dc3d4 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py @@ -1,3 +1,25 @@ +# +# Copyright (c) 2025 Project CHIP Authors +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +"""Type matching utilities for Matter testing infrastructure. + +This module provides functionality for validating type compatibility between +received values and expected type specifications. +""" + import typing from chip.tlv import float32, uint diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 65137cb77ca275..8b938382f78739 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -45,12 +45,12 @@ from chip.testing.conversions import cluster_id_with_name as cluster_id_str from chip.testing.conversions import format_decimal_and_hex as id_str from chip.testing.conversions import hex_from_bytes as hex_from_bytes +from chip.testing.matchers import is_type as type_matches from chip.testing.timeoperations import compare_time as compare_time from chip.testing.timeoperations import get_wait_seconds_from_set_time as get_wait_seconds_from_set_time from chip.testing.timeoperations import utc_datetime_from_matter_epoch_us as utc_datetime_from_matter_epoch_us from chip.testing.timeoperations import utc_datetime_from_posix_time_ms as utc_datetime_from_posix_time_ms from chip.testing.timeoperations import utc_time_in_matter_epoch as utc_time_in_matter_epoch -from chip.testing.matchers import is_type as type_matches from chip.tlv import uint # isort: off diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py b/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py index 15e0f824632642..fbe366d35333e6 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py @@ -1,3 +1,27 @@ +# +# Copyright (c) 2025 Project CHIP Authors +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""Time conversion and validation utilities for Matter testing infrastructure. + +This module provides functionality for handling time-related operations in Matter +testing, particularly focusing on conversions between different time formats and +epochs. +""" + from datetime import datetime, timedelta, timezone from typing import Optional From 5fdbc2180f223dc7afde8dc6306077690a348dac Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 6 Feb 2025 18:37:26 +0100 Subject: [PATCH 16/22] style fix --- .../matter_testing_infrastructure/chip/testing/matchers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py index 8c4d86178dc3d4..c833b290d25a2e 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # + """Type matching utilities for Matter testing infrastructure. This module provides functionality for validating type compatibility between From bbb911adb0090f3794501b217bef828ba0238c47 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Thu, 6 Feb 2025 18:43:06 +0100 Subject: [PATCH 17/22] isort --- src/python_testing/TC_DA_1_2.py | 2 +- src/python_testing/TC_DA_1_5.py | 2 +- src/python_testing/TC_DA_1_7.py | 2 +- src/python_testing/TC_TIMESYNC_2_11.py | 2 +- src/python_testing/TC_TIMESYNC_2_12.py | 2 +- src/python_testing/TC_TIMESYNC_2_4.py | 2 +- src/python_testing/TC_TIMESYNC_2_7.py | 2 +- src/python_testing/TC_TIMESYNC_2_8.py | 2 +- src/python_testing/TC_TIMESYNC_2_9.py | 2 +- src/python_testing/TestMatterTestingSupport.py | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/python_testing/TC_DA_1_2.py b/src/python_testing/TC_DA_1_2.py index 28ada0ec48834b..1f46f19b4d2022 100644 --- a/src/python_testing/TC_DA_1_2.py +++ b/src/python_testing/TC_DA_1_2.py @@ -43,9 +43,9 @@ import chip.clusters as Clusters from chip.interaction_model import InteractionModelError, Status from chip.testing.basic_composition import BasicCompositionTests -from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main from chip.testing.conversions import hex_from_bytes from chip.testing.matchers import type_matches +from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main from chip.tlv import TLVReader from cryptography import x509 from cryptography.exceptions import InvalidSignature diff --git a/src/python_testing/TC_DA_1_5.py b/src/python_testing/TC_DA_1_5.py index 535f2507e987bc..affb355d59f98a 100644 --- a/src/python_testing/TC_DA_1_5.py +++ b/src/python_testing/TC_DA_1_5.py @@ -40,9 +40,9 @@ import chip.clusters as Clusters from chip import ChipDeviceCtrl from chip.interaction_model import InteractionModelError, Status -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.conversions import hex_from_bytes from chip.testing.matchers import type_matches +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.tlv import TLVReader from cryptography import x509 from cryptography.hazmat.primitives import hashes diff --git a/src/python_testing/TC_DA_1_7.py b/src/python_testing/TC_DA_1_7.py index f1c5235cd74347..ce9399d8b7896c 100644 --- a/src/python_testing/TC_DA_1_7.py +++ b/src/python_testing/TC_DA_1_7.py @@ -41,8 +41,8 @@ from typing import List, Optional import chip.clusters as Clusters -from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main from chip.testing.conversions import bytes_from_hex, hex_from_bytes +from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main from cryptography.exceptions import InvalidSignature from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import ec diff --git a/src/python_testing/TC_TIMESYNC_2_11.py b/src/python_testing/TC_TIMESYNC_2_11.py index e96a6f3109cdbf..9f255679fb1313 100644 --- a/src/python_testing/TC_TIMESYNC_2_11.py +++ b/src/python_testing/TC_TIMESYNC_2_11.py @@ -43,9 +43,9 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError +from chip.testing.matchers import type_matches from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main from chip.testing.timeoperations import get_wait_seconds_from_set_time, utc_time_in_matter_epoch -from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_12.py b/src/python_testing/TC_TIMESYNC_2_12.py index 6915cc46887574..7e459e6306bc43 100644 --- a/src/python_testing/TC_TIMESYNC_2_12.py +++ b/src/python_testing/TC_TIMESYNC_2_12.py @@ -43,9 +43,9 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError +from chip.testing.matchers import type_matches from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main from chip.testing.timeoperations import get_wait_seconds_from_set_time, utc_time_in_matter_epoch -from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_4.py b/src/python_testing/TC_TIMESYNC_2_4.py index 167b72739e3f54..b30de37d6ce6e1 100644 --- a/src/python_testing/TC_TIMESYNC_2_4.py +++ b/src/python_testing/TC_TIMESYNC_2_4.py @@ -40,9 +40,9 @@ import chip.clusters as Clusters from chip.interaction_model import InteractionModelError, Status +from chip.testing.matchers import type_matches from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.timeoperations import utc_time_in_matter_epoch -from chip.testing.matchers import type_matches from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_7.py b/src/python_testing/TC_TIMESYNC_2_7.py index 11c09929e71661..62a875b7579811 100644 --- a/src/python_testing/TC_TIMESYNC_2_7.py +++ b/src/python_testing/TC_TIMESYNC_2_7.py @@ -42,9 +42,9 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError +from chip.testing.matchers import type_matches from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch -from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_8.py b/src/python_testing/TC_TIMESYNC_2_8.py index 9041601c935084..47c2463bf5a37f 100644 --- a/src/python_testing/TC_TIMESYNC_2_8.py +++ b/src/python_testing/TC_TIMESYNC_2_8.py @@ -42,9 +42,9 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError +from chip.testing.matchers import type_matches from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch -from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_9.py b/src/python_testing/TC_TIMESYNC_2_9.py index f101f344a87d5f..437293e3003a7e 100644 --- a/src/python_testing/TC_TIMESYNC_2_9.py +++ b/src/python_testing/TC_TIMESYNC_2_9.py @@ -41,9 +41,9 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError +from chip.testing.matchers import type_matches from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch -from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TestMatterTestingSupport.py b/src/python_testing/TestMatterTestingSupport.py index ea094e432e46fa..ad490002313f7c 100644 --- a/src/python_testing/TestMatterTestingSupport.py +++ b/src/python_testing/TestMatterTestingSupport.py @@ -22,13 +22,13 @@ import chip.clusters as Clusters from chip.clusters.Types import Nullable, NullValue +from chip.testing.matchers import type_matches from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, parse_matter_test_args from chip.testing.pics import parse_pics, parse_pics_xml from chip.testing.taglist_and_topology_test import (TagProblem, create_device_type_list_for_root, create_device_type_lists, find_tag_list_problems, find_tree_roots, flat_list_ok, get_all_children, get_direct_children_of_root, parts_list_cycles, separate_endpoint_types) from chip.testing.timeoperations import compare_time, get_wait_seconds_from_set_time, utc_time_in_matter_epoch -from chip.testing.matchers import type_matches from chip.tlv import uint from mobly import asserts, signals From 6384035580d6776b30c4e77bd0a2b1603f9ef59c Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Fri, 7 Feb 2025 11:26:03 +0100 Subject: [PATCH 18/22] point test_testing imports to original module for now --- src/python_testing/TC_DA_1_2.py | 3 +-- src/python_testing/TC_DA_1_5.py | 3 +-- src/python_testing/TC_TIMESYNC_2_11.py | 3 +-- src/python_testing/TC_TIMESYNC_2_12.py | 3 +-- src/python_testing/TC_TIMESYNC_2_4.py | 3 +-- src/python_testing/TC_TIMESYNC_2_7.py | 3 +-- src/python_testing/TC_TIMESYNC_2_8.py | 3 +-- src/python_testing/TC_TIMESYNC_2_9.py | 3 +-- src/python_testing/TestMatterTestingSupport.py | 4 ++-- 9 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/python_testing/TC_DA_1_2.py b/src/python_testing/TC_DA_1_2.py index 1f46f19b4d2022..dde370293eb514 100644 --- a/src/python_testing/TC_DA_1_2.py +++ b/src/python_testing/TC_DA_1_2.py @@ -44,8 +44,7 @@ from chip.interaction_model import InteractionModelError, Status from chip.testing.basic_composition import BasicCompositionTests from chip.testing.conversions import hex_from_bytes -from chip.testing.matchers import type_matches -from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main +from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main, type_matches from chip.tlv import TLVReader from cryptography import x509 from cryptography.exceptions import InvalidSignature diff --git a/src/python_testing/TC_DA_1_5.py b/src/python_testing/TC_DA_1_5.py index affb355d59f98a..6ee3c81ac370ad 100644 --- a/src/python_testing/TC_DA_1_5.py +++ b/src/python_testing/TC_DA_1_5.py @@ -41,8 +41,7 @@ from chip import ChipDeviceCtrl from chip.interaction_model import InteractionModelError, Status from chip.testing.conversions import hex_from_bytes -from chip.testing.matchers import type_matches -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, type_matches from chip.tlv import TLVReader from cryptography import x509 from cryptography.hazmat.primitives import hashes diff --git a/src/python_testing/TC_TIMESYNC_2_11.py b/src/python_testing/TC_TIMESYNC_2_11.py index 9f255679fb1313..e0b8f4dc2eabed 100644 --- a/src/python_testing/TC_TIMESYNC_2_11.py +++ b/src/python_testing/TC_TIMESYNC_2_11.py @@ -43,8 +43,7 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matchers import type_matches -from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main +from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main, type_matches from chip.testing.timeoperations import get_wait_seconds_from_set_time, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_12.py b/src/python_testing/TC_TIMESYNC_2_12.py index 7e459e6306bc43..7a1ff03e72cb3f 100644 --- a/src/python_testing/TC_TIMESYNC_2_12.py +++ b/src/python_testing/TC_TIMESYNC_2_12.py @@ -43,8 +43,7 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matchers import type_matches -from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main +from chip.testing.matter_testing import MatterBaseTest, SimpleEventCallback, async_test_body, default_matter_test_main, type_matches from chip.testing.timeoperations import get_wait_seconds_from_set_time, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_4.py b/src/python_testing/TC_TIMESYNC_2_4.py index b30de37d6ce6e1..a3b6bac020d54e 100644 --- a/src/python_testing/TC_TIMESYNC_2_4.py +++ b/src/python_testing/TC_TIMESYNC_2_4.py @@ -40,8 +40,7 @@ import chip.clusters as Clusters from chip.interaction_model import InteractionModelError, Status -from chip.testing.matchers import type_matches -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, type_matches from chip.testing.timeoperations import utc_time_in_matter_epoch from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_7.py b/src/python_testing/TC_TIMESYNC_2_7.py index 62a875b7579811..a164cd64809f05 100644 --- a/src/python_testing/TC_TIMESYNC_2_7.py +++ b/src/python_testing/TC_TIMESYNC_2_7.py @@ -42,8 +42,7 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matchers import type_matches -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, type_matches from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_8.py b/src/python_testing/TC_TIMESYNC_2_8.py index 47c2463bf5a37f..1a227b579547ef 100644 --- a/src/python_testing/TC_TIMESYNC_2_8.py +++ b/src/python_testing/TC_TIMESYNC_2_8.py @@ -42,8 +42,7 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matchers import type_matches -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, type_matches from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TC_TIMESYNC_2_9.py b/src/python_testing/TC_TIMESYNC_2_9.py index 437293e3003a7e..703e3b9d58e664 100644 --- a/src/python_testing/TC_TIMESYNC_2_9.py +++ b/src/python_testing/TC_TIMESYNC_2_9.py @@ -41,8 +41,7 @@ import chip.clusters as Clusters from chip.clusters.Types import NullValue from chip.interaction_model import InteractionModelError -from chip.testing.matchers import type_matches -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main +from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, type_matches from chip.testing.timeoperations import compare_time, utc_time_in_matter_epoch from chip.tlv import uint from mobly import asserts diff --git a/src/python_testing/TestMatterTestingSupport.py b/src/python_testing/TestMatterTestingSupport.py index ad490002313f7c..5632834a843ba3 100644 --- a/src/python_testing/TestMatterTestingSupport.py +++ b/src/python_testing/TestMatterTestingSupport.py @@ -22,8 +22,8 @@ import chip.clusters as Clusters from chip.clusters.Types import Nullable, NullValue -from chip.testing.matchers import type_matches -from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main, parse_matter_test_args +from chip.testing.matter_testing import (MatterBaseTest, async_test_body, default_matter_test_main, parse_matter_test_args, + type_matches) from chip.testing.pics import parse_pics, parse_pics_xml from chip.testing.taglist_and_topology_test import (TagProblem, create_device_type_list_for_root, create_device_type_lists, find_tag_list_problems, find_tree_roots, flat_list_ok, get_all_children, From 025cc486322af575b0b770c7dfbeffa2b9066710 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Fri, 7 Feb 2025 13:33:12 +0100 Subject: [PATCH 19/22] add doctest for new modules --- .../matter_testing_infrastructure/BUILD.gn | 3 ++ .../chip/testing/conversions.py | 44 ++++++++++++++--- .../chip/testing/matchers.py | 14 ++++++ .../chip/testing/timeoperations.py | 49 +++++++++++++++++-- 4 files changed, 98 insertions(+), 12 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/BUILD.gn b/src/python_testing/matter_testing_infrastructure/BUILD.gn index 6012235f4c664d..33c6ba93d59cdc 100644 --- a/src/python_testing/matter_testing_infrastructure/BUILD.gn +++ b/src/python_testing/matter_testing_infrastructure/BUILD.gn @@ -52,9 +52,12 @@ pw_python_package("chip-testing-module") { "chip/testing/timeoperations.py", ] tests = [ + "chip/testing/conversions.py", + "chip/testing/matchers.py", "chip/testing/test_metadata.py", "chip/testing/test_tasks.py", "chip/testing/test_matter_asserts.py", + "chip/testing/timeoperations.py", ] } diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py b/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py index 5cb127af61a773..20a5d760a1e68d 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/conversions.py @@ -30,8 +30,14 @@ def bytes_from_hex(hex: str) -> bytes: """ Converts hex string to bytes, handling various formats (colons, spaces, newlines). Examples: - "01:ab:cd" -> b'\x01\xab\xcd' - "01 ab cd" -> b'\x01\xab\xcd' + >>> bytes_from_hex("01:ab:cd") + b'\\x01\\xab\\xcd' + >>> bytes_from_hex("01 ab cd") + b'\\x01\\xab\\xcd' + >>> bytes_from_hex("01abcd") + b'\\x01\\xab\\xcd' + >>> bytes_from_hex("01\\nab\\ncd") + b'\\x01\\xab\\xcd' """ return unhexlify("".join(hex.replace(":", "").replace(" ", "").split())) @@ -49,7 +55,11 @@ def hex_from_bytes(b: bytes) -> str: str: A string containing the hexadecimal representation of the bytes, using lowercase letters a-f for hex digits - Example: b'\x01\xab\xcd' -> '01abcd' + Examples: + >>> hex_from_bytes(b'\\x01\\xab\\xcd') + '01abcd' + >>> hex_from_bytes(bytes([1, 171, 205])) # Same bytes, different notation + '01abcd' """ return hexlify(b).decode("utf-8") @@ -65,6 +75,14 @@ def format_decimal_and_hex(number): Returns: str: A formatted string like "123 (0x7b)" + + Examples: + >>> format_decimal_and_hex(123) + '123 (0x7b)' + >>> format_decimal_and_hex(0) + '0 (0x00)' + >>> format_decimal_and_hex(255) + '255 (0xff)' """ return f'{number} (0x{number:02x})' @@ -72,15 +90,22 @@ def format_decimal_and_hex(number): def cluster_id_with_name(id): """ Formats a Matter cluster ID with its name and numeric representation. - Uses id_str() for numeric formatting and looks up cluster name from registry. + Uses format_decimal_and_hex() for numeric formatting and looks up cluster name from registry. Falls back to "Unknown cluster" if ID not recognized. Args: id: int, the Matter cluster identifier Returns: - str: A formatted string containing the ID and cluster name, like - "6 (0x06) OnOff", or "Unknown cluster" if ID not recognized + str: A formatted string containing the ID and cluster name + + Examples: + >>> cluster_id_with_name(6) # OnOff cluster + '6 (0x06) OnOff' + >>> cluster_id_with_name(999999) # Unknown cluster + '999999 (0xf423f) Unknown cluster' + >>> cluster_id_with_name("invalid") # Invalid input + 'HERE IS THE PROBLEM' """ if id in Clusters.ClusterObjects.ALL_CLUSTERS.keys(): s = Clusters.ClusterObjects.ALL_CLUSTERS[id].__name__ @@ -88,5 +113,10 @@ def cluster_id_with_name(id): s = "Unknown cluster" try: return f'{format_decimal_and_hex(id)} {s}' - except TypeError: + except (TypeError, ValueError): return 'HERE IS THE PROBLEM' + + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py index c833b290d25a2e..46e53ddbd2dce0 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matchers.py @@ -39,6 +39,15 @@ def is_type(received_value, desired_type): Returns: bool: True if the received_value matches the desired_type specification + + Examples: + >>> is_type(42, int) + True + >>> from typing import Optional + >>> is_type(None, Optional[str]) + True + >>> is_type([1,2,3], list[int]) + True """ if typing.get_origin(desired_type) == typing.Union: return any(is_type(received_value, t) for t in typing.get_args(desired_type)) @@ -54,3 +63,8 @@ def is_type(received_value, desired_type): return isinstance(received_value, float) else: return isinstance(received_value, desired_type) + + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py b/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py index fbe366d35333e6..7255e2254917fe 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/timeoperations.py @@ -20,6 +20,10 @@ This module provides functionality for handling time-related operations in Matter testing, particularly focusing on conversions between different time formats and epochs. + +Examples: + To run the doctests, execute the module as a script: + $ python3 timeoperations.py """ from datetime import datetime, timedelta, timezone @@ -34,7 +38,7 @@ def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): """ Converts a datetime to microseconds since Matter epoch. The Matter epoch is defined as January 1, 2000, 00:00:00 UTC. This function - calculates the number of microseconds between the input datetime and the + calculates the number of microseconds beчtween the input datetime and the Matter epoch. If no datetime is provided, the current UTC time is used. Args: @@ -43,6 +47,13 @@ def utc_time_in_matter_epoch(desired_datetime: Optional[datetime] = None): Returns: int: Microseconds since Matter epoch + + Examples: + >>> from datetime import datetime, timezone + >>> utc_time_in_matter_epoch(datetime(2000, 1, 1, 0, 0, 0, 0, tzinfo=timezone.utc)) + 0 + >>> utc_time_in_matter_epoch(datetime(2000, 1, 1, 0, 0, 0, 1, tzinfo=timezone.utc)) + 1 """ if desired_datetime is None: utc_native = datetime.now(tz=timezone.utc) @@ -66,6 +77,12 @@ def utc_datetime_from_matter_epoch_us(matter_epoch_us: int) -> datetime: Returns: datetime: UTC datetime + + Examples: + >>> utc_datetime_from_matter_epoch_us(0) + datetime.datetime(2000, 1, 1, 0, 0, tzinfo=datetime.timezone.utc) + >>> utc_datetime_from_matter_epoch_us(123456789) + datetime.datetime(2000, 1, 1, 0, 2, 3, 456789, tzinfo=datetime.timezone.utc) """ delta_from_epoch = timedelta(microseconds=matter_epoch_us) matter_epoch = datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc) @@ -84,6 +101,12 @@ def utc_datetime_from_posix_time_ms(posix_time_ms: int) -> datetime: Returns: datetime: UTC datetime + + Examples: + >>> utc_datetime_from_posix_time_ms(0) + datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc) + >>> utc_datetime_from_posix_time_ms(1609459200000) # 2021-01-01 00:00:00 UTC + datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc) """ millis = posix_time_ms % 1000 seconds = posix_time_ms // 1000 @@ -102,6 +125,15 @@ def compare_time(received: int, offset: timedelta = timedelta(), utc: Optional[i Raises: AssertionError: If the received time differs from the expected time by more than the specified tolerance + + Examples: + >>> compare_time(5000000, utc=0, tolerance=timedelta(seconds=5)) # Passes (exactly 5s) + >>> from mobly import signals + >>> try: + ... compare_time(6000000, utc=0, tolerance=timedelta(seconds=5)) + ... except signals.TestFailure as e: + ... print("AssertionError: Received time is out of tolerance") + AssertionError: Received time is out of tolerance """ if utc is None: utc = utc_time_in_matter_epoch() @@ -128,10 +160,17 @@ def get_wait_seconds_from_set_time(set_time_matter_us: int, wait_seconds: int): int: Remaining seconds (negative if period expired) Examples: - >>> start_time = utc_time_in_matter_epoch() - >>> # After 2 seconds... - >>> get_wait_seconds_from_set_time(start_time, 5) - 3 # Returns remaining 3 seconds from original 5 second wait + >>> import unittest.mock + >>> start_time = utc_time_in_matter_epoch(datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc)) # 0 + >>> with unittest.mock.patch(__name__ + '.utc_time_in_matter_epoch') as mock_time: + ... mock_time.return_value = 2000000 # Simulate current time 2 seconds after start + ... get_wait_seconds_from_set_time(start_time, 5) # 5 - (2000000 / 1e6) = 5 - 2 = 3 + 3 """ seconds_passed = (utc_time_in_matter_epoch() - set_time_matter_us) // 1000000 return wait_seconds - seconds_passed + + +if __name__ == "__main__": + import doctest + doctest.testmod() From b29c46a6c57d7632dfb07209bdb0882b0d6b915b Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Fri, 7 Feb 2025 15:16:29 +0100 Subject: [PATCH 20/22] this should fix build error, doctests will run when the module is imported anyway --- src/python_testing/matter_testing_infrastructure/BUILD.gn | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/BUILD.gn b/src/python_testing/matter_testing_infrastructure/BUILD.gn index ade94f13fa4b6d..cc803ca9a44d60 100644 --- a/src/python_testing/matter_testing_infrastructure/BUILD.gn +++ b/src/python_testing/matter_testing_infrastructure/BUILD.gn @@ -53,12 +53,9 @@ pw_python_package("chip-testing-module") { "chip/testing/timeoperations.py", ] tests = [ - "chip/testing/conversions.py", - "chip/testing/matchers.py", "chip/testing/test_metadata.py", "chip/testing/test_tasks.py", "chip/testing/test_matter_asserts.py", - "chip/testing/timeoperations.py", ] } From 3b9525d89dd51caa5fb46e5472ff56eed828b6c0 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Wed, 12 Feb 2025 12:54:18 +0100 Subject: [PATCH 21/22] import modules as modules only; add ToDo to remove aliases in future --- .../chip/testing/matter_testing.py | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index 75b074ad92b953..d5f0416aa5983d 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -41,16 +41,7 @@ from itertools import chain from typing import Any, Iterable, List, Optional, Tuple -from chip.testing.conversions import bytes_from_hex as bytes_from_hex -from chip.testing.conversions import cluster_id_with_name as cluster_id_str -from chip.testing.conversions import format_decimal_and_hex as id_str -from chip.testing.conversions import hex_from_bytes as hex_from_bytes -from chip.testing.matchers import is_type as type_matches -from chip.testing.timeoperations import compare_time as compare_time -from chip.testing.timeoperations import get_wait_seconds_from_set_time as get_wait_seconds_from_set_time -from chip.testing.timeoperations import utc_datetime_from_matter_epoch_us as utc_datetime_from_matter_epoch_us -from chip.testing.timeoperations import utc_datetime_from_posix_time_ms as utc_datetime_from_posix_time_ms -from chip.testing.timeoperations import utc_time_in_matter_epoch as utc_time_in_matter_epoch +from chip.testing import conversions, matchers, timeoperations from chip.tlv import uint # isort: off @@ -2528,13 +2519,14 @@ def run_tests(test_class: MatterBaseTest, matter_test_config: MatterTestConfig, sys.exit(1) -type_matches = type_matches -utc_time_in_matter_epoch = utc_time_in_matter_epoch -utc_datetime_from_matter_epoch_us = utc_datetime_from_matter_epoch_us -utc_datetime_from_posix_time_ms = utc_datetime_from_posix_time_ms -compare_time = compare_time -get_wait_seconds_from_set_time = get_wait_seconds_from_set_time -bytes_from_hex = bytes_from_hex -hex_from_bytes = hex_from_bytes -id_str = id_str -cluster_id_str = cluster_id_str +# TODO(#37537): Remove these temporary aliases after transition period +type_matches = matchers.is_type +utc_time_in_matter_epoch = timeoperations.utc_time_in_matter_epoch +utc_datetime_from_matter_epoch_us = timeoperations.utc_datetime_from_matter_epoch_us +utc_datetime_from_posix_time_ms = timeoperations.utc_datetime_from_posix_time_ms +compare_time = timeoperations.compare_time +get_wait_seconds_from_set_time = timeoperations.get_wait_seconds_from_set_time +bytes_from_hex = conversions.bytes_from_hex +hex_from_bytes = conversions.hex_from_bytes +id_str = conversions.format_decimal_and_hex +cluster_id_str = conversions.cluster_id_str From 957957a1468566072e02c5b8731f1fca9d9d9a4e Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Wed, 12 Feb 2025 15:41:01 +0100 Subject: [PATCH 22/22] fix imports --- .../chip/testing/matter_testing.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py index d5f0416aa5983d..d47290a98c627f 100644 --- a/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py +++ b/src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py @@ -41,7 +41,9 @@ from itertools import chain from typing import Any, Iterable, List, Optional, Tuple -from chip.testing import conversions, matchers, timeoperations +import chip.testing.conversions as conversions +import chip.testing.matchers as matchers +import chip.testing.timeoperations as timeoperations from chip.tlv import uint # isort: off @@ -2529,4 +2531,4 @@ def run_tests(test_class: MatterBaseTest, matter_test_config: MatterTestConfig, bytes_from_hex = conversions.bytes_from_hex hex_from_bytes = conversions.hex_from_bytes id_str = conversions.format_decimal_and_hex -cluster_id_str = conversions.cluster_id_str +cluster_id_str = conversions.cluster_id_with_name