Some checks failed
🧪 Integration Testing / 🔧 Test Integration (2023.12.0, 3.11) (push) Successful in 2m11s
🧪 Integration Testing / 🔧 Test Integration (2023.12.0, 3.12) (push) Successful in 2m2s
🧪 Integration Testing / 🔧 Test Integration (2024.1.0, 3.11) (push) Successful in 1m4s
🧪 Integration Testing / 🔧 Test Integration (2024.1.0, 3.12) (push) Successful in 1m19s
🛡️ Code Quality & Security Check / 🔍 Code Quality Analysis (push) Failing after 56s
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
86 lines
2.9 KiB
Python
86 lines
2.9 KiB
Python
"""Config flow for AdGuard Control Hub integration."""
|
|
import logging
|
|
from typing import Any
|
|
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 .api import AdGuardHomeAPI
|
|
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): str,
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
|
|
vol.Optional(CONF_USERNAME): str,
|
|
vol.Optional(CONF_PASSWORD): str,
|
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): bool,
|
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): bool,
|
|
})
|
|
|
|
async def validate_input(hass, data: dict) -> dict:
|
|
"""Validate the user input allows us to connect."""
|
|
session = async_get_clientsession(hass, data.get(CONF_VERIFY_SSL, True))
|
|
|
|
api = AdGuardHomeAPI(
|
|
host=data[CONF_HOST],
|
|
port=data[CONF_PORT],
|
|
username=data.get(CONF_USERNAME),
|
|
password=data.get(CONF_PASSWORD),
|
|
ssl=data.get(CONF_SSL, False),
|
|
session=session,
|
|
)
|
|
|
|
# Test the connection
|
|
if not await api.test_connection():
|
|
raise CannotConnect
|
|
|
|
# Get server info
|
|
try:
|
|
status = await api.get_status()
|
|
version = status.get("version", "unknown")
|
|
return {
|
|
"title": f"AdGuard Control Hub ({data[CONF_HOST]})",
|
|
"version": version
|
|
}
|
|
except Exception as err:
|
|
_LOGGER.exception("Unexpected exception: %s", err)
|
|
raise CannotConnect from err
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for AdGuard Control Hub."""
|
|
|
|
VERSION = 1
|
|
|
|
async def async_step_user(self, user_input: dict[str, Any] | None = None):
|
|
"""Handle the initial step."""
|
|
errors: dict[str, str] = {}
|
|
|
|
if user_input is not None:
|
|
try:
|
|
info = await validate_input(self.hass, user_input)
|
|
except CannotConnect:
|
|
errors["base"] = "cannot_connect"
|
|
except Exception:
|
|
_LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "unknown"
|
|
else:
|
|
# Create unique ID based on host and port
|
|
unique_id = f"{user_input[CONF_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=info["title"],
|
|
data=user_input,
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=STEP_USER_DATA_SCHEMA,
|
|
errors=errors,
|
|
)
|
|
|
|
class CannotConnect(Exception):
|
|
"""Error to indicate we cannot connect.""" |