Skip to content

Commit

Permalink
Rename AsyncSimpleCallableHandle to AsyncioCallableHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
agners committed May 2, 2024
1 parent 3fdbbbe commit 9da6eaa
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/controller/python/chip/ChipStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,20 @@ def Wait(self, timeoutMs: int = None):
return self._res


class AsyncSimpleCallableHandle:
class AsyncioCallableHandle:
"""Class which handles Matter SDK Calls asyncio friendly"""

def __init__(self, callback, loop, future):
def __init__(self, callback):
self._callback = callback
self._loop = loop
self._future = future
self._loop = asyncio.get_event_loop()
self._future = self._loop.create_future()
self._result = None
self._exception = None

@property
def future(self):
return self._future

def _done(self):
if self._exception:
self._future.set_exception(self._exception)
Expand Down Expand Up @@ -397,9 +401,7 @@ async def CallAsync(self, callFunct, timeoutMs: int = None):
'''Run a Python function on CHIP stack, and wait for the response.
This function will post a task on CHIP mainloop and waits for the call response in a asyncio friendly manner.
'''
loop = asyncio.get_event_loop()
future = loop.create_future()
callObj = AsyncSimpleCallableHandle(callFunct, loop, future)
callObj = AsyncioCallableHandle(callFunct)
pythonapi.Py_IncRef(py_object(callObj))

res = self._ChipStackLib.pychip_DeviceController_PostTaskOnChipThread(
Expand All @@ -409,7 +411,7 @@ async def CallAsync(self, callFunct, timeoutMs: int = None):
pythonapi.Py_DecRef(py_object(callObj))
raise res.to_exception()

return await asyncio.wait_for(future, timeoutMs / 1000 if timeoutMs else None)
return await asyncio.wait_for(callObj.future, timeoutMs / 1000 if timeoutMs else None)

def CallAsyncWithCallback(self, callFunct):
'''Run a Python function on CHIP stack, and wait for the application specific response.
Expand Down

0 comments on commit 9da6eaa

Please sign in to comment.