diff --git a/homeassistant/components/esphome/bluetooth/client.py b/homeassistant/components/esphome/bluetooth/client.py index a5075da0bf9..66b687b6de9 100644 --- a/homeassistant/components/esphome/bluetooth/client.py +++ b/homeassistant/components/esphome/bluetooth/client.py @@ -265,11 +265,26 @@ class ESPHomeClient(BaseBleakClient): if not (scanner := async_scanner_by_source(self._hass, self._source)): raise BleakError("Scanner disappeared for {self._source}") with scanner.connecting(): - self._cancel_connection_state = await self._client.bluetooth_device_connect( - self._address_as_int, - _on_bluetooth_connection_state, - timeout=timeout, - ) + try: + self._cancel_connection_state = ( + await self._client.bluetooth_device_connect( + self._address_as_int, + _on_bluetooth_connection_state, + timeout=timeout, + ) + ) + except Exception: # pylint: disable=broad-except + with contextlib.suppress(BleakError): + # If the connect call throws an exception, + # we need to make sure we await the future + # to avoid a warning about an un-retrieved + # exception since we prefer to raise the + # exception from the connect call as it + # will be more descriptive. + if connected_future.done(): + await connected_future + connected_future.cancel() + raise await connected_future await self.get_services(dangerous_use_bleak_cache=dangerous_use_bleak_cache) self._disconnected_event = asyncio.Event()