Allow passing options in config flow entry creation (#49912)

This commit is contained in:
Franck Nijhof 2021-05-06 07:14:01 +02:00 committed by GitHub
parent 465161b38c
commit 177317a345
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 2 deletions

View File

@ -153,7 +153,7 @@ class ConfigEntry:
data: Mapping[str, Any],
source: str,
system_options: dict,
options: dict | None = None,
options: Mapping[str, Any] | None = None,
unique_id: str | None = None,
entry_id: str | None = None,
state: str = ENTRY_STATE_NOT_LOADED,
@ -631,7 +631,7 @@ class ConfigEntriesFlowManager(data_entry_flow.FlowManager):
domain=result["handler"],
title=result["title"],
data=result["data"],
options={},
options=result["options"],
system_options={},
source=flow.context["source"],
unique_id=flow.unique_id,
@ -1297,6 +1297,28 @@ class ConfigFlow(data_entry_flow.FlowHandler):
"""Handle a flow initialized by DHCP discovery."""
return await self.async_step_discovery(discovery_info)
@callback
def async_create_entry( # pylint: disable=arguments-differ
self,
*,
title: str,
data: Mapping[str, Any],
description: str | None = None,
description_placeholders: dict | None = None,
options: Mapping[str, Any] | None = None,
) -> data_entry_flow.FlowResult:
"""Finish config flow and create a config entry."""
result = super().async_create_entry(
title=title,
data=data,
description=description,
description_placeholders=description_placeholders,
)
result["options"] = options or {}
return result
class OptionsFlowManager(data_entry_flow.FlowManager):
"""Flow to set options for a configuration entry."""

View File

@ -73,6 +73,7 @@ class FlowResult(TypedDict, total=False):
context: dict[str, Any]
result: Any
last_step: bool | None
options: Mapping[str, Any]
class FlowManager(abc.ABC):

View File

@ -333,6 +333,7 @@ async def test_create_account(hass, client):
},
"description": None,
"description_placeholders": None,
"options": {},
}
@ -403,6 +404,7 @@ async def test_two_step_flow(hass, client):
},
"description": None,
"description_placeholders": None,
"options": {},
}

View File

@ -152,6 +152,7 @@ async def test_pairing(hass, mock_tv_pairable, mock_setup_entry):
"title": "55PUS7181/12 (ABCDEFGHIJKLF)",
"data": MOCK_CONFIG_PAIRED,
"version": 1,
"options": {},
}
await hass.async_block_till_done()

View File

@ -46,6 +46,7 @@ MOCK_FLOW_RESULT = {
"type": data_entry_flow.RESULT_TYPE_CREATE_ENTRY,
"title": "test_ps4",
"data": MOCK_DATA,
"options": {},
}
MOCK_ENTRY_ID = "SomeID"

View File

@ -115,6 +115,7 @@ async def test_user_form_pin_not_required(hass, user_form):
"type": "create_entry",
"version": 1,
"data": deepcopy(TEST_CONFIG),
"options": {},
}
expected["data"][CONF_PIN] = None
result["data"][CONF_DEVICE_ID] = TEST_DEVICE_ID
@ -176,6 +177,7 @@ async def test_pin_form_success(hass, pin_form):
"type": "create_entry",
"version": 1,
"data": TEST_CONFIG,
"options": {},
}
result["data"][CONF_DEVICE_ID] = TEST_DEVICE_ID
assert result == expected

View File

@ -941,6 +941,56 @@ async def test_setup_retrying_during_unload_before_started(hass):
)
async def test_create_entry_options(hass):
"""Test a config entry being created with options."""
async def mock_async_setup(hass, config):
"""Mock setup."""
hass.async_create_task(
hass.config_entries.flow.async_init(
"comp",
context={"source": config_entries.SOURCE_IMPORT},
data={"data": "data", "option": "option"},
)
)
return True
async_setup_entry = AsyncMock(return_value=True)
mock_integration(
hass,
MockModule(
"comp", async_setup=mock_async_setup, async_setup_entry=async_setup_entry
),
)
mock_entity_platform(hass, "config_flow.comp", None)
await async_setup_component(hass, "persistent_notification", {})
class TestFlow(config_entries.ConfigFlow):
"""Test flow."""
VERSION = 1
async def async_step_import(self, user_input):
"""Test import step creating entry, with options."""
return self.async_create_entry(
title="title",
data={"example": user_input["data"]},
options={"example": user_input["option"]},
)
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
assert await async_setup_component(hass, "comp", {})
await hass.async_block_till_done()
assert len(async_setup_entry.mock_calls) == 1
entries = hass.config_entries.async_entries("comp")
assert len(entries) == 1
assert entries[0].data == {"example": "data"}
assert entries[0].options == {"example": "option"}
async def test_entry_options(hass, manager):
"""Test that we can set options on an entry."""
entry = MockConfigEntry(domain="test", data={"first": True}, options=None)