Prefer BLE device object resolution and bump to 0.1.12
This commit is contained in:
@@ -90,6 +90,23 @@ async def find_printer() -> str:
|
||||
raise PrinterNotFound("No Fichero/D11s printer found. Is it turned on?")
|
||||
|
||||
|
||||
async def resolve_ble_target(address: str | None = None):
|
||||
"""Resolve a BLE target as Bleak device object when possible.
|
||||
|
||||
Passing a discovered device object to BleakClient helps BlueZ keep the
|
||||
correct LE context for dual-mode environments.
|
||||
"""
|
||||
if address:
|
||||
device = await BleakScanner.find_device_by_address(address, timeout=8.0)
|
||||
return device or address
|
||||
devices = await BleakScanner.discover(timeout=8)
|
||||
for d in devices:
|
||||
if d.name and any(d.name.startswith(p) for p in PRINTER_NAME_PREFIXES):
|
||||
print(f" Found {d.name} at {d.address}")
|
||||
return d
|
||||
raise PrinterNotFound("No Fichero/D11s printer found. Is it turned on?")
|
||||
|
||||
|
||||
# --- Status ---
|
||||
|
||||
|
||||
@@ -406,7 +423,7 @@ async def connect(
|
||||
) from last_exc
|
||||
raise PrinterError(f"Classic Bluetooth connection failed for '{address}'.")
|
||||
else:
|
||||
addr = address or await find_printer()
|
||||
target = await resolve_ble_target(address)
|
||||
def _is_retryable_ble_error(exc: Exception) -> bool:
|
||||
msg = str(exc).lower()
|
||||
return any(token in msg for token in ("timeout", "timed out", "br-connection-timeout"))
|
||||
@@ -414,7 +431,7 @@ async def connect(
|
||||
last_exc: Exception | None = None
|
||||
for attempt in range(1, BLE_CONNECT_RETRIES + 1):
|
||||
try:
|
||||
async with BleakClient(addr) as client:
|
||||
async with BleakClient(target) as client:
|
||||
pc = PrinterClient(client)
|
||||
await pc.start()
|
||||
yield pc
|
||||
|
||||
Reference in New Issue
Block a user