From eaba29657f06dfca6a5020a654cb15b3fb1a933a Mon Sep 17 00:00:00 2001 From: Alan Murray Date: Thu, 30 Dec 2021 20:06:44 +1100 Subject: [PATCH] Catch bluetooth_tracker OSError (#60437) Catch when bluetooth_tracker integration throws an OSError when a bluetooth device can't be found. --- .../bluetooth_tracker/device_tracker.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/bluetooth_tracker/device_tracker.py b/homeassistant/components/bluetooth_tracker/device_tracker.py index 8883f600019..484a684b4d3 100644 --- a/homeassistant/components/bluetooth_tracker/device_tracker.py +++ b/homeassistant/components/bluetooth_tracker/device_tracker.py @@ -60,13 +60,18 @@ def is_bluetooth_device(device: Device) -> bool: def discover_devices(device_id: int) -> list[tuple[str, str]]: """Discover Bluetooth devices.""" - result = bluetooth.discover_devices( - duration=8, - lookup_names=True, - flush_cache=True, - lookup_class=False, - device_id=device_id, - ) + try: + result = bluetooth.discover_devices( + duration=8, + lookup_names=True, + flush_cache=True, + lookup_class=False, + device_id=device_id, + ) + except OSError as ex: + # OSError is generally thrown if a bluetooth device isn't found + _LOGGER.error("Couldn't discover bluetooth devices: %s", ex) + return [] _LOGGER.debug("Bluetooth devices discovered = %d", len(result)) return result # type: ignore[no-any-return]