mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Always reload after a successful reauth flow (#116026)
* Always reload after a succesfull reauth-flow * Add test, fix CI failures * Add kwarg to prevent reloading and tests * Do not reload entry for bond if it exists * Remove mocks on internals * Rename kwarg to always_reload * Update tests/components/weatherflow_cloud/test_config_flow.py * Update tests/components/homeworks/test_config_flow.py * Update tests/components/homeworks/test_config_flow.py * Rename to option to reload_even_if_entry_is_unchanged
This commit is contained in:
parent
350ca48d4c
commit
70b358bca1
@ -113,7 +113,10 @@ class BondConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
):
|
):
|
||||||
updates[CONF_ACCESS_TOKEN] = token
|
updates[CONF_ACCESS_TOKEN] = token
|
||||||
return self.async_update_reload_and_abort(
|
return self.async_update_reload_and_abort(
|
||||||
entry, data={**entry.data, **updates}, reason="already_configured"
|
entry,
|
||||||
|
data={**entry.data, **updates},
|
||||||
|
reason="already_configured",
|
||||||
|
reload_even_if_entry_is_unchanged=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
self._discovered = {CONF_HOST: host, CONF_NAME: bond_id}
|
self._discovered = {CONF_HOST: host, CONF_NAME: bond_id}
|
||||||
|
@ -690,7 +690,10 @@ class HomeworksConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
CONF_PORT: user_input[CONF_PORT],
|
CONF_PORT: user_input[CONF_PORT],
|
||||||
}
|
}
|
||||||
return self.async_update_reload_and_abort(
|
return self.async_update_reload_and_abort(
|
||||||
entry, options=new_options, reason="reconfigure_successful"
|
entry,
|
||||||
|
options=new_options,
|
||||||
|
reason="reconfigure_successful",
|
||||||
|
reload_even_if_entry_is_unchanged=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
|
@ -50,6 +50,7 @@ class WeatherFlowCloudConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
existing_entry,
|
existing_entry,
|
||||||
data={CONF_API_TOKEN: api_token},
|
data={CONF_API_TOKEN: api_token},
|
||||||
reason="reauth_successful",
|
reason="reauth_successful",
|
||||||
|
reload_even_if_entry_is_unchanged=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
|
@ -2399,6 +2399,7 @@ class ConfigFlow(ConfigEntryBaseFlow):
|
|||||||
data: Mapping[str, Any] | UndefinedType = UNDEFINED,
|
data: Mapping[str, Any] | UndefinedType = UNDEFINED,
|
||||||
options: Mapping[str, Any] | UndefinedType = UNDEFINED,
|
options: Mapping[str, Any] | UndefinedType = UNDEFINED,
|
||||||
reason: str = "reauth_successful",
|
reason: str = "reauth_successful",
|
||||||
|
reload_even_if_entry_is_unchanged: bool = True,
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Update config entry, reload config entry and finish config flow."""
|
"""Update config entry, reload config entry and finish config flow."""
|
||||||
result = self.hass.config_entries.async_update_entry(
|
result = self.hass.config_entries.async_update_entry(
|
||||||
@ -2408,7 +2409,7 @@ class ConfigFlow(ConfigEntryBaseFlow):
|
|||||||
data=data,
|
data=data,
|
||||||
options=options,
|
options=options,
|
||||||
)
|
)
|
||||||
if result:
|
if reload_even_if_entry_is_unchanged or result:
|
||||||
self.hass.config_entries.async_schedule_reload(entry.entry_id)
|
self.hass.config_entries.async_schedule_reload(entry.entry_id)
|
||||||
return self.async_abort(reason=reason)
|
return self.async_abort(reason=reason)
|
||||||
|
|
||||||
|
@ -4504,24 +4504,86 @@ def test_raise_trying_to_add_same_config_entry_twice(
|
|||||||
assert f"An entry with the id {entry.entry_id} already exists" in caplog.text
|
assert f"An entry with the id {entry.entry_id} already exists" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
(
|
||||||
|
"title",
|
||||||
|
"unique_id",
|
||||||
|
"data_vendor",
|
||||||
|
"options_vendor",
|
||||||
|
"kwargs",
|
||||||
|
"calls_entry_load_unload",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
("Test", "Updated title"),
|
||||||
|
("1234", "5678"),
|
||||||
|
("data", "data2"),
|
||||||
|
("options", "options2"),
|
||||||
|
{},
|
||||||
|
(2, 1),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
("Test", "Test"),
|
||||||
|
("1234", "1234"),
|
||||||
|
("data", "data"),
|
||||||
|
("options", "options"),
|
||||||
|
{},
|
||||||
|
(2, 1),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
("Test", "Updated title"),
|
||||||
|
("1234", "5678"),
|
||||||
|
("data", "data2"),
|
||||||
|
("options", "options2"),
|
||||||
|
{"reload_even_if_entry_is_unchanged": True},
|
||||||
|
(2, 1),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
("Test", "Test"),
|
||||||
|
("1234", "1234"),
|
||||||
|
("data", "data"),
|
||||||
|
("options", "options"),
|
||||||
|
{"reload_even_if_entry_is_unchanged": False},
|
||||||
|
(1, 0),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
ids=[
|
||||||
|
"changed_entry_default",
|
||||||
|
"unchanged_entry_default",
|
||||||
|
"changed_entry_explicit_reload",
|
||||||
|
"changed_entry_no_reload",
|
||||||
|
],
|
||||||
|
)
|
||||||
async def test_update_entry_and_reload(
|
async def test_update_entry_and_reload(
|
||||||
hass: HomeAssistant, manager: config_entries.ConfigEntries
|
hass: HomeAssistant,
|
||||||
|
manager: config_entries.ConfigEntries,
|
||||||
|
title: tuple[str, str],
|
||||||
|
unique_id: tuple[str, str],
|
||||||
|
data_vendor: tuple[str, str],
|
||||||
|
options_vendor: tuple[str, str],
|
||||||
|
kwargs: dict[str, Any],
|
||||||
|
calls_entry_load_unload: tuple[int, int],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test updating an entry and reloading."""
|
"""Test updating an entry and reloading."""
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain="comp",
|
domain="comp",
|
||||||
unique_id="1234",
|
unique_id=unique_id[0],
|
||||||
title="Test",
|
title=title[0],
|
||||||
data={"vendor": "data"},
|
data={"vendor": data_vendor[0]},
|
||||||
options={"vendor": "options"},
|
options={"vendor": options_vendor[0]},
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
mock_integration(
|
comp = MockModule(
|
||||||
hass, MockModule("comp", async_setup_entry=AsyncMock(return_value=True))
|
"comp",
|
||||||
|
async_setup_entry=AsyncMock(return_value=True),
|
||||||
|
async_unload_entry=AsyncMock(return_value=True),
|
||||||
)
|
)
|
||||||
|
mock_integration(hass, comp)
|
||||||
mock_platform(hass, "comp.config_flow", None)
|
mock_platform(hass, "comp.config_flow", None)
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
|
||||||
class MockFlowHandler(config_entries.ConfigFlow):
|
class MockFlowHandler(config_entries.ConfigFlow):
|
||||||
"""Define a mock flow handler."""
|
"""Define a mock flow handler."""
|
||||||
|
|
||||||
@ -4531,23 +4593,27 @@ async def test_update_entry_and_reload(
|
|||||||
"""Mock Reauth."""
|
"""Mock Reauth."""
|
||||||
return self.async_update_reload_and_abort(
|
return self.async_update_reload_and_abort(
|
||||||
entry=entry,
|
entry=entry,
|
||||||
unique_id="5678",
|
unique_id=unique_id[1],
|
||||||
title="Updated Title",
|
title=title[1],
|
||||||
data={"vendor": "data2"},
|
data={"vendor": data_vendor[1]},
|
||||||
options={"vendor": "options2"},
|
options={"vendor": options_vendor[1]},
|
||||||
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch.dict(config_entries.HANDLERS, {"comp": MockFlowHandler}):
|
with patch.dict(config_entries.HANDLERS, {"comp": MockFlowHandler}):
|
||||||
task = await manager.flow.async_init("comp", context={"source": "reauth"})
|
task = await manager.flow.async_init("comp", context={"source": "reauth"})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert entry.title == "Updated Title"
|
assert entry.title == title[1]
|
||||||
assert entry.unique_id == "5678"
|
assert entry.unique_id == unique_id[1]
|
||||||
assert entry.data == {"vendor": "data2"}
|
assert entry.data == {"vendor": data_vendor[1]}
|
||||||
assert entry.options == {"vendor": "options2"}
|
assert entry.options == {"vendor": options_vendor[1]}
|
||||||
assert entry.state == config_entries.ConfigEntryState.LOADED
|
assert entry.state == config_entries.ConfigEntryState.LOADED
|
||||||
assert task["type"] == FlowResultType.ABORT
|
assert task["type"] == FlowResultType.ABORT
|
||||||
assert task["reason"] == "reauth_successful"
|
assert task["reason"] == "reauth_successful"
|
||||||
|
# Assert entry was reloaded
|
||||||
|
assert len(comp.async_setup_entry.mock_calls) == calls_entry_load_unload[0]
|
||||||
|
assert len(comp.async_unload_entry.mock_calls) == calls_entry_load_unload[1]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("unique_id", [["blah", "bleh"], {"key": "value"}])
|
@pytest.mark.parametrize("unique_id", [["blah", "bleh"], {"key": "value"}])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user