core/homeassistant/components/lidarr/config_flow.py

108 lines
3.6 KiB
Python
Raw Normal View History

2022-09-20 15:51:29 +00:00
"""Config flow for Lidarr."""
2022-09-20 15:51:29 +00:00
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
from aiohttp import ClientConnectorError
2022-09-28 06:06:24 +00:00
from aiopyarr import exceptions
2022-09-20 15:51:29 +00:00
from aiopyarr.lidarr_client import LidarrClient
import voluptuous as vol
2024-10-18 15:22:14 +00:00
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
2022-09-20 15:51:29 +00:00
from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DEFAULT_NAME, DOMAIN
class LidarrConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Lidarr."""
VERSION = 1
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
2022-09-20 15:51:29 +00:00
"""Handle configuration by re-auth."""
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, str] | None = None
) -> ConfigFlowResult:
2022-09-20 15:51:29 +00:00
"""Confirm reauth dialog."""
if user_input is not None:
return await self.async_step_user()
self._set_confirm_only()
return self.async_show_form(step_id="reauth_confirm")
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
2022-09-20 15:51:29 +00:00
"""Handle a flow initiated by the user."""
errors = {}
2024-10-18 15:22:14 +00:00
if user_input is not None:
2022-09-20 15:51:29 +00:00
try:
2022-09-28 06:06:24 +00:00
if result := await validate_input(self.hass, user_input):
2022-09-20 15:51:29 +00:00
user_input[CONF_API_KEY] = result[1]
except exceptions.ArrAuthenticationException:
errors = {"base": "invalid_auth"}
except (ClientConnectorError, exceptions.ArrConnectionException):
errors = {"base": "cannot_connect"}
2022-09-28 06:06:24 +00:00
except exceptions.ArrWrongAppException:
errors = {"base": "wrong_app"}
except exceptions.ArrZeroConfException:
errors = {"base": "zeroconf_failed"}
2022-09-20 15:51:29 +00:00
except exceptions.ArrException:
errors = {"base": "unknown"}
if not errors:
2024-10-18 15:22:14 +00:00
if self.source == SOURCE_REAUTH:
return self.async_update_reload_and_abort(
self._get_reauth_entry(), data=user_input
2022-09-20 15:51:29 +00:00
)
return self.async_create_entry(title=DEFAULT_NAME, data=user_input)
2024-10-18 15:22:14 +00:00
if user_input is None:
user_input = {}
if self.source == SOURCE_REAUTH:
user_input = dict(self._get_reauth_entry().data)
2022-09-20 15:51:29 +00:00
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_URL, default=user_input.get(CONF_URL, "")): str,
vol.Optional(CONF_API_KEY): str,
vol.Optional(
CONF_VERIFY_SSL,
default=user_input.get(CONF_VERIFY_SSL, False),
): bool,
}
),
errors=errors,
)
async def validate_input(
hass: HomeAssistant, data: dict[str, Any]
2022-09-28 06:06:24 +00:00
) -> tuple[str, str, str] | None:
2022-09-20 15:51:29 +00:00
"""Validate the user input allows us to connect.
Data has the keys from DATA_SCHEMA with values provided by the user.
"""
lidarr = LidarrClient(
api_token=data.get(CONF_API_KEY, ""),
url=data[CONF_URL],
session=async_get_clientsession(hass),
verify_ssl=data[CONF_VERIFY_SSL],
)
if CONF_API_KEY not in data:
return await lidarr.async_try_zeroconf()
2022-09-28 06:06:24 +00:00
await lidarr.async_get_system_status()
return None