core/homeassistant/components/qbittorrent/config_flow.py

57 lines
1.9 KiB
Python

"""Config flow for qBittorrent."""
from __future__ import annotations
import logging
from typing import Any
from qbittorrentapi import APIConnectionError, Forbidden403Error, LoginFailed
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL
from .const import DEFAULT_NAME, DEFAULT_URL, DOMAIN
from .helpers import setup_client
_LOGGER = logging.getLogger(__name__)
USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_URL, default=DEFAULT_URL): str,
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Optional(CONF_VERIFY_SSL, default=True): bool,
}
)
class QbittorrentConfigFlow(ConfigFlow, domain=DOMAIN):
"""Config flow for the qBittorrent integration."""
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a user-initiated config flow."""
errors = {}
if user_input is not None:
self._async_abort_entries_match({CONF_URL: user_input[CONF_URL]})
try:
await self.hass.async_add_executor_job(
setup_client,
user_input[CONF_URL],
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
user_input[CONF_VERIFY_SSL],
)
except (LoginFailed, Forbidden403Error):
errors = {"base": "invalid_auth"}
except APIConnectionError:
errors = {"base": "cannot_connect"}
else:
return self.async_create_entry(title=DEFAULT_NAME, data=user_input)
schema = self.add_suggested_values_to_schema(USER_DATA_SCHEMA, user_input)
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)