Some checks failed
Code Quality Check / Code Formatting (push) Failing after 21s
Code Quality Check / Security Analysis (push) Failing after 20s
Integration Testing / Integration Tests (2024.12.0, 3.13) (push) Failing after 1m32s
Integration Testing / Integration Tests (2025.9.4, 3.13) (push) Failing after 20s
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
94 lines
3.2 KiB
Python
94 lines
3.2 KiB
Python
"""Config flow for AdGuard Control Hub integration."""
|
|
import asyncio
|
|
import logging
|
|
from typing import Any, Dict, Optional
|
|
|
|
import voluptuous as vol
|
|
from homeassistant import config_entries
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from .api import AdGuardHomeAPI, AdGuardConnectionError, AdGuardAuthError, AdGuardTimeoutError
|
|
from .const import (
|
|
CONF_SSL,
|
|
CONF_VERIFY_SSL,
|
|
DEFAULT_PORT,
|
|
DEFAULT_SSL,
|
|
DEFAULT_VERIFY_SSL,
|
|
DOMAIN,
|
|
)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
STEP_USER_DATA_SCHEMA = vol.Schema({
|
|
vol.Required(CONF_HOST): cv.string,
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
vol.Optional(CONF_USERNAME): cv.string,
|
|
vol.Optional(CONF_PASSWORD): cv.string,
|
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
|
})
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for AdGuard Control Hub."""
|
|
|
|
VERSION = 1
|
|
MINOR_VERSION = 1
|
|
|
|
async def async_step_user(
|
|
self, user_input: Optional[Dict[str, Any]] = None
|
|
) -> FlowResult:
|
|
"""Handle the initial step."""
|
|
errors: Dict[str, str] = {}
|
|
|
|
if user_input is not None:
|
|
try:
|
|
# Basic validation
|
|
host = user_input[CONF_HOST].strip()
|
|
if not host:
|
|
errors[CONF_HOST] = "invalid_host"
|
|
|
|
# Test connection
|
|
session = async_get_clientsession(self.hass, user_input.get(CONF_VERIFY_SSL, True))
|
|
api = AdGuardHomeAPI(
|
|
host=host,
|
|
port=user_input[CONF_PORT],
|
|
username=user_input.get(CONF_USERNAME),
|
|
password=user_input.get(CONF_PASSWORD),
|
|
ssl=user_input.get(CONF_SSL, False),
|
|
verify_ssl=user_input.get(CONF_VERIFY_SSL, True),
|
|
session=session,
|
|
timeout=10,
|
|
)
|
|
|
|
if not await api.test_connection():
|
|
errors["base"] = "cannot_connect"
|
|
else:
|
|
unique_id = f"{host}:{user_input[CONF_PORT]}"
|
|
await self.async_set_unique_id(unique_id)
|
|
self._abort_if_unique_id_configured()
|
|
|
|
return self.async_create_entry(
|
|
title=f"AdGuard Control Hub ({host})",
|
|
data=user_input,
|
|
)
|
|
|
|
except AdGuardAuthError:
|
|
errors["base"] = "invalid_auth"
|
|
except AdGuardTimeoutError:
|
|
errors["base"] = "timeout"
|
|
except AdGuardConnectionError:
|
|
errors["base"] = "cannot_connect"
|
|
except Exception:
|
|
_LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "unknown"
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=STEP_USER_DATA_SCHEMA,
|
|
errors=errors,
|
|
)
|