Skip to content

Commit

Permalink
Merge pull request #38 from bdraco/adjustable_retries
Browse files Browse the repository at this point in the history
Make max attempts adjustable
  • Loading branch information
LaStrada authored May 5, 2024
2 parents ba6c1c9 + 5ffe345 commit a817ac0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion airthings_ble/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
RADON_MAX = 16383
TEMPERATURE_MAX = 100

MAX_UPDATE_ATTEMPTS = 3
DEFAULT_MAX_UPDATE_ATTEMPTS = 1
13 changes: 10 additions & 3 deletions airthings_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from bleak_retry_connector import BleakClientWithServiceCache, establish_connection

from .const import (
MAX_UPDATE_ATTEMPTS,
DEFAULT_MAX_UPDATE_ATTEMPTS,
BQ_TO_PCI_MULTIPLIER,
CHAR_UUID_DATETIME,
CHAR_UUID_DEVICE_NAME,
Expand Down Expand Up @@ -481,10 +481,17 @@ def __init__(
self,
logger: Logger,
is_metric: bool = True,
max_attempts: int = DEFAULT_MAX_UPDATE_ATTEMPTS,
) -> None:
"""Initialize the Airthings BLE sensor data object."""
self.logger = logger
self.is_metric = is_metric
self.device_info = AirthingsDeviceInfo()
self.max_attempts = max_attempts

def set_max_attempts(self, max_attempts: int) -> None:
"""Set the number of attempts."""
self.max_attempts = max_attempts

async def _get_device_characteristics(
self, client: BleakClient, device: AirthingsDevice
Expand Down Expand Up @@ -648,8 +655,8 @@ def _handle_disconnect(

async def update_device(self, ble_device: BLEDevice) -> AirthingsDevice:
"""Connects to the device through BLE and retrieves relevant data"""
for attempt in range(MAX_UPDATE_ATTEMPTS):
is_final_attempt = attempt == MAX_UPDATE_ATTEMPTS - 1
for attempt in range(self.max_attempts):
is_final_attempt = attempt == self.max_attempts - 1
try:
return await self._update_device(ble_device)
except DisconnectedError:
Expand Down

0 comments on commit a817ac0

Please sign in to comment.