From f1eab3f11fc861408f6848306f8a9fc67e837776 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 Mar 2024 16:16:50 -1000 Subject: [PATCH] Preload config flow if it exists when loading a component (#112145) Since config_entries always requires the config_flow to be loaded to check for migrations, load it if we know it exists when loading the underlying integration --- homeassistant/loader.py | 9 +++++++++ tests/test_loader.py | 10 +++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/homeassistant/loader.py b/homeassistant/loader.py index af15a7acabf..140153c33c2 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -908,6 +908,15 @@ class Integration: with suppress(ImportError): self.get_platform("config") + if self.config_flow: + # If there is a config flow, we will cache it as well since + # config entry setup always has to load the flow to get the + # major/minor version for migrations. Since we may be running + # in the executor we will use this opportunity to cache the + # config_flow as well. + with suppress(ImportError): + self.get_platform("config_flow") + return cache[self.domain] async def async_get_platform(self, platform_name: str) -> ModuleType: diff --git a/tests/test_loader.py b/tests/test_loader.py index 9552849ac4a..d627d950f30 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -1038,10 +1038,10 @@ async def test_hass_components_use_reported( ) in caplog.text -async def test_async_get_component_preloads_config( +async def test_async_get_component_preloads_config_and_config_flow( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: - """Verify async_get_component will try to preload the config platform.""" + """Verify async_get_component will try to preload the config and config_flow platform.""" executor_import_integration = _get_test_integration( hass, "executor_import", True, import_executor=True ) @@ -1058,7 +1058,7 @@ async def test_async_get_component_preloads_config( await executor_import_integration.async_get_component() assert mock_platform_exists.call_count == 1 - assert mock_import.call_count == 2 + assert mock_import.call_count == 3 assert ( mock_import.call_args_list[0][0][0] == "homeassistant.components.executor_import" @@ -1067,6 +1067,10 @@ async def test_async_get_component_preloads_config( mock_import.call_args_list[1][0][0] == "homeassistant.components.executor_import.config" ) + assert ( + mock_import.call_args_list[2][0][0] + == "homeassistant.components.executor_import.config_flow" + ) async def test_async_get_component_deadlock_fallback(