From d22a26d2dc2acd70e5316c00621048fe17e07dbd Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 15 Oct 2026 23:44:40 -0700 Subject: [PATCH] add timeout exception to _discover_device --- api/ble.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/api/ble.py b/api/ble.py index d7a2d07..c21e2fd 100644 --- a/api/ble.py +++ b/api/ble.py @@ -65,13 +65,26 @@ class SensorReading: # --------------------------------------------------------------------------- # async def _discover_device(mac: str) -> Optional[str]: """Scan once for the given MAC and return its address if found.""" - devices = await BleakScanner.discover(timeout=SCAN_TIMEOUT) - for d in devices: - if d.address.lower() == mac.lower(): - log.info("Found device %s (%s)", d.name or d.address, d.address) - return d.address - log.warning("Device %s not found in scan", mac) + try: + devices = await asyncio.wait_for( + BleakScanner.discover(timeout=SCAN_TIMEOUT), + timeout=SCAN_TIMEOUT + 2.0, # give a small buffer over scan timeout + ) + for d in devices: + if d.address.lower() == mac.lower(): + log.info("Found device %s (%s)", d.name or d.address, d.address) + return d.address + log.warning("Device %s not found in scan", mac) + except asyncio.TimeoutError: + log.warning("Discover call timed out after %.1fs for %s", SCAN_TIMEOUT + 2.0, mac) return None + + #for d in devices: + # if d.address.lower() == mac.lower(): + # log.info("Found device %s (%s)", d.name or d.address, d.address) + # return d.address + #log.warning("Device %s not found in scan", mac) + #return None def _parse_sensor_data(raw: bytes) -> (float, int):