Skip to content

Commit 1163704

Browse files
erjiaqingpull[bot]
authored andcommitted
[Python] Use Generated Cluster Objects instead of functions for ZCLSend / ZCLWriteAttribute (#11961)
* [Python] Use Generated Cluster Objects instead of functions for ZCLSend / ZCLWriteAttribute * Run Codegen
1 parent f67adcf commit 1163704

File tree

6 files changed

+150
-5018
lines changed

6 files changed

+150
-5018
lines changed

src/controller/python/chip/ChipDeviceCtrl.py

+26-21
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
import asyncio
3131
from ctypes import *
3232
from .ChipStack import *
33-
from .interaction_model import delegate as im
33+
from .interaction_model import InteractionModelError, delegate as im
3434
from .exceptions import *
3535
from .clusters import Command as ClusterCommand
3636
from .clusters import Attribute as ClusterAttribute
3737
from .clusters import ClusterObjects as ClusterObjects
38+
from .clusters import Objects as GeneratedObjects
3839
from .clusters.CHIPClusters import *
3940
import enum
4041
import threading
@@ -376,7 +377,7 @@ async def SendCommand(self, nodeid: int, endpoint: int, payload: ClusterObjects.
376377
future.set_exception(self._ChipStack.ErrorToException(res))
377378
return await future
378379

379-
def WriteAttribute(self, nodeid: int, attributes):
380+
async def WriteAttribute(self, nodeid: int, attributes):
380381
eventLoop = asyncio.get_running_loop()
381382
future = eventLoop.create_future()
382383

@@ -387,9 +388,9 @@ def WriteAttribute(self, nodeid: int, attributes):
387388
)
388389
if res != 0:
389390
raise self._ChipStack.ErrorToException(res)
390-
return future
391+
return await future
391392

392-
def ReadAttribute(self, nodeid: int, attributes: typing.List[typing.Union[
393+
async def ReadAttribute(self, nodeid: int, attributes: typing.List[typing.Union[
393394
None, # Empty tuple, all wildcard
394395
typing.Tuple[int], # Endpoint
395396
# Wildcard endpoint, Cluster id present
@@ -437,18 +438,21 @@ def ReadAttribute(self, nodeid: int, attributes: typing.List[typing.Union[
437438
lambda: ClusterAttribute.ReadAttributes(future, eventLoop, device, attrs))
438439
if res != 0:
439440
raise self._ChipStack.ErrorToException(res)
440-
return future
441+
return await future
441442

442443
def ZCLSend(self, cluster, command, nodeid, endpoint, groupid, args, blocking=False):
443-
device = self.GetConnectedDeviceSync(nodeid)
444-
445-
im.ClearCommandStatus(im.PLACEHOLDER_COMMAND_HANDLE)
446-
self._Cluster.SendCommand(
447-
device, cluster, command, endpoint, groupid, args, True)
448-
if blocking:
449-
# We only send 1 command by this function, so index is always 0
450-
return im.WaitCommandIndexStatus(im.PLACEHOLDER_COMMAND_HANDLE, 1)
451-
return (0, None)
444+
req = None
445+
try:
446+
req = eval(
447+
f"GeneratedObjects.{cluster}.Commands.{command}")(**args)
448+
except:
449+
raise UnknownCommand(cluster, command)
450+
try:
451+
res = asyncio.run(self.SendCommand(nodeid, endpoint, req))
452+
print(f"CommandResponse {res}")
453+
return (0, res)
454+
except InteractionModelError as ex:
455+
return (int(ex.state), None)
452456

453457
def ZCLReadAttribute(self, cluster, attribute, nodeid, endpoint, groupid, blocking=True):
454458
device = self.GetConnectedDeviceSync(nodeid)
@@ -460,13 +464,14 @@ def ZCLReadAttribute(self, cluster, attribute, nodeid, endpoint, groupid, blocki
460464
return im.GetAttributeReadResponse(im.DEFAULT_ATTRIBUTEREAD_APPID)
461465

462466
def ZCLWriteAttribute(self, cluster, attribute, nodeid, endpoint, groupid, value, blocking=True):
463-
device = self.GetConnectedDeviceSync(nodeid)
464-
465-
# We are not using IM for Attributes.
466-
self._Cluster.WriteAttribute(
467-
device, cluster, attribute, endpoint, groupid, value, False)
468-
if blocking:
469-
return im.GetAttributeWriteResponse(im.DEFAULT_ATTRIBUTEWRITE_APPID)
467+
req = None
468+
try:
469+
req = ClusterAttribute.AttributeWriteRequest(EndpointId=endpoint, Attribute=eval(
470+
f"GeneratedObjects.{cluster}.Attributes.{attribute}"), Data=value)
471+
except:
472+
raise UnknownAttribute(cluster, attribute)
473+
474+
return asyncio.run(self.WriteAttribute(nodeid, [req]))
470475

471476
def ZCLSubscribeAttribute(self, cluster, attribute, nodeid, endpoint, minInterval, maxInterval, blocking=True):
472477
device = self.GetConnectedDeviceSync(nodeid)

0 commit comments

Comments
 (0)