"""Config flow for the Carefree Connects BT12 awning 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 Bt12ConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for a Carefree BT12 awning controller.""" 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 BT12 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 BT12 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, "BT12 Awning") 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 == "BT12": 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, )