Skip to content

Commit 7d6373f

Browse files
authored
Add an ALL_EVENTS maps in symmetry to ALL_ATTRIBUTES (#35661)
* 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
1 parent 14a98e9 commit 7d6373f

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

src/controller/python/chip/clusters/ClusterObjects.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def descriptor(cls):
214214
# These need to be separate because there can be overlap in command ids for commands and responses.
215215
ALL_ACCEPTED_COMMANDS: typing.Dict = {}
216216
ALL_GENERATED_COMMANDS: typing.Dict = {}
217+
ALL_EVENTS: typing.Dict = {}
217218

218219

219220
class ClusterCommand(ClusterObject):
@@ -261,12 +262,7 @@ def __init_subclass__(cls, *args, **kwargs) -> None:
261262
"""Register a subclass."""
262263
super().__init_subclass__(*args, **kwargs)
263264
# register this cluster in the ALL_CLUSTERS dict for quick lookups
264-
try:
265-
ALL_CLUSTERS[cls.id] = cls
266-
except NotImplementedError:
267-
# handle case where the Cluster class is not (fully) subclassed
268-
# and accessing the id property throws a NotImplementedError.
269-
pass
265+
ALL_CLUSTERS[cls.id] = cls
270266

271267
@property
272268
def data_version(self) -> int:
@@ -300,16 +296,11 @@ class ClusterAttributeDescriptor:
300296
def __init_subclass__(cls, *args, **kwargs) -> None:
301297
"""Register a subclass."""
302298
super().__init_subclass__(*args, **kwargs)
303-
try:
304-
if cls.standard_attribute:
305-
if cls.cluster_id not in ALL_ATTRIBUTES:
306-
ALL_ATTRIBUTES[cls.cluster_id] = {}
307-
# register this clusterattribute in the ALL_ATTRIBUTES dict for quick lookups
308-
ALL_ATTRIBUTES[cls.cluster_id][cls.attribute_id] = cls
309-
except NotImplementedError:
310-
# handle case where the ClusterAttribute class is not (fully) subclassed
311-
# and accessing the id property throws a NotImplementedError.
312-
pass
299+
if cls.standard_attribute:
300+
if cls.cluster_id not in ALL_ATTRIBUTES:
301+
ALL_ATTRIBUTES[cls.cluster_id] = {}
302+
# register this clusterattribute in the ALL_ATTRIBUTES dict for quick lookups
303+
ALL_ATTRIBUTES[cls.cluster_id][cls.attribute_id] = cls
313304

314305
@classmethod
315306
def ToTLV(cls, tag: Union[int, None], value):
@@ -369,6 +360,15 @@ def _cluster_object(cls) -> ClusterObject:
369360

370361

371362
class ClusterEvent(ClusterObject):
363+
def __init_subclass__(cls, *args, **kwargs) -> None:
364+
"""Register a subclass."""
365+
super().__init_subclass__(*args, **kwargs)
366+
367+
if cls.cluster_id not in ALL_EVENTS:
368+
ALL_EVENTS[cls.cluster_id] = {}
369+
# register this clusterattribute in the ALL_ATTRIBUTES dict for quick lookups
370+
ALL_EVENTS[cls.cluster_id][cls.event_id] = cls
371+
372372
@ChipUtility.classproperty
373373
def cluster_id(self) -> int:
374374
raise NotImplementedError()

src/controller/python/test/unit_tests/test_cluster_objects.py

+24
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ class IntAttribute(ClusterObjects.ClusterAttributeDescriptor):
193193
def attribute_type(cls) -> ClusterObjects.ClusterObjectFieldDescriptor:
194194
return ClusterObjects.ClusterObjectFieldDescriptor(Type=int)
195195

196+
@chip.ChipUtility.classproperty
197+
def cluster_id(cls) -> int:
198+
return 0x00000000
199+
200+
@chip.ChipUtility.classproperty
201+
def attribute_id(cls) -> int:
202+
return 0x00000000
203+
196204
def test_basic_encode(self):
197205
res = _encode_attribute_and_then_decode_to_native(
198206
42, TestAttributeDescriptor.IntAttribute)
@@ -208,6 +216,14 @@ class StructAttribute(ClusterObjects.ClusterAttributeDescriptor):
208216
def attribute_type(cls) -> ClusterObjects.ClusterObjectFieldDescriptor:
209217
return ClusterObjects.ClusterObjectFieldDescriptor(Type=TestClusterObjects.C)
210218

219+
@chip.ChipUtility.classproperty
220+
def cluster_id(cls) -> int:
221+
return 0x00000000
222+
223+
@chip.ChipUtility.classproperty
224+
def attribute_id(cls) -> int:
225+
return 0x00000000
226+
211227
def test_struct_encode(self):
212228
res = _encode_attribute_and_then_decode_to_native(
213229
TestClusterObjects.C(X=42, Y=24), TestAttributeDescriptor.StructAttribute)
@@ -223,6 +239,14 @@ class ArrayAttribute(ClusterObjects.ClusterAttributeDescriptor):
223239
def attribute_type(cls) -> ClusterObjects.ClusterObjectFieldDescriptor:
224240
return ClusterObjects.ClusterObjectFieldDescriptor(Type=typing.List[int])
225241

242+
@chip.ChipUtility.classproperty
243+
def cluster_id(cls) -> int:
244+
return 0x00000000
245+
246+
@chip.ChipUtility.classproperty
247+
def attribute_id(cls) -> int:
248+
return 0x00000000
249+
226250
def test_array_encode(self):
227251
res = _encode_attribute_and_then_decode_to_native(
228252
[1, 2, 3, 4, 5], TestAttributeDescriptor.ArrayAttribute)

0 commit comments

Comments
 (0)