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:
Chris Talkington 2021-08-27 11:04:07 -05:00 committed by GitHub
parent e2dac31471
commit 819fd811af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 23 deletions

View File

@ -64,9 +64,7 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
def __init__(self):
"""Initialize the flow."""
self._reauth = False
self._entry_id = None
self._entry_data = {}
self.entry = None
@staticmethod
@callback
@ -76,10 +74,7 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
async def async_step_reauth(self, data: dict[str, Any] | None = None) -> FlowResult:
"""Handle configuration by re-auth."""
self._reauth = True
self._entry_data = dict(data)
entry = await self.async_set_unique_id(self.unique_id)
self._entry_id = entry.entry_id
self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
return await self.async_step_reauth_confirm()
@ -90,7 +85,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={"host": self.entry.data[CONF_HOST]},
data_schema=vol.Schema({}),
errors={},
)
@ -104,8 +99,8 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
errors = {}
if user_input is not None:
if self._reauth:
user_input = {**self._entry_data, **user_input}
if self.entry:
user_input = {**self.entry.data, **user_input}
if CONF_VERIFY_SSL not in user_input:
user_input[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL
@ -120,10 +115,8 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
_LOGGER.exception("Unexpected exception")
return self.async_abort(reason="unknown")
else:
if self._reauth:
return await self._async_reauth_update_entry(
self._entry_id, user_input
)
if self.entry:
return await self._async_reauth_update_entry(user_input)
return self.async_create_entry(
title=user_input[CONF_HOST], data=user_input
@ -136,17 +129,16 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
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."""
entry = self.hass.config_entries.async_get_entry(entry_id)
self.hass.config_entries.async_update_entry(entry, data=data)
await self.hass.config_entries.async_reload(entry.entry_id)
self.hass.config_entries.async_update_entry(self.entry, data=data)
await self.hass.config_entries.async_reload(self.entry.entry_id)
return self.async_abort(reason="reauth_successful")
def _get_user_data_schema(self) -> dict[str, Any]:
"""Get the data schema to display user form."""
if self._reauth:
if self.entry:
return {vol.Required(CONF_API_KEY): str}
data_schema = {

View File

@ -100,14 +100,16 @@ async def test_full_reauth_flow_implementation(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the manual reauth flow from start to finish."""
entry = await setup_integration(
hass, aioclient_mock, skip_entry_setup=True, unique_id="any"
)
entry = await setup_integration(hass, aioclient_mock, skip_entry_setup=True)
assert entry
result = await hass.config_entries.flow.async_init(
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,
)