@@ -34,25 +34,20 @@ STEP_USER_DATA_SCHEMA = vol.Schema({
|
||||
|
||||
async def validate_input(hass, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Validate the user input allows us to connect."""
|
||||
# Normalize host
|
||||
host = data[CONF_HOST].strip()
|
||||
if not host:
|
||||
raise InvalidHost("Host cannot be empty")
|
||||
|
||||
# Remove protocol if provided
|
||||
if host.startswith(("http://", "https://")):
|
||||
host = host.split("://", 1)[1]
|
||||
data[CONF_HOST] = host
|
||||
|
||||
# Validate port
|
||||
port = data[CONF_PORT]
|
||||
if not (1 <= port <= 65535):
|
||||
raise InvalidPort("Port must be between 1 and 65535")
|
||||
|
||||
# Create session with appropriate SSL settings
|
||||
session = async_get_clientsession(hass, data.get(CONF_VERIFY_SSL, True))
|
||||
|
||||
# Create API instance
|
||||
api = AdGuardHomeAPI(
|
||||
host=host,
|
||||
port=port,
|
||||
@@ -60,48 +55,38 @@ async def validate_input(hass, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
password=data.get(CONF_PASSWORD),
|
||||
ssl=data.get(CONF_SSL, False),
|
||||
session=session,
|
||||
timeout=10, # 10 second timeout for setup
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
# Test the connection
|
||||
try:
|
||||
if not await api.test_connection():
|
||||
raise CannotConnect("Failed to connect to AdGuard Home")
|
||||
|
||||
# Get additional server info if possible
|
||||
try:
|
||||
status = await api.get_status()
|
||||
version = status.get("version", "unknown")
|
||||
dns_port = status.get("dns_port", "N/A")
|
||||
|
||||
return {
|
||||
"title": f"AdGuard Control Hub ({host})",
|
||||
"version": version,
|
||||
"dns_port": dns_port,
|
||||
"host": host,
|
||||
}
|
||||
except Exception as err:
|
||||
_LOGGER.warning("Could not get server status, but connection works: %s", err)
|
||||
except Exception:
|
||||
return {
|
||||
"title": f"AdGuard Control Hub ({host})",
|
||||
"version": "unknown",
|
||||
"dns_port": "N/A",
|
||||
"host": host,
|
||||
}
|
||||
|
||||
except AdGuardAuthError as err:
|
||||
_LOGGER.error("Authentication failed: %s", err)
|
||||
raise InvalidAuth from err
|
||||
except AdGuardConnectionError as err:
|
||||
_LOGGER.error("Connection failed: %s", err)
|
||||
if "timeout" in str(err).lower():
|
||||
raise Timeout from err
|
||||
raise CannotConnect from err
|
||||
except asyncio.TimeoutError as err:
|
||||
_LOGGER.error("Connection timeout: %s", err)
|
||||
raise Timeout from err
|
||||
except Exception as err:
|
||||
_LOGGER.exception("Unexpected error during validation: %s", err)
|
||||
raise CannotConnect from err
|
||||
|
||||
|
||||
@@ -121,7 +106,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
try:
|
||||
info = await validate_input(self.hass, user_input)
|
||||
|
||||
# Create unique ID based on host and port
|
||||
unique_id = f"{info['host']}:{user_input[CONF_PORT]}"
|
||||
await self.async_set_unique_id(unique_id)
|
||||
self._abort_if_unique_id_configured()
|
||||
@@ -142,7 +126,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
except Timeout:
|
||||
errors["base"] = "timeout"
|
||||
except Exception:
|
||||
_LOGGER.exception("Unexpected exception during config flow")
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
|
||||
return self.async_show_form(
|
||||
@@ -151,48 +135,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_import(self, import_info: Dict[str, Any]) -> FlowResult:
|
||||
"""Handle configuration import."""
|
||||
return await self.async_step_user(import_info)
|
||||
|
||||
@staticmethod
|
||||
def async_get_options_flow(config_entry):
|
||||
"""Get the options flow for this handler."""
|
||||
return OptionsFlowHandler(config_entry)
|
||||
|
||||
|
||||
class OptionsFlowHandler(config_entries.OptionsFlow):
|
||||
"""Handle options flow for AdGuard Control Hub."""
|
||||
|
||||
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
||||
"""Initialize options flow."""
|
||||
self.config_entry = config_entry
|
||||
|
||||
async def async_step_init(
|
||||
self, user_input: Optional[Dict[str, Any]] = None
|
||||
) -> FlowResult:
|
||||
"""Handle options flow."""
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(title="", data=user_input)
|
||||
|
||||
options_schema = vol.Schema({
|
||||
vol.Optional(
|
||||
"scan_interval",
|
||||
default=self.config_entry.options.get("scan_interval", 30),
|
||||
): vol.All(vol.Coerce(int), vol.Range(min=10, max=300)),
|
||||
vol.Optional(
|
||||
"timeout",
|
||||
default=self.config_entry.options.get("timeout", 10),
|
||||
): vol.All(vol.Coerce(int), vol.Range(min=5, max=60)),
|
||||
})
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=options_schema,
|
||||
)
|
||||
|
||||
|
||||
# Custom exceptions
|
||||
class CannotConnect(Exception):
|
||||
"""Error to indicate we cannot connect."""
|
||||
|
||||
|
Reference in New Issue
Block a user