Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ALL_EVENTS maps in symmetry to ALL_ATTRIBUTES #35661

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
tcarmelveilleux marked this conversation as resolved.
Show resolved Hide resolved
"""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
Loading