"""Config flow for linak integration.""" from __future__ import annotations from typing import Any from home_assistant_bluetooth import BluetoothServiceInfoBleak import voluptuous as vol from homeassistant import config_entries, data_entry_flow from homeassistant.components.bluetooth.api import async_discovered_service_info from homeassistant.const import CONF_ADDRESS from homeassistant.data_entry_flow import FlowResult from .const import DOMAIN, UUID_ADV_SERVICE class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for linak.""" VERSION = 1 def __init__(self) -> None: """Initialize config flow""" self._discovered_address: str self._discovered_addresses: list[str] = [] def _create_entry(self, address: str) -> FlowResult: """Create an entry for a discovered device""" return self.async_create_entry( title=address, data={ CONF_ADDRESS: address, }, ) async def async_step_bluetooth_confirm( self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle user-configuration of discovered device.""" if user_input is not None: return self._create_entry(self._discovered_address) return self.async_show_form( step_id='bluetooth_confirm', description_placeholders={"name": self._discovered_address}, ) async def async_step_bluetooth( self, discovery_info: BluetoothServiceInfoBleak ) -> data_entry_flow.FlowResult: """Handle config initiated by discovery""" address = discovery_info.address await self.async_set_unique_id(address) self._abort_if_unique_id_configured(updates={CONF_ADDRESS: address}) self._discovered_address = address self.context['title_placeholders'] = {"name": address} return await self.async_step_bluetooth_confirm() async def async_step_pick_device( self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle pick discovered device""" if user_input is not None: address = user_input[CONF_ADDRESS] await self.async_set_unique_id(address, raise_on_progress=False) self._abort_if_unique_id_configured() return self._create_entry(address) current_addresses = self._async_current_ids() for discovery_info in async_discovered_service_info( self.hass, connectable=True ): if UUID_ADV_SERVICE in discovery_info.service_uuids: address = discovery_info.address if ( address not in current_addresses and address not in self._discovered_addresses ): self._discovered_addresses.append(address) addresses = { address for address in self._discovered_addresses if address not in current_addresses } if not addresses: return self.async_abort(reason="no_devices_found") return self.async_show_form( step_id='pick_device', data_schema=vol.Schema({vol.Required(CONF_ADDRESS): vol.In(addresses)}), ) async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle the initial step.""" return await self.async_step_pick_device()