Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install bleak
pip install bleak-retry-connector
pip install mypy==1.17.0
- name: Type check with mypy
run: |
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install bleak
pip install bleak-retry-connector
pip install pytest
- name: Test with pytest
run: |
Expand Down
69 changes: 35 additions & 34 deletions automower_ble/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import logging
import json
from importlib.resources import files
from bleak import BleakClient
from bleak.backends.characteristic import BleakGATTCharacteristic
from bleak_retry_connector import establish_connection, BleakClientWithServiceCache
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from bleak import BleakClient

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -345,33 +349,28 @@ async def _read_data(self):
return data

async def _request_response(self, request_data):
i = 5
async with self.lock:
while i > 0:
try:
# If there are previous responses, flush them out
while not self.queue.empty():
await self.queue.get()

await self._write_data(request_data)

response_data = await self._read_data()
if response_data is None:
i = i - 1
continue

except asyncio.exceptions.CancelledError:
logger.debug("Received CancelledError")
i = i - 1
continue

break

if i == 0:
logger.error("Unable to communicate with device: '%s'", self.address)
if self.is_connected():
await self.disconnect()
return None
try:
# If there are previous responses, flush them out
while not self.queue.empty():
await self.queue.get()

await self._write_data(request_data)

response_data = await self._read_data()
if response_data is None:
logger.error(
"Unable to communicate with device: '%s'", self.address
)
if self.is_connected():
await self.disconnect()
return None

except asyncio.exceptions.CancelledError:
logger.debug("Received CancelledError")
if self.is_connected():
await self.disconnect()
return None

return response_data

Expand All @@ -388,10 +387,11 @@ async def connect(self, device) -> ResponseResult:
return ResponseResult.UNKNOWN_ERROR

logger.info("connecting to device...")
self.client = BleakClient(
device, services=["98bd0001-0b0e-421a-84e5-ddbf75dc6de4"], use_cached=True
self.client = await establish_connection(
BleakClientWithServiceCache,
device,
device.name or "Unknown Device",
)
await self.client.connect()
logger.info("connected")

logger.info("pairing device...")
Expand Down Expand Up @@ -475,11 +475,12 @@ def is_connected(self) -> bool:

async def probe_gatts(self, device):
logger.info("connecting to device...")
client = BleakClient(
device, services=["98bd0001-0b0e-421a-84e5-ddbf75dc6de4"], use_cached=True
client = await establish_connection(
BleakClientWithServiceCache,
device,
device.name or "Unknown Device",
max_attempts=3, # Will retry up to 3 times with backoff
)

await client.connect()
logger.info("connected")

manufacture = None
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ classifiers = [
]
dependencies = [
"bleak",
"bleak_retry_connector",
]

[tool.ruff]
Expand Down