diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index 89283b01037..7ff7f76c61c 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -330,6 +330,15 @@ def add_extra_js_url(hass: HomeAssistant, url: str, es5: bool = False) -> None: hass.data[key].add(url) +def remove_extra_js_url(hass: HomeAssistant, url: str, es5: bool = False) -> None: + """Remove extra js or module url to load. + + This function allows custom integrations to remove extra js or module. + """ + key = DATA_EXTRA_JS_URL_ES5 if es5 else DATA_EXTRA_MODULE_URL + hass.data[key].remove(url) + + def add_manifest_json_key(key: str, val: Any) -> None: """Add a keyval to the manifest.json.""" MANIFEST_JSON.update_key(key, val) diff --git a/tests/components/frontend/test_init.py b/tests/components/frontend/test_init.py index 610e18ddcff..81bec28598d 100644 --- a/tests/components/frontend/test_init.py +++ b/tests/components/frontend/test_init.py @@ -21,6 +21,7 @@ from homeassistant.components.frontend import ( add_extra_js_url, async_register_built_in_panel, async_remove_panel, + remove_extra_js_url, ) from homeassistant.components.websocket_api import TYPE_RESULT from homeassistant.core import HomeAssistant @@ -409,43 +410,48 @@ async def test_missing_themes(hass: HomeAssistant, ws_client) -> None: @pytest.mark.usefixtures("mock_onboarded") async def test_extra_js(hass: HomeAssistant, mock_http_client_with_extra_js) -> None: """Test that extra javascript is loaded.""" - resp = await mock_http_client_with_extra_js.get("") - assert resp.status == 200 - assert "cache-control" not in resp.headers - text = await resp.text() + async def get_response(): + resp = await mock_http_client_with_extra_js.get("") + assert resp.status == 200 + assert "cache-control" not in resp.headers + + return await resp.text() + + text = await get_response() assert '"/local/my_module.js"' in text assert '"/local/my_es5.js"' in text - # Test dynamically adding extra javascript + # Test dynamically adding and removing 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() + text = await get_response() assert '"/local/my_module_2.js"' in text assert '"/local/my_es5_2.js"' in text + remove_extra_js_url(hass, "/local/my_module_2.js", False) + remove_extra_js_url(hass, "/local/my_es5_2.js", True) + text = await get_response() + assert '"/local/my_module_2.js"' not in text + assert '"/local/my_es5_2.js"' not in text + + # Remove again should not raise + remove_extra_js_url(hass, "/local/my_module_2.js", False) + remove_extra_js_url(hass, "/local/my_es5_2.js", True) + text = await get_response() + assert '"/local/my_module_2.js"' not in text + assert '"/local/my_es5_2.js"' not in text + # safe mode hass.config.safe_mode = 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() + text = await get_response() 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() + text = await get_response() assert '"/local/my_module_2.js"' not in text assert '"/local/my_es5_2.js"' not in text