diff --git a/aioshelly/block_device/device.py b/aioshelly/block_device/device.py index 41fdcdf9..0809c779 100644 --- a/aioshelly/block_device/device.py +++ b/aioshelly/block_device/device.py @@ -88,6 +88,7 @@ def __init__( self.initialized = False self._initializing = False self._last_error: ShellyError | None = None + self._init_task: asyncio.Task[None] | None = None @classmethod async def create( @@ -189,7 +190,12 @@ def _coap_message_received(self, msg: CoapMessage) -> None: """COAP message received.""" if not self._initializing and not self.initialized: loop = asyncio.get_running_loop() - loop.create_task(self._async_init()) + self._init_task = loop.create_task(self._async_init()) + + def _clear_init_task(_: Any) -> None: + self._init_task = None + + self._init_task.add_done_callback(_clear_init_task) if not msg.payload: return diff --git a/aioshelly/rpc_device/device.py b/aioshelly/rpc_device/device.py index 5f507966..84837ba2 100644 --- a/aioshelly/rpc_device/device.py +++ b/aioshelly/rpc_device/device.py @@ -84,6 +84,7 @@ def __init__( self.initialized: bool = False self._initializing: bool = False self._last_error: ShellyError | None = None + self._init_task: asyncio.Task[None] | None = None @classmethod async def create( @@ -127,7 +128,12 @@ def _on_notification( if not self._initializing and not self.initialized: loop = asyncio.get_running_loop() - loop.create_task(self._async_init()) + self._init_task = loop.create_task(self._async_init()) + + def _clear_init_task(_: Any) -> None: + self._init_task = None + + self._init_task.add_done_callback(_clear_init_task) return if self._update_listener and self.initialized: