|
| 1 | +From ad37e367f4989efb5cd8668e3a086101385d2c2a Mon Sep 17 00:00:00 2001 |
| 2 | +From: Marcel van der Veldt <m.vanderveldt@outlook.com> |
| 3 | +Date: Mon, 20 Feb 2023 19:17:39 +0100 |
| 4 | +Subject: [PATCH] Add lookup dicts for clusters and attributes |
| 5 | + |
| 6 | +--- |
| 7 | + .../python/chip/clusters/ClusterObjects.py | 20 +++++++++++++++++++ |
| 8 | + 1 file changed, 20 insertions(+) |
| 9 | + |
| 10 | +diff --git a/src/controller/python/chip/clusters/ClusterObjects.py b/src/controller/python/chip/clusters/ClusterObjects.py |
| 11 | +index c3af537da..2d3a73a10 100644 |
| 12 | +--- a/src/controller/python/chip/clusters/ClusterObjects.py |
| 13 | ++++ b/src/controller/python/chip/clusters/ClusterObjects.py |
| 14 | +@@ -217,6 +217,10 @@ class ClusterCommand(ClusterObject): |
| 15 | + def must_use_timed_invoke(cls) -> bool: |
| 16 | + return False |
| 17 | + |
| 18 | ++# The below dictionaries will be filled dynamically |
| 19 | ++# and are used for quick lookup/mapping from cluster/attribute id to the correct class |
| 20 | ++ALL_CLUSTERS = {} |
| 21 | ++ALL_ATTRIBUTES = {} |
| 22 | + |
| 23 | + class Cluster(ClusterObject): |
| 24 | + ''' |
| 25 | +@@ -227,6 +231,13 @@ class Cluster(ClusterObject): |
| 26 | + especially the TLV decoding logic. Also ThreadNetworkDiagnostics has an attribute with the same name so we |
| 27 | + picked data_version as its name. |
| 28 | + ''' |
| 29 | ++ |
| 30 | ++ def __init_subclass__(cls, *args, **kwargs) -> None: |
| 31 | ++ """Register a subclass.""" |
| 32 | ++ super().__init_subclass__(*args, **kwargs) |
| 33 | ++ # register this cluster in the ALL_CLUSTERS dict for quick lookups |
| 34 | ++ ALL_CLUSTERS[cls.id] = cls |
| 35 | ++ |
| 36 | + @property |
| 37 | + def data_version(self) -> int: |
| 38 | + return self._data_version |
| 39 | +@@ -253,6 +264,15 @@ class ClusterAttributeDescriptor: |
| 40 | + |
| 41 | + The implementation of this functions is quite tricky, it will create a cluster object on-the-fly, and use it for actual encode / decode routine to save lines of code. |
| 42 | + ''' |
| 43 | ++ |
| 44 | ++ def __init_subclass__(cls, *args, **kwargs) -> None: |
| 45 | ++ """Register a subclass.""" |
| 46 | ++ super().__init_subclass__(*args, **kwargs) |
| 47 | ++ if cls.cluster_id not in ALL_ATTRIBUTES: |
| 48 | ++ ALL_ATTRIBUTES[cls.cluster_id] = {} |
| 49 | ++ # register this clusterattribute in the ALL_ATTRIBUTES dict for quick lookups |
| 50 | ++ ALL_ATTRIBUTES[cls.cluster_id][cls.attribute_id] = cls |
| 51 | ++ |
| 52 | + @classmethod |
| 53 | + def ToTLV(cls, tag: Union[int, None], value): |
| 54 | + writer = tlv.TLVWriter() |
| 55 | +-- |
| 56 | +2.37.0 (Apple Git-136) |
| 57 | + |
0 commit comments