84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
"""Bluetti Bluetooth Config Flow"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.components.bluetooth import (
|
|
BluetoothServiceInfoBleak,
|
|
async_discovered_service_info,
|
|
)
|
|
from homeassistant.const import CONF_ADDRESS
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
|
|
from .const import DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Handle config flow for Bluetti BT devices."""
|
|
|
|
def __init__(self) -> None:
|
|
_LOGGER.info("Initialize config flow")
|
|
self._discovery_info: BluetoothServiceInfoBleak | None = None
|
|
self._discovered_devices: dict[str, BluetoothServiceInfoBleak] = {}
|
|
|
|
async def async_step_bluetooth(
|
|
self, discovery_info: BluetoothServiceInfoBleak
|
|
) -> FlowResult:
|
|
"""Handle bluetooth discovery."""
|
|
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_user()
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> FlowResult:
|
|
"""Handle user input."""
|
|
|
|
if user_input is not None:
|
|
address = user_input[CONF_ADDRESS]
|
|
discovery_info = self._discovered_devices[address]
|
|
await self.async_set_unique_id(address, raise_on_progress=False)
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title=discovery_info.name,
|
|
data={
|
|
CONF_ADDRESS: discovery_info.address,
|
|
},
|
|
)
|
|
|
|
if discovery := self._discovery_info:
|
|
self._discovered_devices[discovery.address] = discovery
|
|
else:
|
|
current_addresses = self._async_current_ids()
|
|
for discovery in async_discovered_service_info(self.hass):
|
|
address = discovery.address
|
|
if address in current_addresses or address in self._discovered_devices:
|
|
continue
|
|
self._discovered_devices[discovery.address] = discovery
|
|
|
|
if not self._discovered_devices:
|
|
return self.async_abort(reason="no_unconfigured_devices")
|
|
|
|
data_schema = vol.Schema(
|
|
{
|
|
vol.Required(CONF_ADDRESS): vol.In(
|
|
{
|
|
service_info.address: service_info.name
|
|
for service_info in self._discovered_devices.values()
|
|
}
|
|
),
|
|
}
|
|
)
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=data_schema,
|
|
)
|