"""Xiaoxiang Smart BMS — Home Assistant integration.""" from __future__ import annotations import asyncio from homeassistant.components.bluetooth import ( BluetoothChange, BluetoothScanningMode, BluetoothServiceInfoBleak, async_register_callback, ) from homeassistant.components.bluetooth.match import ADDRESS, BluetoothCallbackMatcher from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from .const import CONF_ADDRESS, CONF_POLL_INTERVAL, DEFAULT_POLL_INTERVAL, DOMAIN from .coordinator import BmsCoordinator PLATFORMS = ["sensor", "binary_sensor", "number", "select"] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up the BMS integration from a config entry.""" address = entry.data[CONF_ADDRESS] poll_interval = entry.options.get(CONF_POLL_INTERVAL, DEFAULT_POLL_INTERVAL) hass.data.setdefault(DOMAIN, {}) # Shared BLE lock — only one BMS connects at a time to avoid # ESPHome proxy connection slot exhaustion with multiple devices. if "_ble_lock" not in hass.data[DOMAIN]: hass.data[DOMAIN]["_ble_lock"] = asyncio.Lock() ble_lock = hass.data[DOMAIN]["_ble_lock"] coordinator = BmsCoordinator( hass, address, poll_interval, name=entry.title, ble_lock=ble_lock, ) # Keep the coordinator's BLE device reference fresh via advertisement callback. # This avoids stale transport paths when ESPHome proxies cycle. @callback def _async_update_ble( service_info: BluetoothServiceInfoBleak, change: BluetoothChange, ) -> None: coordinator._ble_device = service_info.device entry.async_on_unload( async_register_callback( hass, _async_update_ble, BluetoothCallbackMatcher({ADDRESS: address}), BluetoothScanningMode.PASSIVE, ) ) await coordinator.async_setup() await coordinator.async_config_entry_first_refresh() hass.data[DOMAIN][entry.entry_id] = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) entry.async_on_unload(entry.add_update_listener(_async_update_listener)) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload the BMS integration.""" unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: coordinator: BmsCoordinator = hass.data[DOMAIN].pop(entry.entry_id) await coordinator.async_teardown() return unload_ok async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: """Reload the entry when options are changed.""" await hass.config_entries.async_reload(entry.entry_id)