Allow options flows to be aborted (#41875)

This commit is contained in:
Raman Gupta 2020-11-09 02:59:42 -05:00 committed by GitHub
parent 0f46916f9e
commit a416a9e046
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -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)

View File

@ -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)