Make creating background tasks in the client a bound method (#778)

This commit is contained in:
J. Nick Koston 2023-11-28 11:54:29 -06:00 committed by GitHub
parent d40acb1f85
commit b3a621f809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1259,9 +1259,7 @@ class APIClient:
# We hold a reference to the start_task in unsub function # We hold a reference to the start_task in unsub function
# so we don't need to add it to the background tasks. # so we don't need to add it to the background tasks.
else: else:
stop_task = asyncio.create_task(handle_stop()) self._create_background_task(handle_stop())
self._background_tasks.add(stop_task)
stop_task.add_done_callback(self._background_tasks.discard)
connection.send_message(SubscribeVoiceAssistantRequest(subscribe=True)) connection.send_message(SubscribeVoiceAssistantRequest(subscribe=True))
@ -1283,6 +1281,12 @@ class APIClient:
return unsub return unsub
def _create_background_task(self, coro: Coroutine[Any, Any, None]) -> None:
"""Create a background task and add it to the background tasks set."""
task = asyncio.create_task(coro)
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.discard)
def send_voice_assistant_event( def send_voice_assistant_event(
self, event_type: VoiceAssistantEventType, data: dict[str, str] | None self, event_type: VoiceAssistantEventType, data: dict[str, str] | None
) -> None: ) -> None: