Add re-authentication for transmission (#73124)

* Add reauth flow to transmission

* fix async_setup

* add strings

* fix test coverage
This commit is contained in:
Rami Mosleh
2022-06-20 17:09:58 +03:00
committed by GitHub
parent 3824703a64
commit 81e3ed790d
6 changed files with 168 additions and 12 deletions

View File

@@ -1,6 +1,9 @@
"""Config flow for Transmission Bittorent Client."""
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
import voluptuous as vol
from homeassistant import config_entries
@@ -13,6 +16,7 @@ from homeassistant.const import (
CONF_USERNAME,
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from . import get_api
from .const import (
@@ -43,6 +47,7 @@ class TransmissionFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle Tansmission config flow."""
VERSION = 1
_reauth_entry: config_entries.ConfigEntry | None
@staticmethod
@callback
@@ -87,6 +92,48 @@ class TransmissionFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
errors=errors,
)
async def async_step_reauth(self, data: Mapping[str, Any]) -> FlowResult:
"""Perform reauth upon an API authentication error."""
self._reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, str] | None = None
) -> FlowResult:
"""Confirm reauth dialog."""
errors = {}
assert self._reauth_entry
if user_input is not None:
user_input = {**self._reauth_entry.data, **user_input}
try:
await get_api(self.hass, user_input)
except AuthenticationError:
errors[CONF_PASSWORD] = "invalid_auth"
except (CannotConnect, UnknownError):
errors["base"] = "cannot_connect"
else:
self.hass.config_entries.async_update_entry(
self._reauth_entry, data=user_input
)
await self.hass.config_entries.async_reload(self._reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful")
return self.async_show_form(
description_placeholders={
CONF_USERNAME: self._reauth_entry.data[CONF_USERNAME]
},
step_id="reauth_confirm",
data_schema=vol.Schema(
{
vol.Required(CONF_PASSWORD): str,
}
),
errors=errors,
)
class TransmissionOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle Transmission client options."""