Try to switch to a different esphome BLE proxy if we run out of slots while connecting (#81268)

This commit is contained in:
J. Nick Koston 2022-10-31 08:27:04 -05:00 committed by GitHub
parent 5ba3b499fe
commit 8416cc1906
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -264,6 +264,7 @@ class HaBleakClientWrapper(BleakClient):
self.__address = address_or_ble_device
self.__disconnected_callback = disconnected_callback
self.__timeout = timeout
self.__ble_device: BLEDevice | None = None
self._backend: BaseBleakClient | None = None # type: ignore[assignment]
@property
@ -283,14 +284,21 @@ class HaBleakClientWrapper(BleakClient):
async def connect(self, **kwargs: Any) -> bool:
"""Connect to the specified GATT server."""
if not self._backend:
if (
not self._backend
or not self.__ble_device
or not self._async_get_backend_for_ble_device(self.__ble_device)
):
assert MANAGER is not None
wrapped_backend = (
self._async_get_backend() or self._async_get_fallback_backend()
)
self._backend = wrapped_backend.client(
self.__ble_device = (
await freshen_ble_device(wrapped_backend.device)
or wrapped_backend.device,
or wrapped_backend.device
)
self._backend = wrapped_backend.client(
self.__ble_device,
disconnected_callback=self.__disconnected_callback,
timeout=self.__timeout,
hass=MANAGER.hass,