Use aiopyarr for sonarr (#65349)
This commit is contained in:
parent
c14912471d
commit
f30681dae7
20 changed files with 464 additions and 345 deletions
|
@ -4,28 +4,21 @@ from __future__ import annotations
|
|||
import logging
|
||||
from typing import Any
|
||||
|
||||
from sonarr import Sonarr, SonarrAccessRestricted, SonarrError
|
||||
from aiopyarr import ArrAuthenticationException, ArrException
|
||||
from aiopyarr.models.host_configuration import PyArrHostConfiguration
|
||||
from aiopyarr.sonarr_client import SonarrClient
|
||||
import voluptuous as vol
|
||||
import yarl
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, OptionsFlow
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
CONF_SSL,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import (
|
||||
CONF_BASE_PATH,
|
||||
CONF_UPCOMING_DAYS,
|
||||
CONF_WANTED_MAX_ITEMS,
|
||||
DEFAULT_BASE_PATH,
|
||||
DEFAULT_PORT,
|
||||
DEFAULT_SSL,
|
||||
DEFAULT_UPCOMING_DAYS,
|
||||
DEFAULT_VERIFY_SSL,
|
||||
DEFAULT_WANTED_MAX_ITEMS,
|
||||
|
@ -40,25 +33,24 @@ async def validate_input(hass: HomeAssistant, data: dict) -> None:
|
|||
|
||||
Data has the keys from DATA_SCHEMA with values provided by the user.
|
||||
"""
|
||||
session = async_get_clientsession(hass)
|
||||
|
||||
sonarr = Sonarr(
|
||||
host=data[CONF_HOST],
|
||||
port=data[CONF_PORT],
|
||||
api_key=data[CONF_API_KEY],
|
||||
base_path=data[CONF_BASE_PATH],
|
||||
tls=data[CONF_SSL],
|
||||
host_configuration = PyArrHostConfiguration(
|
||||
api_token=data[CONF_API_KEY],
|
||||
url=data[CONF_URL],
|
||||
verify_ssl=data[CONF_VERIFY_SSL],
|
||||
session=session,
|
||||
)
|
||||
|
||||
await sonarr.update()
|
||||
sonarr = SonarrClient(
|
||||
host_configuration=host_configuration,
|
||||
session=async_get_clientsession(hass),
|
||||
)
|
||||
|
||||
await sonarr.async_get_system_status()
|
||||
|
||||
|
||||
class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Sonarr."""
|
||||
|
||||
VERSION = 1
|
||||
VERSION = 2
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the flow."""
|
||||
|
@ -83,7 +75,7 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||
if user_input is None:
|
||||
return self.async_show_form(
|
||||
step_id="reauth_confirm",
|
||||
description_placeholders={"host": self.entry.data[CONF_HOST]},
|
||||
description_placeholders={"url": self.entry.data[CONF_URL]},
|
||||
data_schema=vol.Schema({}),
|
||||
errors={},
|
||||
)
|
||||
|
@ -105,9 +97,9 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||
|
||||
try:
|
||||
await validate_input(self.hass, user_input)
|
||||
except SonarrAccessRestricted:
|
||||
except ArrAuthenticationException:
|
||||
errors = {"base": "invalid_auth"}
|
||||
except SonarrError:
|
||||
except ArrException:
|
||||
errors = {"base": "cannot_connect"}
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
|
@ -116,8 +108,10 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||
if self.entry:
|
||||
return await self._async_reauth_update_entry(user_input)
|
||||
|
||||
parsed = yarl.URL(user_input[CONF_URL])
|
||||
|
||||
return self.async_create_entry(
|
||||
title=user_input[CONF_HOST], data=user_input
|
||||
title=parsed.host or "Sonarr", data=user_input
|
||||
)
|
||||
|
||||
data_schema = self._get_user_data_schema()
|
||||
|
@ -139,12 +133,9 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||
if self.entry:
|
||||
return {vol.Required(CONF_API_KEY): str}
|
||||
|
||||
data_schema = {
|
||||
vol.Required(CONF_HOST): str,
|
||||
data_schema: dict[str, Any] = {
|
||||
vol.Required(CONF_URL): str,
|
||||
vol.Required(CONF_API_KEY): str,
|
||||
vol.Optional(CONF_BASE_PATH, default=DEFAULT_BASE_PATH): str,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
|
||||
vol.Optional(CONF_SSL, default=DEFAULT_SSL): bool,
|
||||
}
|
||||
|
||||
if self.show_advanced_options:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue