From 263bdaa565b0fd84d64617a69f0cd0897207f6b9 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 19 Jun 2020 07:03:39 -0700 Subject: [PATCH] Make panel_custom backwards compatible (#36926) --- .../components/panel_custom/__init__.py | 25 +++++++++---------- tests/components/panel_custom/test_init.py | 24 ++++++++++-------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/panel_custom/__init__.py b/homeassistant/components/panel_custom/__init__.py index ea5dc5c4aa6..6b8079f577f 100644 --- a/homeassistant/components/panel_custom/__init__.py +++ b/homeassistant/components/panel_custom/__init__.py @@ -38,18 +38,8 @@ def url_validator(value): has_html_url = CONF_WEBCOMPONENT_PATH in value has_module_url = CONF_MODULE_URL in value - if has_html_url: - if has_js_url or has_module_url: - raise vol.Invalid( - "You cannot specify other urls besides a webcomponent path" - ) - - return value - - if not has_js_url and not has_module_url: - raise vol.Invalid( - f"You need to specify either {CONF_MODULE_URL} or {CONF_JS_URL} or both." - ) + if has_html_url and (has_js_url or has_module_url): + raise vol.Invalid("You cannot specify other urls besides a webcomponent path") return value @@ -165,6 +155,8 @@ async def async_setup(hass, config): if DOMAIN not in config: return True + seen = set() + for panel in config[DOMAIN]: name = panel[CONF_COMPONENT_NAME] @@ -185,7 +177,14 @@ async def async_setup(hass, config): if CONF_MODULE_URL in panel: kwargs["module_url"] = panel[CONF_MODULE_URL] - if CONF_WEBCOMPONENT_PATH in panel: + if CONF_MODULE_URL not in panel and CONF_JS_URL not in panel: + if name in seen: + _LOGGER.warning( + "Got HTML panel with duplicate name %s. Not registering", name + ) + continue + + seen.add(name) panel_path = panel.get(CONF_WEBCOMPONENT_PATH) if panel_path is None: diff --git a/tests/components/panel_custom/test_init.py b/tests/components/panel_custom/test_init.py index 2506321fc74..caa55749c50 100644 --- a/tests/components/panel_custom/test_init.py +++ b/tests/components/panel_custom/test_init.py @@ -30,19 +30,22 @@ async def test_webcomponent_custom_path_not_found(hass): assert "nice_url" not in panels -async def test_webcomponent_custom_path(hass): +async def test_webcomponent_custom_path(hass, caplog): """Test if a web component is found in config panels dir.""" filename = "mock.file" config = { - "panel_custom": { - "name": "todo-mvc", - "webcomponent_path": filename, - "sidebar_title": "Sidebar Title", - "sidebar_icon": "mdi:iconicon", - "url_path": "nice_url", - "config": {"hello": "world"}, - } + "panel_custom": [ + { + "name": "todo-mvc", + "webcomponent_path": filename, + "sidebar_title": "Sidebar Title", + "sidebar_icon": "mdi:iconicon", + "url_path": "nice_url", + "config": {"hello": "world"}, + }, + {"name": "todo-mvc"}, + ] } with patch("os.path.isfile", Mock(return_value=True)): @@ -70,6 +73,8 @@ async def test_webcomponent_custom_path(hass): assert panel.sidebar_icon == "mdi:iconicon" assert panel.sidebar_title == "Sidebar Title" + assert "Got HTML panel with duplicate name todo-mvc. Not registering" in caplog.text + async def test_js_webcomponent(hass): """Test if a web component is found in config panels dir.""" @@ -186,7 +191,6 @@ async def test_latest_and_es5_build(hass): async def test_url_option_conflict(hass): """Test config with multiple url options.""" to_try = [ - {"panel_custom": {"name": "todo-mvc"}}, { "panel_custom": { "name": "todo-mvc",