Skip to content

Commit 37a6a2a

Browse files
committed
Recover from mid-auth BLE link loss instead of hanging
1 parent 3b7cc09 commit 37a6a2a

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

custom_components/ef_ble/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""The unofficial EcoFlow BLE devices integration"""
22

3+
import asyncio
34
import logging
45
from collections.abc import Callable
56
from functools import partial
@@ -130,8 +131,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: DeviceConfigEntry) -> bo
130131
max_attempts=0 if eflib.is_solar_only(device) else None,
131132
)
132133
)
133-
state = await device.wait_until_authenticated_or_error(raise_on_error=True)
134+
async with asyncio.timeout(timeout):
135+
state = await device.wait_until_authenticated_or_error(raise_on_error=True)
134136
except (ConnectionTimeout, BleakError, TimeoutError) as e:
137+
await device.disconnect()
135138
raise ConfigEntryNotReady(
136139
translation_key="could_not_connect",
137140
translation_placeholders={"time": str(timeout), "error_msg": str(e)},

custom_components/ef_ble/eflib/connection.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,17 @@ async def sendRequest(self, send_data: bytes, response_handler=None):
809809
try:
810810
await self._sendRequest(send_data, response_handler)
811811
except Exception as e: # noqa: BLE001
812+
if self._client is None or not self._client.is_connected:
813+
# The BLE link dropped mid-request - e.g. BlueZ raising "Remote peer
814+
# disconnected" synchronously from start_notify. bleak does not
815+
# always fire its disconnected callback for a synchronous GATT
816+
# failure, so nothing else would drive a reconnect and
817+
# `wait_until_authenticated_or_error` hangs forever.
818+
self._logger.warning(
819+
"BLE link lost while sending request (%s); reconnecting", e
820+
)
821+
self.disconnected()
822+
return
812823
self._logger.log_filtered(
813824
LogOptions.CONNECTION_DEBUG,
814825
(

0 commit comments

Comments
 (0)