mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 23:27:37 +00:00
Add extended class for OptionsFlow that automatically reloads (#146910)
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
parent
f90e06fde1
commit
6f59aaebdd
@ -3491,7 +3491,22 @@ class OptionsFlowManager(
|
|||||||
entry = self.hass.config_entries.async_get_known_entry(flow.handler)
|
entry = self.hass.config_entries.async_get_known_entry(flow.handler)
|
||||||
|
|
||||||
if result["data"] is not None:
|
if result["data"] is not None:
|
||||||
self.hass.config_entries.async_update_entry(entry, options=result["data"])
|
automatic_reload = False
|
||||||
|
if isinstance(flow, OptionsFlowWithReload):
|
||||||
|
automatic_reload = flow.automatic_reload
|
||||||
|
|
||||||
|
if automatic_reload and entry.update_listeners:
|
||||||
|
raise ValueError(
|
||||||
|
"Config entry update listeners should not be used with OptionsFlowWithReload"
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
self.hass.config_entries.async_update_entry(
|
||||||
|
entry, options=result["data"]
|
||||||
|
)
|
||||||
|
and automatic_reload is True
|
||||||
|
):
|
||||||
|
self.hass.config_entries.async_schedule_reload(entry.entry_id)
|
||||||
|
|
||||||
result["result"] = True
|
result["result"] = True
|
||||||
return result
|
return result
|
||||||
@ -3600,6 +3615,18 @@ class OptionsFlowWithConfigEntry(OptionsFlow):
|
|||||||
return self._options
|
return self._options
|
||||||
|
|
||||||
|
|
||||||
|
class OptionsFlowWithReload(OptionsFlow):
|
||||||
|
"""Automatic reloading class for config options flows.
|
||||||
|
|
||||||
|
Triggers an automatic reload of the config entry when the flow ends with
|
||||||
|
calling `async_create_entry` with changed options.
|
||||||
|
It's not allowed to use this class if the integration uses config entry
|
||||||
|
update listeners.
|
||||||
|
"""
|
||||||
|
|
||||||
|
automatic_reload: bool = True
|
||||||
|
|
||||||
|
|
||||||
class EntityRegistryDisabledHandler:
|
class EntityRegistryDisabledHandler:
|
||||||
"""Handler when entities related to config entries updated disabled_by."""
|
"""Handler when entities related to config entries updated disabled_by."""
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ from freezegun import freeze_time
|
|||||||
from freezegun.api import FrozenDateTimeFactory
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow, loader
|
from homeassistant import config_entries, data_entry_flow, loader
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -8656,6 +8657,95 @@ async def test_options_flow_config_entry(
|
|||||||
assert result["reason"] == "abort"
|
assert result["reason"] == "abort"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
(
|
||||||
|
"option_flow_base_class",
|
||||||
|
"number_of_update_listeners",
|
||||||
|
"expected_configure_result",
|
||||||
|
"expected_number_of_unloads",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
(config_entries.OptionsFlow, 0, does_not_raise(), 0),
|
||||||
|
(config_entries.OptionsFlowWithReload, 0, does_not_raise(), 1),
|
||||||
|
(config_entries.OptionsFlow, 1, does_not_raise(), 0),
|
||||||
|
(
|
||||||
|
config_entries.OptionsFlowWithReload,
|
||||||
|
1,
|
||||||
|
pytest.raises(
|
||||||
|
ValueError,
|
||||||
|
match="Config entry update listeners should not be used with OptionsFlowWithReload",
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_options_flow_automatic_reload(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
manager: config_entries.ConfigEntries,
|
||||||
|
option_flow_base_class: type[config_entries.OptionsFlow],
|
||||||
|
number_of_update_listeners: int,
|
||||||
|
expected_configure_result: AbstractContextManager,
|
||||||
|
expected_number_of_unloads: int,
|
||||||
|
) -> None:
|
||||||
|
"""Test options flow with automatic reload when updated."""
|
||||||
|
original_entry = MockConfigEntry(
|
||||||
|
domain="test", title="Test", data={}, options={"test": "first"}
|
||||||
|
)
|
||||||
|
original_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
"""Mock setup entry."""
|
||||||
|
for _ in range(number_of_update_listeners):
|
||||||
|
entry.add_update_listener(Mock())
|
||||||
|
return True
|
||||||
|
|
||||||
|
unload_entry_mock = AsyncMock(return_value=True)
|
||||||
|
|
||||||
|
mock_integration(
|
||||||
|
hass,
|
||||||
|
MockModule(
|
||||||
|
"test",
|
||||||
|
async_setup_entry=async_setup_entry,
|
||||||
|
async_unload_entry=unload_entry_mock,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
mock_platform(hass, "test.config_flow", None)
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(original_entry.entry_id)
|
||||||
|
assert original_entry.state is config_entries.ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
class TestFlow(config_entries.ConfigFlow):
|
||||||
|
"""Test flow."""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@callback
|
||||||
|
def async_get_options_flow(config_entry):
|
||||||
|
"""Test options flow."""
|
||||||
|
|
||||||
|
class _OptionsFlow(option_flow_base_class):
|
||||||
|
"""Test flow."""
|
||||||
|
|
||||||
|
async def async_step_init(self, user_input=None):
|
||||||
|
"""Test user step."""
|
||||||
|
if user_input is not None:
|
||||||
|
return self.async_create_entry(data=user_input)
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="init", data_schema=vol.Schema({"test": str})
|
||||||
|
)
|
||||||
|
|
||||||
|
return _OptionsFlow()
|
||||||
|
|
||||||
|
with mock_config_flow("test", TestFlow):
|
||||||
|
result = await hass.config_entries.options.async_init(original_entry.entry_id)
|
||||||
|
with expected_configure_result:
|
||||||
|
await hass.config_entries.options.async_configure(
|
||||||
|
result["flow_id"], {"test": "updated"}
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
|
||||||
|
assert len(unload_entry_mock.mock_calls) == expected_number_of_unloads
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("integration_frame_path", ["custom_components/my_integration"])
|
@pytest.mark.parametrize("integration_frame_path", ["custom_components/my_integration"])
|
||||||
@pytest.mark.usefixtures("mock_integration_frame")
|
@pytest.mark.usefixtures("mock_integration_frame")
|
||||||
async def test_options_flow_deprecated_config_entry_setter(
|
async def test_options_flow_deprecated_config_entry_setter(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user