Skip to content

Commit 2760231

Browse files
authored
[Python] Improve FindCommandClusterObject helper function (project-chip#41975)
1 parent 7619c0b commit 2760231

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ def cluster_id(self) -> int:
249249
def command_id(self) -> int:
250250
raise NotImplementedError()
251251

252+
@ChipUtility.classproperty
253+
def is_client(self) -> bool:
254+
raise NotImplementedError()
255+
252256
@ChipUtility.classproperty
253257
def must_use_timed_invoke(cls) -> bool:
254258
return False

src/controller/python/matter/clusters/Command.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import ctypes
2020
import inspect
2121
import logging
22-
import sys
2322
from asyncio.futures import Future
2423
from ctypes import CFUNCTYPE, POINTER, c_bool, c_char_p, c_size_t, c_uint8, c_uint16, c_uint32, c_void_p, cast, py_object
2524
from dataclasses import dataclass
@@ -29,8 +28,8 @@
2928
from ..interaction_model import Status as InteractionModelStatus
3029
from ..interaction_model import TestOnlyPyBatchCommandsOverrides, TestOnlyPyOnDoneInfo
3130
from ..native import GetLibraryHandle, NativeLibraryHandleMethodArguments, PyChipError
32-
from . import Objects as GeneratedObjects # noqa: F401
33-
from .ClusterObjects import ClusterCommand
31+
from . import Objects as GeneratedObjects
32+
from .ClusterObjects import Cluster, ClusterCommand
3433

3534
logger = logging.getLogger('matter.cluster.Command')
3635
logger.setLevel(logging.ERROR)
@@ -70,17 +69,12 @@ def FindCommandClusterObject(isClientSideCommand: bool, path: CommandPath):
7069
7170
Returns the type of the cluster object if one is found. Otherwise, returns None.
7271
'''
73-
for clusterName, obj in inspect.getmembers(sys.modules['matter.clusters.Objects']):
74-
if ('matter.clusters.Objects' in str(obj)) and inspect.isclass(obj):
75-
for objName, subclass in inspect.getmembers(obj):
76-
if inspect.isclass(subclass) and (('Commands') in str(subclass)):
77-
for commandName, command in inspect.getmembers(subclass):
78-
if inspect.isclass(command):
79-
for name, field in inspect.getmembers(command):
80-
if ('__dataclass_fields__' in name):
81-
if (field['cluster_id'].default == path.ClusterId) and (field['command_id'].default ==
82-
path.CommandId) and (field['is_client'].default == isClientSideCommand):
83-
return eval('GeneratedObjects.' + clusterName + '.Commands.' + commandName)
72+
for clusterName in GeneratedObjects.__all__:
73+
obj = getattr(GeneratedObjects, clusterName)
74+
if issubclass(obj, Cluster) and obj.id == path.ClusterId:
75+
for _, cmd in inspect.getmembers(getattr(obj, "Commands", None), inspect.isclass):
76+
if issubclass(cmd, ClusterCommand) and cmd.command_id == path.CommandId and cmd.is_client == isClientSideCommand:
77+
return cmd
8478
return None
8579

8680

0 commit comments

Comments
 (0)