diff --git a/homeassistant/components/http/__init__.py b/homeassistant/components/http/__init__.py index e06ceb087c2..b387cea350e 100644 --- a/homeassistant/components/http/__init__.py +++ b/homeassistant/components/http/__init__.py @@ -19,6 +19,7 @@ from homeassistant.core import Event, HomeAssistant from homeassistant.helpers import storage import homeassistant.helpers.config_validation as cv from homeassistant.loader import bind_hass +from homeassistant.setup import ATTR_COMPONENT, EVENT_COMPONENT_LOADED import homeassistant.util as hass_util from homeassistant.util import ssl as ssl_util @@ -232,6 +233,17 @@ async def async_setup(hass, config): await start_http_server_and_save_config(hass, dict(conf), server) + async def async_wait_frontend_load(event: Event) -> None: + """Wait for the frontend to load.""" + + if event.data[ATTR_COMPONENT] != "frontend": + return + + await start_server(event) + + startup_listeners.append( + hass.bus.async_listen(EVENT_COMPONENT_LOADED, async_wait_frontend_load) + ) startup_listeners.append( hass.bus.async_listen(EVENT_HOMEASSISTANT_START, start_server) ) diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index 3ea42d4545b..e14afdca28a 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -316,6 +316,8 @@ async def test_setup_hass_takes_longer_than_log_slow_startup( ), patch.object(bootstrap, "LOG_SLOW_STARTUP_INTERVAL", 0.3), patch( "homeassistant.components.frontend.async_setup", side_effect=_async_setup_that_blocks_startup, + ), patch( + "homeassistant.components.http.start_http_server_and_save_config" ): await bootstrap.async_setup_hass( config_dir=get_test_config_dir(),