30
30
import asyncio
31
31
from ctypes import *
32
32
from .ChipStack import *
33
- from .interaction_model import delegate as im
33
+ from .interaction_model import InteractionModelError , delegate as im
34
34
from .exceptions import *
35
35
from .clusters import Command as ClusterCommand
36
36
from .clusters import Attribute as ClusterAttribute
37
37
from .clusters import ClusterObjects as ClusterObjects
38
+ from .clusters import Objects as GeneratedObjects
38
39
from .clusters .CHIPClusters import *
39
40
import enum
40
41
import threading
@@ -376,7 +377,7 @@ async def SendCommand(self, nodeid: int, endpoint: int, payload: ClusterObjects.
376
377
future .set_exception (self ._ChipStack .ErrorToException (res ))
377
378
return await future
378
379
379
- def WriteAttribute (self , nodeid : int , attributes ):
380
+ async def WriteAttribute (self , nodeid : int , attributes ):
380
381
eventLoop = asyncio .get_running_loop ()
381
382
future = eventLoop .create_future ()
382
383
@@ -387,9 +388,9 @@ def WriteAttribute(self, nodeid: int, attributes):
387
388
)
388
389
if res != 0 :
389
390
raise self ._ChipStack .ErrorToException (res )
390
- return future
391
+ return await future
391
392
392
- def ReadAttribute (self , nodeid : int , attributes : typing .List [typing .Union [
393
+ async def ReadAttribute (self , nodeid : int , attributes : typing .List [typing .Union [
393
394
None , # Empty tuple, all wildcard
394
395
typing .Tuple [int ], # Endpoint
395
396
# Wildcard endpoint, Cluster id present
@@ -437,18 +438,21 @@ def ReadAttribute(self, nodeid: int, attributes: typing.List[typing.Union[
437
438
lambda : ClusterAttribute .ReadAttributes (future , eventLoop , device , attrs ))
438
439
if res != 0 :
439
440
raise self ._ChipStack .ErrorToException (res )
440
- return future
441
+ return await future
441
442
442
443
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 )
452
456
453
457
def ZCLReadAttribute (self , cluster , attribute , nodeid , endpoint , groupid , blocking = True ):
454
458
device = self .GetConnectedDeviceSync (nodeid )
@@ -460,13 +464,14 @@ def ZCLReadAttribute(self, cluster, attribute, nodeid, endpoint, groupid, blocki
460
464
return im .GetAttributeReadResponse (im .DEFAULT_ATTRIBUTEREAD_APPID )
461
465
462
466
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 ]))
470
475
471
476
def ZCLSubscribeAttribute (self , cluster , attribute , nodeid , endpoint , minInterval , maxInterval , blocking = True ):
472
477
device = self .GetConnectedDeviceSync (nodeid )
0 commit comments