Skip to content

Commit

Permalink
wait time before reconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
zweckj committed Dec 7, 2023
1 parent 59e018d commit 077df0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions pyacaia_async/acaiascale.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def timer(self) -> int:

def _device_disconnected_callback(self, client: BleakClient) -> None:
"""Callback for device disconnected."""
_LOGGER.debug("Device disconnected")
_LOGGER.warning("Scale with MAC %s disconnected", self.mac)
self._connected = False
self._last_disconnect_time = time.time()
if self._notify_callback:
Expand All @@ -165,7 +165,7 @@ async def _write_msg(self, char_id: str, payload: bytearray) -> None:
if not self._connected:
return
assert self._client
_LOGGER.debug("Writing to device...")
_LOGGER.debug("Writing to scale %s", self.mac)
await self._client.write_gatt_char(char_id, payload)
self._timestamp_last_command = time.time()
except (BleakDeviceNotFoundError, BleakError, TimeoutError) as ex:
Expand Down Expand Up @@ -216,6 +216,15 @@ async def connect(
raise AcaiaError("Client not initialized")
if self.connected:
return
if self._last_disconnect_time and self._last_disconnect_time > (
time.time() - 15
):
_LOGGER.debug(
"Scale has recently been disconnected, waiting 15 seconds before reconnecting",
self.mac,
)
return

try:
try:
await self._client.connect()
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.11b10",
version="0.0.11b11",
description="An async implementation of PyAcaia",
long_description=readme,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 077df0e

Please sign in to comment.