Skip to content

Commit

Permalink
Save strong reference to create_task (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Feb 27, 2024
1 parent 4007cd5 commit 1978474
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion aioshelly/block_device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion aioshelly/rpc_device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 1978474

Please sign in to comment.