Prevent new discovery flows from being created when stopping (#88743)

pull/88571/head
J. Nick Koston 2023-02-25 05:02:07 -06:00 committed by GitHub
parent 7b61d3763b
commit 57360a7528
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -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)

View File

@ -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