diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index 2f64a019c19..89283b01037 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -322,7 +322,10 @@ def async_remove_panel(hass: HomeAssistant, frontend_url_path: str) -> None: def add_extra_js_url(hass: HomeAssistant, url: str, es5: bool = False) -> None: - """Register extra js or module url to load.""" + """Register extra js or module url to load. + + This function allows custom integrations to register extra js or module. + """ key = DATA_EXTRA_JS_URL_ES5 if es5 else DATA_EXTRA_MODULE_URL hass.data[key].add(url) diff --git a/tests/components/frontend/test_init.py b/tests/components/frontend/test_init.py index 084db2a27d5..610e18ddcff 100644 --- a/tests/components/frontend/test_init.py +++ b/tests/components/frontend/test_init.py @@ -18,6 +18,7 @@ from homeassistant.components.frontend import ( DOMAIN, EVENT_PANELS_UPDATED, THEMES_STORAGE_KEY, + add_extra_js_url, async_register_built_in_panel, async_remove_panel, ) @@ -416,6 +417,17 @@ async def test_extra_js(hass: HomeAssistant, mock_http_client_with_extra_js) -> assert '"/local/my_module.js"' in text assert '"/local/my_es5.js"' in text + # Test dynamically adding extra javascript + add_extra_js_url(hass, "/local/my_module_2.js", False) + add_extra_js_url(hass, "/local/my_es5_2.js", True) + resp = await mock_http_client_with_extra_js.get("") + assert resp.status == 200 + assert "cache-control" not in resp.headers + + text = await resp.text() + assert '"/local/my_module_2.js"' in text + assert '"/local/my_es5_2.js"' in text + # safe mode hass.config.safe_mode = True resp = await mock_http_client_with_extra_js.get("") @@ -426,6 +438,17 @@ async def test_extra_js(hass: HomeAssistant, mock_http_client_with_extra_js) -> assert '"/local/my_module.js"' not in text assert '"/local/my_es5.js"' not in text + # Test dynamically adding extra javascript + add_extra_js_url(hass, "/local/my_module_2.js", False) + add_extra_js_url(hass, "/local/my_es5_2.js", True) + resp = await mock_http_client_with_extra_js.get("") + assert resp.status == 200 + assert "cache-control" not in resp.headers + + text = await resp.text() + assert '"/local/my_module_2.js"' not in text + assert '"/local/my_es5_2.js"' not in text + async def test_get_panels( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, mock_http_client