Skip to content

Commit 531397e

Browse files
authored
Avoid firing discovery events when flows immediately create a config entry (home-assistant#155753)
1 parent d6cc0f8 commit 531397e

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

homeassistant/config_entries.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,16 +1471,18 @@ async def async_init(
14711471
if not self._pending_import_flows[handler]:
14721472
del self._pending_import_flows[handler]
14731473

1474-
if (
1475-
result["type"] != data_entry_flow.FlowResultType.ABORT
1476-
and source in DISCOVERY_SOURCES
1477-
):
1474+
# Flows can abort or create an entry in their initial step, we do not want to
1475+
# fire a discovery event if no flow is actually in progress
1476+
flow_completed = result["type"] in {
1477+
data_entry_flow.FlowResultType.ABORT,
1478+
data_entry_flow.FlowResultType.CREATE_ENTRY,
1479+
}
1480+
1481+
if not flow_completed and source in DISCOVERY_SOURCES:
14781482
# Fire discovery event
14791483
await self._discovery_event_debouncer.async_call()
14801484

1481-
if result["type"] != data_entry_flow.FlowResultType.ABORT and source in (
1482-
DISCOVERY_SOURCES | {SOURCE_REAUTH}
1483-
):
1485+
if not flow_completed and source in DISCOVERY_SOURCES | {SOURCE_REAUTH}:
14841486
# Notify listeners that a flow is created
14851487
for subscription in self._flow_subscriptions:
14861488
subscription("added", flow.flow_id)

tests/components/config/test_config_entries.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections.abc import Generator
44
from http import HTTPStatus
55
from typing import Any
6-
from unittest.mock import ANY, AsyncMock, patch
6+
from unittest.mock import ANY, AsyncMock, Mock, patch
77

88
from aiohttp.test_utils import TestClient
99
from freezegun.api import FrozenDateTimeFactory
@@ -1002,6 +1002,36 @@ async def async_step_reconfigure(
10021002
}
10031003

10041004

1005+
async def test_get_progress_subscribe_create_entry(hass: HomeAssistant) -> None:
1006+
"""Test flows creating entry immediately don't trigger subscription notification."""
1007+
assert await async_setup_component(hass, "config", {})
1008+
mock_platform(hass, "test.config_flow", None)
1009+
1010+
mock_integration(
1011+
hass, MockModule("test", async_setup_entry=AsyncMock(return_value=True))
1012+
)
1013+
1014+
class TestFlow(core_ce.ConfigFlow):
1015+
VERSION = 1
1016+
1017+
async def async_step_import(
1018+
self, user_input: dict[str, Any]
1019+
) -> ConfigFlowResult:
1020+
"""Handle import - creates entry immediately."""
1021+
return self.async_create_entry(title="Test", data={})
1022+
1023+
subscription_mock = Mock()
1024+
hass.config_entries.flow.async_subscribe_flow(subscription_mock)
1025+
1026+
with mock_config_flow("test", TestFlow):
1027+
result = await hass.config_entries.flow.async_init(
1028+
"test", context={"source": core_ce.SOURCE_IMPORT}, data={}
1029+
)
1030+
1031+
assert result["type"] == FlowResultType.CREATE_ENTRY
1032+
assert len(subscription_mock.mock_calls) == 0
1033+
1034+
10051035
async def test_get_progress_subscribe_in_progress(
10061036
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
10071037
) -> None:

0 commit comments

Comments
 (0)