diff --git a/src/controller/python/chip/clusters/ClusterObjects.py b/src/controller/python/chip/clusters/ClusterObjects.py index ddafe87bf2c89d..beb490c631ab50 100644 --- a/src/controller/python/chip/clusters/ClusterObjects.py +++ b/src/controller/python/chip/clusters/ClusterObjects.py @@ -214,6 +214,7 @@ def descriptor(cls): # These need to be separate because there can be overlap in command ids for commands and responses. ALL_ACCEPTED_COMMANDS: typing.Dict = {} ALL_GENERATED_COMMANDS: typing.Dict = {} +ALL_EVENTS: typing.Dict = {} class ClusterCommand(ClusterObject): @@ -261,12 +262,7 @@ def __init_subclass__(cls, *args, **kwargs) -> None: """Register a subclass.""" super().__init_subclass__(*args, **kwargs) # register this cluster in the ALL_CLUSTERS dict for quick lookups - try: - ALL_CLUSTERS[cls.id] = cls - except NotImplementedError: - # handle case where the Cluster class is not (fully) subclassed - # and accessing the id property throws a NotImplementedError. - pass + ALL_CLUSTERS[cls.id] = cls @property def data_version(self) -> int: @@ -300,16 +296,11 @@ class ClusterAttributeDescriptor: def __init_subclass__(cls, *args, **kwargs) -> None: """Register a subclass.""" super().__init_subclass__(*args, **kwargs) - try: - if cls.standard_attribute: - if cls.cluster_id not in ALL_ATTRIBUTES: - ALL_ATTRIBUTES[cls.cluster_id] = {} - # register this clusterattribute in the ALL_ATTRIBUTES dict for quick lookups - ALL_ATTRIBUTES[cls.cluster_id][cls.attribute_id] = cls - except NotImplementedError: - # handle case where the ClusterAttribute class is not (fully) subclassed - # and accessing the id property throws a NotImplementedError. - pass + if cls.standard_attribute: + if cls.cluster_id not in ALL_ATTRIBUTES: + ALL_ATTRIBUTES[cls.cluster_id] = {} + # register this clusterattribute in the ALL_ATTRIBUTES dict for quick lookups + ALL_ATTRIBUTES[cls.cluster_id][cls.attribute_id] = cls @classmethod def ToTLV(cls, tag: Union[int, None], value): @@ -369,6 +360,15 @@ def _cluster_object(cls) -> ClusterObject: class ClusterEvent(ClusterObject): + def __init_subclass__(cls, *args, **kwargs) -> None: + """Register a subclass.""" + super().__init_subclass__(*args, **kwargs) + + if cls.cluster_id not in ALL_EVENTS: + ALL_EVENTS[cls.cluster_id] = {} + # register this clusterattribute in the ALL_ATTRIBUTES dict for quick lookups + ALL_EVENTS[cls.cluster_id][cls.event_id] = cls + @ChipUtility.classproperty def cluster_id(self) -> int: raise NotImplementedError() diff --git a/src/controller/python/test/unit_tests/test_cluster_objects.py b/src/controller/python/test/unit_tests/test_cluster_objects.py index 13b12db185270e..beca5ff39863d9 100644 --- a/src/controller/python/test/unit_tests/test_cluster_objects.py +++ b/src/controller/python/test/unit_tests/test_cluster_objects.py @@ -193,6 +193,14 @@ class IntAttribute(ClusterObjects.ClusterAttributeDescriptor): def attribute_type(cls) -> ClusterObjects.ClusterObjectFieldDescriptor: return ClusterObjects.ClusterObjectFieldDescriptor(Type=int) + @chip.ChipUtility.classproperty + def cluster_id(cls) -> int: + return 0x00000000 + + @chip.ChipUtility.classproperty + def attribute_id(cls) -> int: + return 0x00000000 + def test_basic_encode(self): res = _encode_attribute_and_then_decode_to_native( 42, TestAttributeDescriptor.IntAttribute) @@ -208,6 +216,14 @@ class StructAttribute(ClusterObjects.ClusterAttributeDescriptor): def attribute_type(cls) -> ClusterObjects.ClusterObjectFieldDescriptor: return ClusterObjects.ClusterObjectFieldDescriptor(Type=TestClusterObjects.C) + @chip.ChipUtility.classproperty + def cluster_id(cls) -> int: + return 0x00000000 + + @chip.ChipUtility.classproperty + def attribute_id(cls) -> int: + return 0x00000000 + def test_struct_encode(self): res = _encode_attribute_and_then_decode_to_native( TestClusterObjects.C(X=42, Y=24), TestAttributeDescriptor.StructAttribute) @@ -223,6 +239,14 @@ class ArrayAttribute(ClusterObjects.ClusterAttributeDescriptor): def attribute_type(cls) -> ClusterObjects.ClusterObjectFieldDescriptor: return ClusterObjects.ClusterObjectFieldDescriptor(Type=typing.List[int]) + @chip.ChipUtility.classproperty + def cluster_id(cls) -> int: + return 0x00000000 + + @chip.ChipUtility.classproperty + def attribute_id(cls) -> int: + return 0x00000000 + def test_array_encode(self): res = _encode_attribute_and_then_decode_to_native( [1, 2, 3, 4, 5], TestAttributeDescriptor.ArrayAttribute)