From af11dd210d578b6b15f983a4a532df7e065baf5a Mon Sep 17 00:00:00 2001 From: ol-iver Date: Sat, 3 Feb 2024 17:02:20 +0100 Subject: [PATCH] Save references to telnet callback tasks --- denonavr/api.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/denonavr/api.py b/denonavr/api.py index 69b5524..1b1c219 100644 --- a/denonavr/api.py +++ b/denonavr/api.py @@ -23,6 +23,7 @@ Hashable, List, Optional, + Set, Tuple, cast, ) @@ -435,6 +436,7 @@ class DenonAVRTelnetApi: default=attr.Factory(dict), init=False, ) + _callback_tasks: Set[asyncio.Task] = attr.ib(attr.Factory(set)) async def async_connect(self) -> None: """Connect to the receiver asynchronously.""" @@ -624,7 +626,9 @@ def _process_event(self, message: str) -> None: if event not in TELNET_EVENTS: return - asyncio.create_task(self._async_run_callbacks(event, zone, parameter)) + task = asyncio.create_task(self._async_run_callbacks(event, zone, parameter)) + self._callback_tasks.add(task) + task.add_done_callback(self._callback_tasks.discard) async def _async_run_callbacks(self, event: str, zone: str, parameter: str) -> None: """Handle triggering the registered callbacks for the event."""