Add global BLE lock + faster timeouts for multi-device reliability

Root cause: 3 BMS devices fighting for 3 ESPHome proxy connection slots
simultaneously, causing 80% timeout failures and 22s+ poll times.

Fixes:
- Add shared asyncio.Lock so only one BMS polls at a time — eliminates
  proxy slot contention entirely
- Pass ble_device_callback to establish_connection so retry attempts
  get a fresh BLEDevice (handles proxy path changes)
- Reduce command timeout 5s -> 3s, retries 3 -> 2 (BMS responds in
  <200ms when connection is clean)
- Reduce establish_connection max_attempts 3 -> 2 (fail fast, retry
  next cycle instead of blocking 25s)
- Fixed poll timeout to 15s (was poll_interval-5=25s)

Expected: polls complete in 2-5s instead of 22s, ~95%+ success rate.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-12 10:05:54 +02:00
parent 1520ed3c0f
commit dcc528b96a
3 changed files with 65 additions and 23 deletions
@@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio
import logging
import struct
from collections.abc import Callable
from bleak import BleakError
from bleak.backends.device import BLEDevice
@@ -63,8 +64,9 @@ class BmsBluetoothHandler:
self,
ble_device: BLEDevice,
commands: list[bytes],
timeout: float = 5.0,
retries: int = 3,
timeout: float = 3.0,
retries: int = 2,
ble_device_callback: Callable[[], BLEDevice | None] | None = None,
) -> list[bytes | None]:
"""Connect, send each command in sequence, disconnect.
@@ -79,7 +81,8 @@ class BmsBluetoothHandler:
BleakClientWithServiceCache,
ble_device,
self._address,
max_attempts=3,
max_attempts=2,
ble_device_callback=ble_device_callback,
)
try:
await client.start_notify(RX_CHAR_UUID, self._on_notify)
@@ -166,7 +169,7 @@ class BmsBluetoothHandler:
_LOGGER.warning("BLE write failed (attempt %d/%d): %s",
attempt, retries, exc)
if attempt < retries:
await asyncio.sleep(0.5)
await asyncio.sleep(0.3)
continue
try:
@@ -178,7 +181,7 @@ class BmsBluetoothHandler:
_LOGGER.warning("BMS timeout (cmd=0x%s, attempt %d/%d)",
command.hex(), attempt, retries)
if attempt < retries:
await asyncio.sleep(0.5)
await asyncio.sleep(0.3)
return None
@@ -186,7 +189,12 @@ class BmsBluetoothHandler:
# MOS write command
# ------------------------------------------------------------------
async def write_mos(self, ble_device: BLEDevice, value: int) -> bool:
async def write_mos(
self,
ble_device: BLEDevice,
value: int,
ble_device_callback: Callable[[], BLEDevice | None] | None = None,
) -> bool:
"""Send a MOS control write command and return True on ACK.
Follows the same connect -> send -> disconnect pattern as poll() so
@@ -200,12 +208,13 @@ class BmsBluetoothHandler:
BleakClientWithServiceCache,
ble_device,
self._address,
max_attempts=3,
max_attempts=2,
ble_device_callback=ble_device_callback,
)
try:
await client.start_notify(RX_CHAR_UUID, self._on_notify)
await asyncio.sleep(0.3)
response = await self._request(client, command, timeout=5.0, retries=2)
response = await self._request(client, command, timeout=3.0, retries=2)
return response is not None and response[2] == 0x00
finally:
try: