Don't show active user initiated data entry config flows (#137334)

Do not show active user initiated  data entry config flows
This commit is contained in:
Jan Bouwhuis 2025-02-04 17:54:05 +01:00 committed by Bram Kragten
parent 2005e14d5f
commit 37f0832c8b
2 changed files with 44 additions and 6 deletions

View File

@ -302,7 +302,8 @@ def config_entries_progress(
[ [
flw flw
for flw in hass.config_entries.flow.async_progress() for flw in hass.config_entries.flow.async_progress()
if flw["context"]["source"] != config_entries.SOURCE_USER if flw["context"]["source"]
not in (config_entries.SOURCE_RECONFIGURE, config_entries.SOURCE_USER)
], ],
) )

View File

@ -3,6 +3,7 @@
from collections import OrderedDict from collections import OrderedDict
from collections.abc import Generator from collections.abc import Generator
from http import HTTPStatus from http import HTTPStatus
from typing import Any
from unittest.mock import ANY, AsyncMock, patch from unittest.mock import ANY, AsyncMock, patch
from aiohttp.test_utils import TestClient from aiohttp.test_utils import TestClient
@ -12,12 +13,13 @@ import voluptuous as vol
from homeassistant import config_entries as core_ce, data_entry_flow, loader from homeassistant import config_entries as core_ce, data_entry_flow, loader
from homeassistant.components.config import config_entries from homeassistant.components.config import config_entries
from homeassistant.config_entries import HANDLERS, ConfigFlow from homeassistant.config_entries import HANDLERS, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_RADIUS from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_RADIUS
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import config_entry_flow, config_validation as cv from homeassistant.helpers import config_entry_flow, config_validation as cv
from homeassistant.helpers.discovery_flow import DiscoveryKey from homeassistant.helpers.discovery_flow import DiscoveryKey
from homeassistant.helpers.service_info.hassio import HassioServiceInfo
from homeassistant.loader import IntegrationNotFound from homeassistant.loader import IntegrationNotFound
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
@ -729,27 +731,62 @@ async def test_get_progress_index(
mock_platform(hass, "test.config_flow", None) mock_platform(hass, "test.config_flow", None)
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
mock_integration(
hass, MockModule("test", async_setup_entry=AsyncMock(return_value=True))
)
entry = MockConfigEntry(domain="test", title="Test", entry_id="1234")
entry.add_to_hass(hass)
class TestFlow(core_ce.ConfigFlow): class TestFlow(core_ce.ConfigFlow):
VERSION = 5 VERSION = 5
async def async_step_hassio(self, discovery_info): async def async_step_hassio(
self, discovery_info: HassioServiceInfo
) -> ConfigFlowResult:
"""Handle a Hass.io discovery."""
return await self.async_step_account() return await self.async_step_account()
async def async_step_account(self, user_input=None): async def async_step_account(self, user_input: dict[str, Any] | None = None):
"""Show a form to the user."""
return self.async_show_form(step_id="account") return self.async_show_form(step_id="account")
async def async_step_user(self, user_input: dict[str, Any] | None = None):
"""Handle a config flow initialized by the user."""
return await self.async_step_account()
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
):
"""Handle a reconfiguration flow initialized by the user."""
nonlocal entry
assert self._get_reconfigure_entry() is entry
return await self.async_step_account()
with patch.dict(HANDLERS, {"test": TestFlow}): with patch.dict(HANDLERS, {"test": TestFlow}):
form = await hass.config_entries.flow.async_init( form_hassio = await hass.config_entries.flow.async_init(
"test", context={"source": core_ce.SOURCE_HASSIO} "test", context={"source": core_ce.SOURCE_HASSIO}
) )
form_user = await hass.config_entries.flow.async_init(
"test", context={"source": core_ce.SOURCE_USER}
)
form_reconfigure = await hass.config_entries.flow.async_init(
"test", context={"source": core_ce.SOURCE_RECONFIGURE, "entry_id": "1234"}
)
for form in (form_hassio, form_user, form_reconfigure):
assert form["type"] == data_entry_flow.FlowResultType.FORM
assert form["step_id"] == "account"
await ws_client.send_json({"id": 5, "type": "config_entries/flow/progress"}) await ws_client.send_json({"id": 5, "type": "config_entries/flow/progress"})
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response["success"] assert response["success"]
# Active flows with SOURCE_USER and SOURCE_RECONFIGURE should be filtered out
assert response["result"] == [ assert response["result"] == [
{ {
"flow_id": form["flow_id"], "flow_id": form_hassio["flow_id"],
"handler": "test", "handler": "test",
"step_id": "account", "step_id": "account",
"context": {"source": core_ce.SOURCE_HASSIO}, "context": {"source": core_ce.SOURCE_HASSIO},