mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Fix reauth for sonarr (#55329)
* fix reauth for sonarr * Update config_flow.py * Update config_flow.py * Update config_flow.py * Update test_config_flow.py * Update config_flow.py * Update test_config_flow.py * Update config_flow.py
This commit is contained in:
parent
e2dac31471
commit
819fd811af
@ -64,9 +64,7 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initialize the flow."""
|
"""Initialize the flow."""
|
||||||
self._reauth = False
|
self.entry = None
|
||||||
self._entry_id = None
|
|
||||||
self._entry_data = {}
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
@ -76,10 +74,7 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
async def async_step_reauth(self, data: dict[str, Any] | None = None) -> FlowResult:
|
async def async_step_reauth(self, data: dict[str, Any] | None = None) -> FlowResult:
|
||||||
"""Handle configuration by re-auth."""
|
"""Handle configuration by re-auth."""
|
||||||
self._reauth = True
|
self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
|
||||||
self._entry_data = dict(data)
|
|
||||||
entry = await self.async_set_unique_id(self.unique_id)
|
|
||||||
self._entry_id = entry.entry_id
|
|
||||||
|
|
||||||
return await self.async_step_reauth_confirm()
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
@ -90,7 +85,7 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
if user_input is None:
|
if user_input is None:
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="reauth_confirm",
|
step_id="reauth_confirm",
|
||||||
description_placeholders={"host": self._entry_data[CONF_HOST]},
|
description_placeholders={"host": self.entry.data[CONF_HOST]},
|
||||||
data_schema=vol.Schema({}),
|
data_schema=vol.Schema({}),
|
||||||
errors={},
|
errors={},
|
||||||
)
|
)
|
||||||
@ -104,8 +99,8 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
if self._reauth:
|
if self.entry:
|
||||||
user_input = {**self._entry_data, **user_input}
|
user_input = {**self.entry.data, **user_input}
|
||||||
|
|
||||||
if CONF_VERIFY_SSL not in user_input:
|
if CONF_VERIFY_SSL not in user_input:
|
||||||
user_input[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL
|
user_input[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL
|
||||||
@ -120,10 +115,8 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
_LOGGER.exception("Unexpected exception")
|
_LOGGER.exception("Unexpected exception")
|
||||||
return self.async_abort(reason="unknown")
|
return self.async_abort(reason="unknown")
|
||||||
else:
|
else:
|
||||||
if self._reauth:
|
if self.entry:
|
||||||
return await self._async_reauth_update_entry(
|
return await self._async_reauth_update_entry(user_input)
|
||||||
self._entry_id, user_input
|
|
||||||
)
|
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=user_input[CONF_HOST], data=user_input
|
title=user_input[CONF_HOST], data=user_input
|
||||||
@ -136,17 +129,16 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_reauth_update_entry(self, entry_id: str, data: dict) -> FlowResult:
|
async def _async_reauth_update_entry(self, data: dict) -> FlowResult:
|
||||||
"""Update existing config entry."""
|
"""Update existing config entry."""
|
||||||
entry = self.hass.config_entries.async_get_entry(entry_id)
|
self.hass.config_entries.async_update_entry(self.entry, data=data)
|
||||||
self.hass.config_entries.async_update_entry(entry, data=data)
|
await self.hass.config_entries.async_reload(self.entry.entry_id)
|
||||||
await self.hass.config_entries.async_reload(entry.entry_id)
|
|
||||||
|
|
||||||
return self.async_abort(reason="reauth_successful")
|
return self.async_abort(reason="reauth_successful")
|
||||||
|
|
||||||
def _get_user_data_schema(self) -> dict[str, Any]:
|
def _get_user_data_schema(self) -> dict[str, Any]:
|
||||||
"""Get the data schema to display user form."""
|
"""Get the data schema to display user form."""
|
||||||
if self._reauth:
|
if self.entry:
|
||||||
return {vol.Required(CONF_API_KEY): str}
|
return {vol.Required(CONF_API_KEY): str}
|
||||||
|
|
||||||
data_schema = {
|
data_schema = {
|
||||||
|
@ -100,14 +100,16 @@ async def test_full_reauth_flow_implementation(
|
|||||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the manual reauth flow from start to finish."""
|
"""Test the manual reauth flow from start to finish."""
|
||||||
entry = await setup_integration(
|
entry = await setup_integration(hass, aioclient_mock, skip_entry_setup=True)
|
||||||
hass, aioclient_mock, skip_entry_setup=True, unique_id="any"
|
|
||||||
)
|
|
||||||
assert entry
|
assert entry
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={CONF_SOURCE: SOURCE_REAUTH, "unique_id": entry.unique_id},
|
context={
|
||||||
|
CONF_SOURCE: SOURCE_REAUTH,
|
||||||
|
"entry_id": entry.entry_id,
|
||||||
|
"unique_id": entry.unique_id,
|
||||||
|
},
|
||||||
data=entry.data,
|
data=entry.data,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user