2022-07-22 18:19:53 +00:00
|
|
|
"""Config flow to configure the Bluetooth integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-08-01 15:54:06 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2022-07-22 18:19:53 +00:00
|
|
|
|
2022-07-25 14:52:35 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2022-07-23 15:47:12 +00:00
|
|
|
from homeassistant.components import onboarding
|
2022-07-25 14:52:35 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
|
|
|
|
from homeassistant.core import callback
|
2022-07-22 18:19:53 +00:00
|
|
|
|
2022-07-25 14:52:35 +00:00
|
|
|
from .const import CONF_ADAPTER, DEFAULT_NAME, DOMAIN
|
|
|
|
from .util import async_get_bluetooth_adapters
|
2022-07-22 18:19:53 +00:00
|
|
|
|
2022-08-01 15:54:06 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
|
|
|
2022-07-22 18:19:53 +00:00
|
|
|
|
|
|
|
class BluetoothConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Config flow for Bluetooth."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
return await self.async_step_enable_bluetooth()
|
|
|
|
|
|
|
|
async def async_step_enable_bluetooth(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle a flow initialized by the user or import."""
|
|
|
|
if self._async_current_entries():
|
|
|
|
return self.async_abort(reason="already_configured")
|
|
|
|
|
2022-07-23 15:47:12 +00:00
|
|
|
if user_input is not None or not onboarding.async_is_onboarded(self.hass):
|
2022-07-22 18:19:53 +00:00
|
|
|
return self.async_create_entry(title=DEFAULT_NAME, data={})
|
|
|
|
|
|
|
|
return self.async_show_form(step_id="enable_bluetooth")
|
|
|
|
|
|
|
|
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
|
|
|
|
"""Handle import from configuration.yaml."""
|
|
|
|
return await self.async_step_enable_bluetooth(user_input)
|
2022-07-25 14:52:35 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@callback
|
|
|
|
def async_get_options_flow(
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
) -> OptionsFlowHandler:
|
|
|
|
"""Get the options flow for this handler."""
|
|
|
|
return OptionsFlowHandler(config_entry)
|
|
|
|
|
|
|
|
|
|
|
|
class OptionsFlowHandler(OptionsFlow):
|
|
|
|
"""Handle the option flow for bluetooth."""
|
|
|
|
|
|
|
|
def __init__(self, config_entry: ConfigEntry) -> None:
|
|
|
|
"""Initialize options flow."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
|
|
|
|
async def async_step_init(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle options flow."""
|
|
|
|
if user_input is not None:
|
|
|
|
return self.async_create_entry(title="", data=user_input)
|
|
|
|
|
|
|
|
if not (adapters := await async_get_bluetooth_adapters()):
|
|
|
|
return self.async_abort(reason="no_adapters")
|
|
|
|
|
|
|
|
data_schema = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(
|
|
|
|
CONF_ADAPTER,
|
|
|
|
default=self.config_entry.options.get(CONF_ADAPTER, adapters[0]),
|
|
|
|
): vol.In(adapters),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return self.async_show_form(step_id="init", data_schema=data_schema)
|