Skip to content

Commit

Permalink
Add an ALL_EVENTS maps in symmetry to ALL_ATTRIBUTES (project-chip#35661
Browse files Browse the repository at this point in the history
)

* add an ALL_EVENTS

* But do it correctly

* Remove try: except: and let badly subclassed items fail

* Fix unit tests to instantiate the classes properly
  • Loading branch information
cecille authored and FergusHuang committed Oct 20, 2024
1 parent cf1dfaa commit 578bb73
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/controller/python/chip/clusters/ClusterObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions src/controller/python/test/unit_tests/test_cluster_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 578bb73

Please sign in to comment.