Skip to content

Commit

Permalink
pybricksdev.cli.flash: remove use of deprecated BLEDevice.metadata
Browse files Browse the repository at this point in the history
Bleak raises a FutureWarning about using the deprecated BLEDevice.metadata
attribute. We can avoid this by capturing the advertising data ourselves.
Ideally, a better API should be provided upstream though.
  • Loading branch information
dlech committed Apr 12, 2023
1 parent 54f3725 commit 7b19137
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Fixed Bleak `FutureWarning` about using `BLEDevice.metadata`.

## [1.0.0-alpha.41] - 2023-03-26

### Fixed
Expand Down
23 changes: 18 additions & 5 deletions pybricksdev/cli/flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
import zipfile
from tempfile import NamedTemporaryFile
from typing import BinaryIO, Optional
from typing import BinaryIO, Dict, Optional

from bleak import BleakClient, BleakScanner
from bleak.backends.device import BLEDevice
Expand Down Expand Up @@ -267,11 +267,22 @@ async def flash_ble(hub_kind: HubKind, firmware: bytes, metadata: dict):

print(f"Searching for {hub_kind.name} hub...")

# TODO: add upstream feature to Bleak to allow getting device, adv tuple
# as return value from find_device_by_filter()
# https://github.com/hbldh/bleak/issues/1277

device_adv_map: Dict[str, AdvertisementData] = {}

def map_and_match(device: BLEDevice, adv: AdvertisementData):
# capture the adv data for later use
device_adv_map[device.address] = adv
return match_hub(hub_kind, adv)

# scan for hubs in bootloader mode, running official LEGO firmware or
# running Pybricks firmware

device = await BleakScanner.find_device_by_filter(
lambda _d, a: match_hub(hub_kind, a),
map_and_match,
service_uuids=[
LWP3_BOOTLOADER_SERVICE_UUID,
LWP3_HUB_SERVICE_UUID,
Expand All @@ -283,16 +294,18 @@ async def flash_ble(hub_kind: HubKind, firmware: bytes, metadata: dict):
print("timed out", file=sys.stderr)
return

adv_data = device_adv_map[device.address]

# if not already in bootlaoder mode, we need to reboot into bootloader mode
if LWP3_HUB_SERVICE_UUID in device.metadata["uuids"]:
if LWP3_HUB_SERVICE_UUID in adv_data.service_uuids:
print("Found hub running official LEGO firmware.")
await reboot_official_to_bootloader(hub_kind, device)
elif PYBRICKS_SERVICE_UUID in device.metadata["uuids"]:
elif PYBRICKS_SERVICE_UUID in adv_data.service_uuids:
print("Found hub running Pybricks firmware.")
await reboot_pybricks_to_bootloader(hub_kind, device)

# if not previously in bootlaoder mode, scan again, this time only for bootloader
if LWP3_BOOTLOADER_SERVICE_UUID not in device.metadata["uuids"]:
if LWP3_BOOTLOADER_SERVICE_UUID not in adv_data.service_uuids:
device = await BleakScanner.find_device_by_filter(
lambda _d, a: match_hub(hub_kind, a),
service_uuids=[
Expand Down

0 comments on commit 7b19137

Please sign in to comment.