Fix un-retrieved future in esphome ble client when library raises (#82537)

pull/82670/head
J. Nick Koston 2022-11-24 13:38:53 -07:00 committed by GitHub
parent c30ce12c49
commit 18dd605a74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 5 deletions

View File

@ -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()