Skip to content

Commit

Permalink
move things to pyacaia
Browse files Browse the repository at this point in the history
  • Loading branch information
zweckj committed Nov 22, 2023
1 parent 0aebe16 commit b82c7e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
27 changes: 17 additions & 10 deletions pyacaia_async/acaiascale.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def __init__(
self._data: dict[str, Any] = {BATTERY_LEVEL: None, UNITS: GRAMS, WEIGHT: 0.0}

self._queue: asyncio.Queue = asyncio.Queue()
self._heartbeat_task: asyncio.Task | None = None
self._process_queue_task: asyncio.Task | None = None

self._msg_types["auth"] = encode_id(is_pyxis_style=is_new_style_scale)

Expand Down Expand Up @@ -120,16 +122,7 @@ async def create(
else:
raise ValueError("Either mac or bleDevice must be specified")

if callback is None:
callback = self.on_bluetooth_data_received
await self.connect(callback)
asyncio.create_task(
self._send_heartbeats(
interval=HEARTBEAT_INTERVAL if not is_new_style_scale else 1,
new_style_heartbeat=is_new_style_scale,
)
)
asyncio.create_task(self._process_queue())
return self

@property
Expand Down Expand Up @@ -214,7 +207,7 @@ async def connect(
self._connected = True
_LOGGER.debug("Connected to Acaia Scale")

if callback is None:
if callback is None:
callback = self.on_bluetooth_data_received
try:
await self._client.start_notify(self._notify_char_id, callback)
Expand All @@ -231,6 +224,14 @@ async def connect(
except BleakDeviceNotFoundError as ex:
raise AcaiaDeviceNotFound("Device not found") from ex

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,
)
)
asyncio.create_task(self._process_queue())

async def auth(self) -> None:
"""Send auth message to scale, if subscribed to notifications returns Settings object"""
await self._queue.put((self._default_char_id, self.msg_types["auth"]))
Expand Down Expand Up @@ -288,10 +289,14 @@ async def disconnect(self) -> None:

async def tare(self) -> None:
"""Tare the scale."""
if not self.connected:
await self.connect()
await self._queue.put((self._default_char_id, self.msg_types["tare"]))

async def start_stop_timer(self) -> None:
"""Start/Stop the timer."""
if not self.connected:
await self.connect()
if not self._timer_running:
_LOGGER.debug('Sending "start" message.')
await self._queue.put((self._default_char_id, self.msg_types["startTimer"]))
Expand All @@ -306,6 +311,8 @@ async def start_stop_timer(self) -> None:

async def reset_timer(self) -> None:
"""Reset the timer."""
if not self.connected:
await self.connect()
await self._queue.put((self._default_char_id, self.msg_types["resetTimer"]))
self._timer_start = None
self._timer_stop = None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pyacaia_async",
version="0.0.11b3",
version="0.0.11b4",
description="An async implementation of PyAcaia",
long_description=readme,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit b82c7e0

Please sign in to comment.