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

fix: add retries to polling functionality #43

Merged
merged 4 commits into from
Mar 21, 2023
Merged
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
46 changes: 30 additions & 16 deletions src/oralb_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
from dataclasses import dataclass
from enum import Enum, auto

from bleak import BLEDevice
from bleak_retry_connector import BleakClientWithServiceCache, establish_connection
from bleak import BleakError, BLEDevice
from bleak_retry_connector import (
BleakClientWithServiceCache,
establish_connection,
retry_bluetooth_connection_error,
)
from bluetooth_data_tools import short_address
from bluetooth_sensor_state_data import BluetoothData
from home_assistant_bluetooth import BluetoothServiceInfo
Expand Down Expand Up @@ -329,20 +333,13 @@ def poll_needed(
update_interval = BRUSHING_UPDATE_INTERVAL_SECONDS
return last_poll > update_interval

async def async_poll(self, ble_device: BLEDevice) -> SensorUpdate:
"""
Poll the device to retrieve any values we can't get from passive listening.
"""
client = await establish_connection(
BleakClientWithServiceCache, ble_device, ble_device.address
)
try:
battery_char = client.services.get_characteristic(CHARACTERISTIC_BATTERY)
battery_payload = await client.read_gatt_char(battery_char)
pressure_char = client.services.get_characteristic(CHARACTERISTIC_PRESSURE)
pressure_payload = await client.read_gatt_char(pressure_char)
finally:
await client.disconnect()
@retry_bluetooth_connection_error()
async def _get_payload(self, client: BleakClientWithServiceCache) -> None:
"""Get the payload from the brush using its gatt_characteristics."""
battery_char = client.services.get_characteristic(CHARACTERISTIC_BATTERY)
battery_payload = await client.read_gatt_char(battery_char)
pressure_char = client.services.get_characteristic(CHARACTERISTIC_PRESSURE)
pressure_payload = await client.read_gatt_char(pressure_char)
tb_pressure = ACTIVE_CONNECTION_PRESSURE.get(
pressure_payload[0], f"unknown pressure {pressure_payload[0]}"
)
Expand All @@ -356,4 +353,21 @@ async def async_poll(self, ble_device: BLEDevice) -> SensorUpdate:
SensorDeviceClass.BATTERY,
"Battery",
)
_LOGGER.debug("Successfully read active gatt characters")

async def async_poll(self, ble_device: BLEDevice) -> SensorUpdate:
"""
Poll the device to retrieve any values we can't get from passive listening.
"""
_LOGGER.debug("Polling Oral-B device: %s", ble_device.address)
client = await establish_connection(
BleakClientWithServiceCache, ble_device, ble_device.address
)
try:
await self._get_payload(client)
except BleakError as err:
_LOGGER.warning(f"Reading gatt characters failed with err: {err}")
finally:
await client.disconnect()
_LOGGER.debug("Disconnected from active bluetooth client")
return self._finish_update()