diff --git a/homeassistant/helpers/discovery_flow.py b/homeassistant/helpers/discovery_flow.py index 2bfccf46960..f7e78e82fb4 100644 --- a/homeassistant/helpers/discovery_flow.py +++ b/homeassistant/helpers/discovery_flow.py @@ -44,7 +44,9 @@ def _async_init_flow( # as ones in progress as it may cause additional device probing # which can overload devices since zeroconf/ssdp updates can happen # multiple times in the same minute - if hass.config_entries.flow.async_has_matching_flow(domain, context, data): + if hass.is_stopping or hass.config_entries.flow.async_has_matching_flow( + domain, context, data + ): return None return hass.config_entries.flow.async_init(domain, context=context, data=data) diff --git a/tests/helpers/test_discovery_flow.py b/tests/helpers/test_discovery_flow.py index 3b20782f5b4..9f1d8dfcbc9 100644 --- a/tests/helpers/test_discovery_flow.py +++ b/tests/helpers/test_discovery_flow.py @@ -96,3 +96,20 @@ async def test_async_create_flow_checks_existing_flows_before_startup( data={"properties": {"id": "aa:bb:cc:dd:ee:ff"}}, ) ] + + +async def test_async_create_flow_does_nothing_after_stop( + hass: HomeAssistant, mock_flow_init +) -> None: + """Test we no longer create flows when hass is stopping.""" + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) + await hass.async_block_till_done() + hass.state = CoreState.stopping + mock_flow_init.reset_mock() + discovery_flow.async_create_flow( + hass, + "hue", + {"source": config_entries.SOURCE_HOMEKIT}, + {"properties": {"id": "aa:bb:cc:dd:ee:ff"}}, + ) + assert len(mock_flow_init.mock_calls) == 0