From 5c8370c506e1477981322ed520dbeb9b0252b48a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 28 Nov 2023 12:03:55 -0600 Subject: [PATCH] Use background task logic for the on_stop callback (#780) --- aioesphomeapi/client.py | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/aioesphomeapi/client.py b/aioesphomeapi/client.py index 2d18367..1e81ba7 100644 --- a/aioesphomeapi/client.py +++ b/aioesphomeapi/client.py @@ -206,7 +206,6 @@ class APIClient: "cached_name", "_background_tasks", "_loop", - "_on_stop_task", "log_name", ) @@ -253,7 +252,6 @@ class APIClient: self.cached_name: str | None = None self._background_tasks: set[asyncio.Task[Any]] = set() self._loop = asyncio.get_event_loop() - self._on_stop_task: asyncio.Task[None] | None = None self._set_log_name() def set_debug(self, enabled: bool) -> None: @@ -314,20 +312,7 @@ class APIClient: # Hook into on_stop handler to clear connection when stopped self._connection = None if on_stop: - self._on_stop_task = asyncio.create_task( - on_stop(expected_disconnect), - name=f"{self.log_name} aioesphomeapi on_stop", - ) - self._on_stop_task.add_done_callback(self._remove_on_stop_task) - - def _remove_on_stop_task(self, _fut: asyncio.Future[None]) -> None: - """Remove the stop task. - - We need to do this because the asyncio does not hold - a strong reference to the task, so it can be garbage - collected unexpectedly. - """ - self._on_stop_task = None + self._create_background_task(on_stop(expected_disconnect)) async def start_connection( self,