From d58240660b0620b96a8c97bc78927c0e1f3799d3 Mon Sep 17 00:00:00 2001 From: Josef Zweck <24647999+zweckj@users.noreply.github.com> Date: Sat, 2 Dec 2023 17:50:13 +0100 Subject: [PATCH] split out task creation --- pyacaia_async/acaiascale.py | 15 +++++++++++---- setup.py | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pyacaia_async/acaiascale.py b/pyacaia_async/acaiascale.py index ef0cf24..824a74a 100644 --- a/pyacaia_async/acaiascale.py +++ b/pyacaia_async/acaiascale.py @@ -63,6 +63,7 @@ def __init__( self._queue: asyncio.Queue = asyncio.Queue() self._heartbeat_task: asyncio.Task | None = None self._process_queue_task: asyncio.Task | None = None + self._tasks_initialized = False self._msg_types["auth"] = encode_id(is_pyxis_style=is_new_style_scale) @@ -232,16 +233,22 @@ async def connect( except BleakDeviceNotFoundError as ex: raise AcaiaDeviceNotFound("Device not found") from ex + + self._setup_tasks() - if not self._heartbeat_task: - self._heartbeat_task = asyncio.create_task( + def _setup_tasks(self) -> None: + """Setup background tasks""": + if self._tasks_initialized: + return + + self._heartbeat_task = asyncio.create_task( self._send_heartbeats( interval=HEARTBEAT_INTERVAL if not self._is_new_style_scale else 1, new_style_heartbeat=self._is_new_style_scale, ) ) - if not self._process_queue_task: - self._process_queue_task = asyncio.create_task(self._process_queue()) + self._process_queue_task = asyncio.create_task(self._process_queue()) + self._tasks_initialized = True async def auth(self) -> None: """Send auth message to scale, if subscribed to notifications returns Settings object""" diff --git a/setup.py b/setup.py index aa8d795..ec42997 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="pyacaia_async", - version="0.0.11b7", + version="0.0.11b8", description="An async implementation of PyAcaia", long_description=readme, long_description_content_type="text/markdown",