From 020f4670e3bb66c513a647f05150f51aca35d5c8 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sun, 22 Jun 2025 11:14:14 +0000 Subject: [PATCH] Mods --- homeassistant/config_entries.py | 11 +++--- tests/test_config_entries.py | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 89f4429b031..2058333dabd 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -3485,13 +3485,14 @@ class OptionsFlowManager( entry = self.hass.config_entries.async_get_known_entry(flow.handler) if result["data"] is not None: + entry_updated = self.hass.config_entries.async_update_entry( + entry, options=result["data"] + ) if ( - self.hass.config_entries.async_update_entry( - entry, options=result["data"] - ) + entry_updated is True + and hasattr(flow, "automatic_reload") is True + and flow.automatic_reload is True # type: ignore[attr-defined] and not entry.update_listeners - and hasattr(flow, "automatic_reload") - and flow.automatic_reload ): self.hass.config_entries.async_schedule_reload(entry.entry_id) diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 55b8434160e..d7130ccfe1a 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -15,6 +15,7 @@ from freezegun import freeze_time from freezegun.api import FrozenDateTimeFactory import pytest from syrupy.assertion import SnapshotAssertion +import voluptuous as vol from homeassistant import config_entries, data_entry_flow, loader from homeassistant.config_entries import ConfigEntry @@ -8654,6 +8655,69 @@ async def test_options_flow_config_entry( assert result["reason"] == "abort" +async def test_options_flow_automatic_reload( + hass: HomeAssistant, + manager: config_entries.ConfigEntries, + caplog: pytest.LogCaptureFixture, +) -> 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.""" + config_entries._LOGGER.info( + "loaded entry %s with options %s", entry.title, entry.options + ) + return True + + mock_integration( + hass, + MockModule( + "test", + async_setup_entry=async_setup_entry, + async_unload_entry=AsyncMock(return_value=True), + ), + ) + 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 + assert "loaded entry First" in caplog.text + + class TestFlow(config_entries.ConfigFlow): + """Test flow.""" + + @staticmethod + @callback + def async_get_options_flow(config_entry): + """Test options flow.""" + + class _OptionsFlow(config_entries.OptionsFlowWithReload): + """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) + result = await hass.config_entries.options.async_configure( + result["flow_id"], {"test": "updated"} + ) + await hass.async_block_till_done(wait_background_tasks=True) + + assert "loaded entry Updated" in caplog.text + + @pytest.mark.parametrize("integration_frame_path", ["custom_components/my_integration"]) @pytest.mark.usefixtures("mock_integration_frame") async def test_options_flow_deprecated_config_entry_setter(