mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Reject string flow result types in data entry flow (#189583)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
aeeb2d6e9b
commit
2fd3d3bbb7
@@ -548,12 +548,20 @@ class FlowManager(abc.ABC, Generic[_FlowContextT, _FlowResultT, _HandlerT]):
|
||||
except AbortFlow as err:
|
||||
result = self._abort_flow_result(flow, err)
|
||||
|
||||
# Reject before the result is used, a plain string type would bypass the
|
||||
# handling and filtering of results that compare the type by identity
|
||||
if not isinstance(result["type"], FlowResultType):
|
||||
if flow.flow_id in self._progress: # type: ignore[unreachable]
|
||||
self._async_remove_flow_progress(flow.flow_id)
|
||||
raise TypeError(
|
||||
f"Flow step {step_id} of {flow.handler} returned result type "
|
||||
f"{result['type']!r}, which is not a FlowResultType"
|
||||
)
|
||||
|
||||
if flow.flow_id not in self._progress:
|
||||
# The flow was removed during the step, raise UnknownFlow unless
|
||||
# the result is an abort. Compares against the string value
|
||||
# because this runs before the legacy-string normalization
|
||||
# below, and out-of-tree flow handlers may still return raw "abort".
|
||||
if result["type"] != FlowResultType.ABORT.value:
|
||||
# the result is an abort.
|
||||
if result["type"] is not FlowResultType.ABORT:
|
||||
raise UnknownFlow
|
||||
return result
|
||||
|
||||
@@ -561,14 +569,6 @@ class FlowManager(abc.ABC, Generic[_FlowContextT, _FlowResultT, _HandlerT]):
|
||||
if result.get("preview") is not None:
|
||||
await self._async_setup_preview(flow)
|
||||
|
||||
if not isinstance(result["type"], FlowResultType):
|
||||
result["type"] = FlowResultType(result["type"]) # type: ignore[unreachable]
|
||||
report_usage(
|
||||
"does not use FlowResultType enum for data entry flow result type",
|
||||
core_behavior=ReportBehavior.LOG,
|
||||
breaks_in_ha_version="2025.1",
|
||||
)
|
||||
|
||||
if (
|
||||
result["type"] is FlowResultType.SHOW_PROGRESS
|
||||
# Mypy does not agree with using pop on _FlowResultT
|
||||
|
||||
@@ -24,6 +24,7 @@ from homeassistant.components.mqtt import DOMAIN as MQTT_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntries
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STARTED
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers.discovery_flow import DiscoveryKey
|
||||
from homeassistant.helpers.service_info.hassio import HassioServiceInfo
|
||||
from homeassistant.setup import async_setup_component
|
||||
@@ -42,7 +43,7 @@ def mock_mqtt_fixture(
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async_step_hassio = AsyncMock(return_value={"type": "abort"})
|
||||
async_step_hassio = AsyncMock(return_value={"type": FlowResultType.ABORT})
|
||||
|
||||
with mock_config_flow(MQTT_DOMAIN, MqttFlow):
|
||||
yield MqttFlow
|
||||
|
||||
@@ -262,6 +262,31 @@ async def test_abort_removes_instance(manager: MockFlowManager) -> None:
|
||||
assert len(manager.mock_created_entries) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("result_type", ["create_entry", "form", "abort"])
|
||||
async def test_string_result_type_raises(
|
||||
manager: MockFlowManager, result_type: str
|
||||
) -> None:
|
||||
"""Test a step result with a plain string type is rejected."""
|
||||
|
||||
@manager.mock_reg_handler("test")
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
async def async_step_init(self, user_input=None):
|
||||
return {
|
||||
"type": result_type,
|
||||
"flow_id": self.flow_id,
|
||||
"handler": self.handler,
|
||||
"step_id": "init",
|
||||
"title": "Test",
|
||||
"data": {"password": "secret"},
|
||||
"reason": "test",
|
||||
}
|
||||
|
||||
with pytest.raises(TypeError, match="which is not a FlowResultType"):
|
||||
await manager.async_init("test")
|
||||
assert len(manager.mock_created_entries) == 0
|
||||
assert manager.async_progress(include_uninitialized=True) == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"translation_domain",
|
||||
[None, "homeassistant"],
|
||||
|
||||
Reference in New Issue
Block a user