Use BLE device name, add hard poll timeout, tighten request timing

- Device name: coordinator now takes name=entry.title so the HA device card
  shows the actual BLE advertised name instead of "Xiaoxiang Smart BMS"
- Hard poll timeout: each poll is capped at (poll_interval - 3)s via
  asyncio.wait_for so a stalled poll can't bleed into the next cycle
- Request timeout: 5s → 3s (BMS should reply in <1s under normal conditions)
- Retry delay: 0.5s → 0.3s between retries

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-11 20:21:37 +02:00
parent 3fc25c083b
commit e1e6d69d2c
3 changed files with 18 additions and 7 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
address = entry.data[CONF_ADDRESS]
poll_interval = entry.options.get(CONF_POLL_INTERVAL, DEFAULT_POLL_INTERVAL)
coordinator = BmsCoordinator(hass, address, poll_interval)
coordinator = BmsCoordinator(hass, address, poll_interval, name=entry.title)
await coordinator.async_setup()
await coordinator.async_config_entry_first_refresh()
@@ -47,7 +47,7 @@ class BmsBluetoothHandler:
self,
ble_device: BLEDevice,
commands: list[bytes],
timeout: float = 5.0,
timeout: float = 3.0,
retries: int = 3,
) -> list[bytes | None]:
"""Connect, send each command in sequence, disconnect.
@@ -63,7 +63,7 @@ class BmsBluetoothHandler:
await client.start_notify(RX_CHAR_UUID, self._on_notify)
# Give the BMS a moment to register the subscription before
# we start sending commands
await asyncio.sleep(0.5)
await asyncio.sleep(0.3)
return [
await self._request(client, cmd, timeout, retries)
for cmd in commands
@@ -147,7 +147,7 @@ class BmsBluetoothHandler:
_LOGGER.error("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:
@@ -157,7 +157,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
+13 -2
View File
@@ -1,6 +1,7 @@
"""DataUpdateCoordinator for the Xiaoxiang Smart BMS."""
from __future__ import annotations
import asyncio
import logging
from datetime import timedelta
@@ -27,6 +28,7 @@ class BmsCoordinator(DataUpdateCoordinator[dict]):
hass: HomeAssistant,
address: str,
poll_interval: int,
name: str = "Xiaoxiang Smart BMS",
) -> None:
super().__init__(
hass,
@@ -35,6 +37,8 @@ class BmsCoordinator(DataUpdateCoordinator[dict]):
update_interval=timedelta(seconds=poll_interval),
)
self.address = address
self._device_name = name
self._poll_timeout = max(poll_interval - 3, 10) # hard cap, leaves 3s slack
self._handler = BmsBluetoothHandler(address)
self.hw_version: str | None = None
@@ -46,7 +50,7 @@ class BmsCoordinator(DataUpdateCoordinator[dict]):
def device_info(self) -> DeviceInfo:
return DeviceInfo(
identifiers={(DOMAIN, self.address)},
name="Xiaoxiang Smart BMS",
name=self._device_name,
manufacturer="Xiaoxiang",
model=self.hw_version or "Smart BMS",
)
@@ -79,7 +83,14 @@ class BmsCoordinator(DataUpdateCoordinator[dict]):
commands.append(CMD_VERSION)
try:
responses = await self._handler.poll(device, commands)
responses = await asyncio.wait_for(
self._handler.poll(device, commands),
timeout=self._poll_timeout,
)
except asyncio.TimeoutError:
raise UpdateFailed(
f"BMS poll timed out after {self._poll_timeout}s"
)
except Exception as exc:
raise UpdateFailed(f"BMS poll failed: {exc}") from exc