Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make max attempts adjustable #38

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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