Skip to content

Commit

Permalink
[Python] Use Generated Cluster Objects instead of functions for ZCLSe…
Browse files Browse the repository at this point in the history
…nd / ZCLWriteAttribute (#11961)

* [Python] Use Generated Cluster Objects instead of functions for ZCLSend / ZCLWriteAttribute

* Run Codegen
  • Loading branch information
erjiaqing authored Nov 18, 2021
1 parent 2a7eaf0 commit 02d8a1a
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 5,018 deletions.
47 changes: 26 additions & 21 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
import asyncio
from ctypes import *
from .ChipStack import *
from .interaction_model import delegate as im
from .interaction_model import InteractionModelError, delegate as im
from .exceptions import *
from .clusters import Command as ClusterCommand
from .clusters import Attribute as ClusterAttribute
from .clusters import ClusterObjects as ClusterObjects
from .clusters import Objects as GeneratedObjects
from .clusters.CHIPClusters import *
import enum
import threading
Expand Down Expand Up @@ -376,7 +377,7 @@ async def SendCommand(self, nodeid: int, endpoint: int, payload: ClusterObjects.
future.set_exception(self._ChipStack.ErrorToException(res))
return await future

def WriteAttribute(self, nodeid: int, attributes):
async def WriteAttribute(self, nodeid: int, attributes):
eventLoop = asyncio.get_running_loop()
future = eventLoop.create_future()

Expand All @@ -387,9 +388,9 @@ def WriteAttribute(self, nodeid: int, attributes):
)
if res != 0:
raise self._ChipStack.ErrorToException(res)
return future
return await future

def ReadAttribute(self, nodeid: int, attributes: typing.List[typing.Union[
async def ReadAttribute(self, nodeid: int, attributes: typing.List[typing.Union[
None, # Empty tuple, all wildcard
typing.Tuple[int], # Endpoint
# Wildcard endpoint, Cluster id present
Expand Down Expand Up @@ -437,18 +438,21 @@ def ReadAttribute(self, nodeid: int, attributes: typing.List[typing.Union[
lambda: ClusterAttribute.ReadAttributes(future, eventLoop, device, attrs))
if res != 0:
raise self._ChipStack.ErrorToException(res)
return future
return await future

def ZCLSend(self, cluster, command, nodeid, endpoint, groupid, args, blocking=False):
device = self.GetConnectedDeviceSync(nodeid)

im.ClearCommandStatus(im.PLACEHOLDER_COMMAND_HANDLE)
self._Cluster.SendCommand(
device, cluster, command, endpoint, groupid, args, True)
if blocking:
# We only send 1 command by this function, so index is always 0
return im.WaitCommandIndexStatus(im.PLACEHOLDER_COMMAND_HANDLE, 1)
return (0, None)
req = None
try:
req = eval(
f"GeneratedObjects.{cluster}.Commands.{command}")(**args)
except:
raise UnknownCommand(cluster, command)
try:
res = asyncio.run(self.SendCommand(nodeid, endpoint, req))
print(f"CommandResponse {res}")
return (0, res)
except InteractionModelError as ex:
return (int(ex.state), None)

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

def ZCLWriteAttribute(self, cluster, attribute, nodeid, endpoint, groupid, value, blocking=True):
device = self.GetConnectedDeviceSync(nodeid)

# We are not using IM for Attributes.
self._Cluster.WriteAttribute(
device, cluster, attribute, endpoint, groupid, value, False)
if blocking:
return im.GetAttributeWriteResponse(im.DEFAULT_ATTRIBUTEWRITE_APPID)
req = None
try:
req = ClusterAttribute.AttributeWriteRequest(EndpointId=endpoint, Attribute=eval(
f"GeneratedObjects.{cluster}.Attributes.{attribute}"), Data=value)
except:
raise UnknownAttribute(cluster, attribute)

return asyncio.run(self.WriteAttribute(nodeid, [req]))

def ZCLSubscribeAttribute(self, cluster, attribute, nodeid, endpoint, minInterval, maxInterval, blocking=True):
device = self.GetConnectedDeviceSync(nodeid)
Expand Down
Loading

0 comments on commit 02d8a1a

Please sign in to comment.