The li3 BMS has a persistently marginal BLE link from any single fixed vantage point on this Pi (onboard adapter, a USB dongle that turned out to be BR/EDR-only, and the Athom ESPHome BT proxy even after moving it closer). Move capture into HA proper so bluetooth.async_ble_device_from_address can pick whichever known source currently has the device, instead of hardcoding one. Protocol parsing ported verbatim from li3/van-li3-battery. van-li3-battery is disabled on the Pi; its MQTT discovery entities were cleared. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
"""Config flow for the Lithionics Li3 BMS integration."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components.bluetooth import (
|
|
BluetoothServiceInfoBleak,
|
|
async_discovered_service_info,
|
|
)
|
|
from homeassistant.config_entries import ConfigFlow
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
class Li3ConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for a Lithionics Li3 BMS."""
|
|
|
|
VERSION = 1
|
|
|
|
def __init__(self) -> None:
|
|
self._discovery_info: BluetoothServiceInfoBleak | None = None
|
|
self._discovered: dict[str, str] = {}
|
|
|
|
async def async_step_bluetooth(
|
|
self, discovery_info: BluetoothServiceInfoBleak
|
|
) -> FlowResult:
|
|
"""Handle a discovered Li3 advertisement (from any Bluetooth source)."""
|
|
await self.async_set_unique_id(discovery_info.address)
|
|
self._abort_if_unique_id_configured()
|
|
self._discovery_info = discovery_info
|
|
self.context["title_placeholders"] = {"name": discovery_info.name}
|
|
return await self.async_step_bluetooth_confirm()
|
|
|
|
async def async_step_bluetooth_confirm(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> FlowResult:
|
|
assert self._discovery_info is not None
|
|
if user_input is not None:
|
|
return self.async_create_entry(
|
|
title=self._discovery_info.name,
|
|
data={
|
|
"address": self._discovery_info.address,
|
|
"name": self._discovery_info.name,
|
|
},
|
|
)
|
|
return self.async_show_form(
|
|
step_id="bluetooth_confirm",
|
|
description_placeholders={"name": self._discovery_info.name},
|
|
)
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> FlowResult:
|
|
"""Manual entry, plus a dropdown of any Li3 already seen advertising."""
|
|
errors: dict[str, str] = {}
|
|
if user_input is not None:
|
|
address = user_input["address"]
|
|
await self.async_set_unique_id(address, raise_on_progress=False)
|
|
self._abort_if_unique_id_configured()
|
|
name = self._discovered.get(address, "Li3 Battery")
|
|
return self.async_create_entry(title=name, data={"address": address, "name": name})
|
|
|
|
current_addresses = self._async_current_ids()
|
|
for info in async_discovered_service_info(self.hass, connectable=True):
|
|
if info.address in current_addresses:
|
|
continue
|
|
if info.name and info.name.startswith("Li3-"):
|
|
self._discovered[info.address] = info.name
|
|
|
|
if not self._discovered:
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=vol.Schema({vol.Required("address"): str}),
|
|
errors=errors,
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=vol.Schema({vol.Required("address"): vol.In(self._discovered)}),
|
|
errors=errors,
|
|
)
|