From 55528d1e4934dbeeded39f64944b45d51e6529de Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Fri, 18 Sep 2026 11:14:10 +0200 Subject: [PATCH] Fix IMAP custom data event template option not reset when cleared (#182377) --- homeassistant/components/imap/config_flow.py | 5 ++ tests/components/imap/conftest.py | 9 ++- tests/components/imap/test_config_flow.py | 81 ++++++++++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/imap/config_flow.py b/homeassistant/components/imap/config_flow.py index 546762121209..96ff2e7445ce 100644 --- a/homeassistant/components/imap/config_flow.py +++ b/homeassistant/components/imap/config_flow.py @@ -233,6 +233,11 @@ class ImapOptionsFlow(OptionsFlow): except AbortFlow as err: errors = {"base": err.reason} else: + if ( + CONF_CUSTOM_EVENT_DATA_TEMPLATE not in user_input + and CONF_CUSTOM_EVENT_DATA_TEMPLATE in entry_data + ): + entry_data.pop(CONF_CUSTOM_EVENT_DATA_TEMPLATE) entry_data.update(user_input) errors = await validate_input(self.hass, entry_data) if not errors: diff --git a/tests/components/imap/conftest.py b/tests/components/imap/conftest.py index ecc9e8a1fb41..a80791684b01 100644 --- a/tests/components/imap/conftest.py +++ b/tests/components/imap/conftest.py @@ -12,9 +12,12 @@ from .const import EMPTY_SEARCH_RESPONSE, TEST_FETCH_RESPONSE_TEXT_PLAIN @pytest.fixture def mock_setup_entry() -> Generator[AsyncMock]: """Override async_setup_entry.""" - with patch( - "homeassistant.components.imap.async_setup_entry", return_value=True - ) as mock_setup_entry: + with ( + patch("homeassistant.components.imap.async_unload_entry", return_value=True), + patch( + "homeassistant.components.imap.async_setup_entry", return_value=True + ) as mock_setup_entry, + ): yield mock_setup_entry diff --git a/tests/components/imap/test_config_flow.py b/tests/components/imap/test_config_flow.py index f8560bc2e001..c26fc19d5021 100644 --- a/tests/components/imap/test_config_flow.py +++ b/tests/components/imap/test_config_flow.py @@ -347,6 +347,87 @@ async def test_options_form(hass: HomeAssistant) -> None: assert entry.data[key] == value +async def test_options_form_reset_template( + hass: HomeAssistant, mock_setup_entry: AsyncMock +) -> None: + """Test resetting the custom_event_data_template option.""" + + # Set up an entry + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] is None + + with patch( + "homeassistant.components.imap.config_flow.connect_to_server" + ) as mock_client: + mock_client.return_value.search.return_value = ( + "OK", + [b""], + ) + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], MOCK_CONFIG + ) + await hass.async_block_till_done() + + assert result2["type"] is FlowResultType.CREATE_ENTRY + assert result2["title"] == "email@email.com" + assert result2["data"] == MOCK_CONFIG + assert len(mock_setup_entry.mock_calls) == 1 + + entry = result2["result"] + + # Set the custom_event_data_template option via the options flow + result = await hass.config_entries.options.async_init(entry.entry_id) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" + + new_config = MOCK_OPTIONS.copy() + new_config["custom_event_data_template"] = "{{ subject }}" + + with patch( + "homeassistant.components.imap.config_flow.connect_to_server" + ) as mock_client: + mock_client.return_value.search.return_value = ("OK", [b""]) + result2 = await hass.config_entries.options.async_configure( + result["flow_id"], + new_config, + ) + await hass.async_block_till_done() + assert result2["type"] is FlowResultType.CREATE_ENTRY + assert result2["data"] == {} + for key, value in new_config.items(): + assert entry.data[key] == value + + assert "custom_event_data_template" in entry.data + + # Reset custom_event_data_template option + result = await hass.config_entries.options.async_init(entry.entry_id) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" + + new_config = MOCK_OPTIONS + + with patch( + "homeassistant.components.imap.config_flow.connect_to_server" + ) as mock_client: + mock_client.return_value.search.return_value = ("OK", [b""]) + result2 = await hass.config_entries.options.async_configure( + result["flow_id"], + new_config, + ) + await hass.async_block_till_done() + assert result2["type"] is FlowResultType.CREATE_ENTRY + assert result2["data"] == {} + for key, value in new_config.items(): + assert entry.data[key] == value + + assert "custom_event_data_template" not in entry.data + + async def test_key_options_in_options_form(hass: HomeAssistant) -> None: """Test we cannot change options if that would cause duplicates."""