Skip to content

Commit

Permalink
Support coroutines for EventUpdateCallback
Browse files Browse the repository at this point in the history
This change posts the attribute update callback onto the callee Thread.

Note: Unlike the AttributeUpdateCallback this is directly triggered from
the handleEventData callback from the SDK instead of being called on
handleDone (OnReadDoneCallback). It is not clear to me why there is a
difference between the two.
  • Loading branch information
agners committed Mar 8, 2024
1 parent 7a45132 commit 87eecca
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/controller/python/chip/clusters/Attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def SetAttributeUpdateCallback(
if callback is not None:
self._onAttributeChangeCb = callback

def SetEventUpdateCallback(self, callback: Callable[[EventReadResult, SubscriptionTransaction], None]):
def SetEventUpdateCallback(self, callback: Callable[[EventReadResult, SubscriptionTransaction], Union[None, Awaitable[None]]]):
if callback is not None:
self._onEventChangeCb = callback

Expand All @@ -570,7 +570,7 @@ def OnAttributeChangeCb(self) -> Callable[[TypedAttributePath, SubscriptionTrans
return self._onAttributeChangeCb

@property
def OnEventChangeCb(self) -> Callable[[EventReadResult, SubscriptionTransaction], None]:
def OnEventChangeCb(self) -> Callable[[EventReadResult, SubscriptionTransaction], Union[None, Awaitable[None]]]:
return self._onEventChangeCb

@property
Expand Down Expand Up @@ -724,9 +724,14 @@ def handleEventData(self, header: EventHeader, path: EventPath, data: bytes, sta
Header=header, Data=eventValue, Status=chip.interaction_model.Status(status))
self._events.append(eventResult)

if (self._subscription_handler is not None):
self._subscription_handler.OnEventChangeCb(
eventResult, self._subscription_handler)
if self._subscription_handler is not None:
callback = self._subscription_handler.OnEventChangeCb
if inspect.iscoroutinefunction(callback):
asyncio.run_coroutine_threadsafe(callback(), self._event_loop)
else:
self._event_loop.call_soon_threadsafe(
self._subscription_handler.OnEventChangeCb,
eventResult, self._subscription_handler)

except Exception as ex:
logging.exception(ex)
Expand Down

0 comments on commit 87eecca

Please sign in to comment.