From a416a9e0461719df45d55f0055202726f8402f2f Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Mon, 9 Nov 2020 02:59:42 -0500 Subject: [PATCH] Allow options flows to be aborted (#41875) --- homeassistant/config_entries.py | 3 +++ tests/test_config_entries.py | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index a7de2107ecd..7fa343146d2 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -1075,6 +1075,9 @@ class OptionsFlowManager(data_entry_flow.FlowManager): """ flow = cast(OptionsFlow, flow) + if result["type"] != data_entry_flow.RESULT_TYPE_CREATE_ENTRY: + return result + entry = self.hass.config_entries.async_get_entry(flow.handler) if entry is None: raise UnknownEntry(flow.handler) diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 6c2df29985c..5d63964d6b1 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -861,12 +861,45 @@ async def test_entry_options(hass, manager): flow.handler = entry.entry_id # Used to keep reference to config entry - await manager.options.async_finish_flow(flow, {"data": {"second": True}}) + await manager.options.async_finish_flow( + flow, + {"data": {"second": True}, "type": data_entry_flow.RESULT_TYPE_CREATE_ENTRY}, + ) assert entry.data == {"first": True} assert entry.options == {"second": True} +async def test_entry_options_abort(hass, manager): + """Test that we can abort options flow.""" + entry = MockConfigEntry(domain="test", data={"first": True}, options=None) + entry.add_to_manager(manager) + + class TestFlow: + """Test flow.""" + + @staticmethod + @callback + def async_get_options_flow(config_entry): + """Test options flow.""" + + class OptionsFlowHandler(data_entry_flow.FlowHandler): + """Test options flow handler.""" + + return OptionsFlowHandler() + + config_entries.HANDLERS["test"] = TestFlow() + flow = await manager.options.async_create_flow( + entry.entry_id, context={"source": "test"}, data=None + ) + + flow.handler = entry.entry_id # Used to keep reference to config entry + + assert await manager.options.async_finish_flow( + flow, {"type": data_entry_flow.RESULT_TYPE_ABORT, "reason": "test"} + ) + + async def test_entry_setup_succeed(hass, manager): """Test that we can setup an entry.""" entry = MockConfigEntry(domain="comp", state=config_entries.ENTRY_STATE_NOT_LOADED)