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):