"""Config flow for Smart Home integration.""" from __future__ import annotations import logging from typing import Any import voluptuous as vol import aiohttp from homeassistant import config_entries from homeassistant.const import CONF_HOST, CONF_PORT, CONF_NAME from homeassistant.data_entry_flow import FlowResult import homeassistant.helpers.config_validation as cv from .const import DOMAIN, DEFAULT_PORT, STEP_USER _LOGGER = logging.getLogger(__name__) class SmartHomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for Smart Home.""" VERSION = 1 async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle the initial step.""" errors = {} if user_input is not None: try: # Test connection to API async with aiohttp.ClientSession() as session: url = f"http://{user_input[CONF_HOST]}:{user_input[CONF_PORT]}" async with session.get(f"{url}/") as response: if response.status == 200: return self.async_create_entry( title=user_input[CONF_NAME], data=user_input, ) else: errors["base"] = "cannot_connect" except aiohttp.ClientError: errors["base"] = "cannot_connect" except Exception: # pylint: disable=broad-except _LOGGER.exception("Unexpected exception") errors["base"] = "unknown" return self.async_show_form( step_id=STEP_USER, data_schema=vol.Schema( { vol.Required(CONF_NAME): str, vol.Required(CONF_HOST): str, vol.Required(CONF_PORT, default=DEFAULT_PORT): int, } ), errors=errors, )