Files
Jannis 1520ed3c0f Refactor BLE layer for 24/7 reliability
- Replace raw BleakClient with establish_connection from
  bleak-retry-connector (retries, GATT service cache, proxy-aware)
- Replace fragile asyncio.Event with asyncio.Queue for response frames,
  drain stale data on each connection to prevent cross-cycle leakage
- Register BLE advertisement callback to keep BLEDevice reference fresh
  across ESPHome proxy path changes
- Remove asyncio.sleep(2) device lookup hack
- Increase poll timeout floor from 10s to 20s
- Increase failure tolerance from 3 to 5 consecutive misses
- Bump default poll interval to 30s, min to 15s (halves connection churn)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-12 09:36:08 +02:00

36 lines
1.4 KiB
Python

"""Constants for the Xiaoxiang Smart BMS integration."""
DOMAIN = "xiaoxiang_bms"
CONF_ADDRESS = "address"
CONF_POLL_INTERVAL = "poll_interval"
DEFAULT_POLL_INTERVAL = 30 # seconds — each poll does a full BLE connect/disconnect
MIN_POLL_INTERVAL = 15 # below this the BMS has no breathing room between polls
MAX_POLL_INTERVAL = 300
# GATT UUIDs (Xiaoxiang BMS UART-over-GATT)
UART_SERVICE_UUID = "0000ff00-0000-1000-8000-00805f9b34fb"
RX_CHAR_UUID = "0000ff01-0000-1000-8000-00805f9b34fb" # BMS → HA (notify)
TX_CHAR_UUID = "0000ff02-0000-1000-8000-00805f9b34fb" # HA → BMS (write)
# Request frames: [0xDD, 0xA5, CMD, 0x00, CHK_HI, CHK_LO, 0x77]
CMD_GENERAL = bytes([0xDD, 0xA5, 0x03, 0x00, 0xFF, 0xFD, 0x77]) # pack info
CMD_CELL = bytes([0xDD, 0xA5, 0x04, 0x00, 0xFF, 0xFC, 0x77]) # cell voltages
CMD_VERSION = bytes([0xDD, 0xA5, 0x05, 0x00, 0xFF, 0xFB, 0x77]) # hardware version string
# Frame markers
FRAME_START = 0xDD
FRAME_END = 0x77
# MOS control values (XX byte in write command DD 5A E1 02 00 XX CHK_H CHK_L 77)
MOS_NORMAL = 0x00 # release software lock — both gates open
MOS_CHARGE_OFF = 0x01 # disable charge MOS, keep discharge MOS on
MOS_DISCHARGE_OFF = 0x02 # disable discharge MOS, keep charge MOS on
MOS_BOTH_OFF = 0x03 # disable both charge and discharge MOS
# Response command IDs (byte 1 of frame)
CMD_ID_GENERAL = 0x03
CMD_ID_CELL = 0x04
CMD_ID_VERSION = 0x05