Skip to content

Commit 4526cba

Browse files
committed
poetry: update to Bleak v0.12.0
This allows us to simplify device scanning by using a new class method and enables support for Python 3.9.
1 parent 168dca3 commit 4526cba

File tree

5 files changed

+41
-29
lines changed

5 files changed

+41
-29
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8-
## Fixed
8+
9+
### Added
10+
- Support for Python 3.9.
11+
12+
### Changed
13+
- Update to Bleak v0.12.0
14+
15+
### Fixed
916
- Fix `pybricks lwp3 repl` can only connect to remote control.
1017
- Fix Technic Large hub Bluetooth hub kind.
1118

poetry.lock

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pybricksdev/ble/__init__.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,25 @@ async def find_device(
4141
"""
4242
print(f"Searching for {name or service}")
4343

44-
queue = asyncio.Queue()
45-
46-
def handle_detection(device: BLEDevice, adv: AdvertisementData):
44+
def match_uuid_and_name(device: BLEDevice, adv: AdvertisementData):
4745
if service not in adv.service_uuids:
48-
return
46+
return False
47+
4948
if (
5049
name is not None
5150
and adv.local_name != name
5251
and device.address.upper() != name.upper()
5352
):
54-
return
55-
queue.put_nowait(device)
53+
return False
54+
55+
return True
56+
57+
device = await BleakScanner.find_device_by_filter(match_uuid_and_name, timeout)
58+
59+
if device is None:
60+
raise asyncio.TimeoutError
5661

57-
async with BleakScanner(detection_callback=handle_detection):
58-
return await asyncio.wait_for(queue.get(), timeout=timeout)
62+
return device
5963

6064

6165
class BLEConnection:

pybricksdev/cli/lwp3/repl.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@ async def repl() -> None:
7171
os.makedirs(history_file.parent, exist_ok=True)
7272
session = PromptSession(history=FileHistory(history_file))
7373

74-
queue = asyncio.Queue()
75-
76-
def callback(dev: BLEDevice, adv: AdvertisementData) -> None:
74+
def match_lwp3_uuid(dev: BLEDevice, adv: AdvertisementData) -> None:
7775
if LWP3_HUB_SERVICE_UUID.lower() not in adv.service_uuids:
78-
return
76+
return False
7977

8078
mfg_data = adv.manufacturer_data[LEGO_CID]
8179
button, kind, cap, last_net, status, opt = struct.unpack("<6B", mfg_data)
@@ -94,18 +92,17 @@ def callback(dev: BLEDevice, adv: AdvertisementData) -> None:
9492
opt,
9593
)
9694

97-
queue.put_nowait(dev)
95+
return True
96+
97+
logger.info("scanning...")
9898

99-
async with BleakScanner(detection_callback=callback) as scanner:
100-
logger.info("scanning...")
101-
device = await queue.get()
99+
device = await BleakScanner.find_device_by_filter(match_lwp3_uuid)
102100

103-
# monkey patch for bleak bug
104-
# https://github.com/hbldh/bleak/pull/534
105-
if hasattr(scanner, "_manager"):
106-
device.metadata["delegate"] = scanner._manager.central_manager.delegate()
101+
if device is None:
102+
logger.error("timed out")
103+
return
107104

108-
logger.info("found device")
105+
logger.info("found device")
109106

110107
def handle_disconnect(client: BleakClient):
111108
logger.info("disconnected")

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ pybricksdev = 'pybricksdev.cli:main'
3030
aioserial = "^1.3.0"
3131
argcomplete = "^1.11.1"
3232
asyncssh = "^2.2.1"
33-
bleak = "^0.11.0"
33+
bleak = "0.12.0"
3434
mpy-cross = "1.14"
35-
python = "~3.8"
35+
python = "^3.8"
3636
tqdm = "^4.46.1"
3737
validators = "^0.18.2"
3838
pyusb = "^1.0.2"

0 commit comments

Comments
 (0)