Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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