Compare commits

...
Sign in to create a new pull request.

2 commits

Author SHA1 Message Date
Erik
8ef9a4d015 Tweak 2022-12-13 10:44:00 +01:00
Erik
e1eaa9fb9d Add new method async_finish_flow to FlowHandler 2022-12-13 10:43:59 +01:00
8 changed files with 67 additions and 15 deletions

View file

@ -104,7 +104,10 @@ class AuthManagerFlowManager(data_entry_flow.FlowManager):
"""Return a user as result of login flow."""
flow = cast(LoginFlow, flow)
if result["type"] != data_entry_flow.FlowResultType.CREATE_ENTRY:
if result["type"] not in (
data_entry_flow.FlowResultType.CREATE_ENTRY,
data_entry_flow.FlowResultType.FINISH_FLOW,
):
return result
# we got final result

View file

@ -52,7 +52,8 @@ flow for details.
Progress the flow. Most flows will be 1 page, but could optionally add extra
login challenges, like TFA. Once the flow has finished, the returned step will
have type FlowResultType.CREATE_ENTRY and "result" key will contain an authorization code.
have type FlowResultType.CREATE_ENTRY or FlowResultType.FINISH_FLOW and "result"
key will contain an authorization code.
The authorization code associated with an authorized user by default, it will
associate with an credential if "type" set to "link_user" in
"/auth/login_flow"
@ -156,7 +157,10 @@ def _prepare_result_json(
result: data_entry_flow.FlowResult,
) -> data_entry_flow.FlowResult:
"""Convert result to JSON."""
if result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY:
if result["type"] in (
data_entry_flow.FlowResultType.CREATE_ENTRY,
data_entry_flow.FlowResultType.FINISH_FLOW,
):
data = result.copy()
data.pop("result")
data.pop("data")
@ -196,7 +200,10 @@ class LoginFlowBaseView(HomeAssistantView):
result: data_entry_flow.FlowResult,
) -> web.Response:
"""Convert the flow result to a response."""
if result["type"] != data_entry_flow.FlowResultType.CREATE_ENTRY:
if result["type"] not in (
data_entry_flow.FlowResultType.CREATE_ENTRY,
data_entry_flow.FlowResultType.FINISH_FLOW,
):
# @log_invalid_auth does not work here since it returns HTTP 200.
# We need to manually log failed login attempts.
if (

View file

@ -145,7 +145,10 @@ def _prepare_result_json(
result: data_entry_flow.FlowResult,
) -> data_entry_flow.FlowResult:
"""Convert result to JSON."""
if result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY:
if result["type"] in (
data_entry_flow.FlowResultType.CREATE_ENTRY,
data_entry_flow.FlowResultType.FINISH_FLOW,
):
data = result.copy()
return data

View file

@ -117,7 +117,10 @@ class ConfigManagerEntryResourceReloadView(HomeAssistantView):
def _prepare_config_flow_result_json(result, prepare_result_json):
"""Convert result to JSON."""
if result["type"] != data_entry_flow.FlowResultType.CREATE_ENTRY:
if result["type"] not in (
data_entry_flow.FlowResultType.CREATE_ENTRY,
data_entry_flow.FlowResultType.FINISH_FLOW,
):
return prepare_result_json(result)
data = result.copy()

View file

@ -761,7 +761,10 @@ class ConfigEntriesFlowManager(data_entry_flow.FlowManager):
if not self._async_has_other_discovery_flows(flow.flow_id):
persistent_notification.async_dismiss(self.hass, DISCOVERY_NOTIFICATION_ID)
if result["type"] != data_entry_flow.FlowResultType.CREATE_ENTRY:
if result["type"] not in (
data_entry_flow.FlowResultType.CREATE_ENTRY,
data_entry_flow.FlowResultType.FINISH_FLOW,
):
return result
# Check if config entry exists with unique ID. Unload it.
@ -1674,7 +1677,10 @@ class OptionsFlowManager(data_entry_flow.FlowManager):
"""
flow = cast(OptionsFlow, flow)
if result["type"] != data_entry_flow.FlowResultType.CREATE_ENTRY:
if result["type"] not in (
data_entry_flow.FlowResultType.CREATE_ENTRY,
data_entry_flow.FlowResultType.FINISH_FLOW,
):
return result
entry = self.hass.config_entries.async_get_entry(flow.handler)

View file

@ -24,14 +24,16 @@ _LOGGER = logging.getLogger(__name__)
class FlowResultType(StrEnum):
"""Result type for a data entry flow."""
FORM = "form"
CREATE_ENTRY = "create_entry"
ABORT = "abort"
# CREATE_ENTRY is deprecated and replaced by FINISH_FLOW, to be removed in 2024.1
CREATE_ENTRY = "create_entry"
EXTERNAL_STEP = "external"
EXTERNAL_STEP_DONE = "external_done"
FINISH_FLOW = "finish_flow"
FORM = "form"
MENU = "menu"
SHOW_PROGRESS = "progress"
SHOW_PROGRESS_DONE = "progress_done"
MENU = "menu"
# RESULT_TYPE_* is deprecated, to be removed in 2022.9
@ -162,7 +164,7 @@ class FlowManager(abc.ABC):
async def async_finish_flow(
self, flow: FlowHandler, result: FlowResult
) -> FlowResult:
"""Finish a config flow and add an entry."""
"""Finish a flow."""
async def async_post_init(self, flow: FlowHandler, result: FlowResult) -> None:
"""Entry has finished executing its first step asynchronously."""
@ -503,7 +505,10 @@ class FlowHandler:
description: str | None = None,
description_placeholders: Mapping[str, str] | None = None,
) -> FlowResult:
"""Finish config flow and create a config entry."""
"""Finish a flow.
Deprecated and replaced by async_finish_flow, to be removed in 2024.1
"""
flow_result = FlowResult(
version=self.VERSION,
type=FlowResultType.CREATE_ENTRY,
@ -518,6 +523,28 @@ class FlowHandler:
flow_result["title"] = title
return flow_result
@callback
def async_finish_flow(
self,
*,
title: str,
data: Mapping[str, Any],
description: str | None = None,
description_placeholders: Mapping[str, str] | None = None,
) -> FlowResult:
"""Finish a flow."""
return FlowResult(
version=self.VERSION,
type=FlowResultType.FINISH_FLOW,
flow_id=self.flow_id,
handler=self.handler,
title=title,
data=data,
description=description,
description_placeholders=description_placeholders,
context=self.context,
)
@callback
def async_abort(
self,

View file

@ -26,7 +26,10 @@ class _BaseFlowManagerView(HomeAssistantView):
self, result: data_entry_flow.FlowResult
) -> data_entry_flow.FlowResult:
"""Convert result to JSON."""
if result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY:
if result["type"] in (
data_entry_flow.FlowResultType.CREATE_ENTRY,
data_entry_flow.FlowResultType.FINISH_FLOW,
):
data = result.copy()
data.pop("result")
data.pop("data")

View file

@ -398,7 +398,7 @@ class SchemaOptionsFlowHandler(config_entries.OptionsFlowWithConfigEntry):
data: Mapping[str, Any],
**kwargs: Any,
) -> FlowResult:
"""Finish config flow and create a config entry."""
"""Finish options flow."""
if self._async_options_flow_finished:
self._async_options_flow_finished(self.hass, data)
return super().async_create_entry(data=data, **kwargs)