@@ -165,16 +165,20 @@ def Wait(self, timeoutMs: int = None):
165
165
return self ._res
166
166
167
167
168
- class AsyncSimpleCallableHandle :
168
+ class AsyncioCallableHandle :
169
169
"""Class which handles Matter SDK Calls asyncio friendly"""
170
170
171
- def __init__ (self , callback , loop , future ):
171
+ def __init__ (self , callback ):
172
172
self ._callback = callback
173
- self ._loop = loop
174
- self ._future = future
173
+ self ._loop = asyncio . get_event_loop ()
174
+ self ._future = self . _loop . create_future ()
175
175
self ._result = None
176
176
self ._exception = None
177
177
178
+ @property
179
+ def future (self ):
180
+ return self ._future
181
+
178
182
def _done (self ):
179
183
if self ._exception :
180
184
self ._future .set_exception (self ._exception )
@@ -397,9 +401,7 @@ async def CallAsync(self, callFunct, timeoutMs: int = None):
397
401
'''Run a Python function on CHIP stack, and wait for the response.
398
402
This function will post a task on CHIP mainloop and waits for the call response in a asyncio friendly manner.
399
403
'''
400
- loop = asyncio .get_event_loop ()
401
- future = loop .create_future ()
402
- callObj = AsyncSimpleCallableHandle (callFunct , loop , future )
404
+ callObj = AsyncioCallableHandle (callFunct )
403
405
pythonapi .Py_IncRef (py_object (callObj ))
404
406
405
407
res = self ._ChipStackLib .pychip_DeviceController_PostTaskOnChipThread (
@@ -409,7 +411,7 @@ async def CallAsync(self, callFunct, timeoutMs: int = None):
409
411
pythonapi .Py_DecRef (py_object (callObj ))
410
412
raise res .to_exception ()
411
413
412
- return await asyncio .wait_for (future , timeoutMs / 1000 if timeoutMs else None )
414
+ return await asyncio .wait_for (callObj . future , timeoutMs / 1000 if timeoutMs else None )
413
415
414
416
def CallAsyncWithCallback (self , callFunct ):
415
417
'''Run a Python function on CHIP stack, and wait for the application specific response.
0 commit comments