From 773cb7424c2df8e3bd6389e048bad11be1b66d3a Mon Sep 17 00:00:00 2001 From: steinmn <46349253+steinmn@users.noreply.github.com> Date: Sun, 23 Nov 2025 14:43:44 +0100 Subject: [PATCH] Translatable error msg to frontend if new dashboard url already in use (#153501) --- .../components/lovelace/dashboard.py | 6 +++- .../components/lovelace/strings.json | 5 ++++ tests/components/lovelace/test_dashboard.py | 28 ++++++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/lovelace/dashboard.py b/homeassistant/components/lovelace/dashboard.py index 4faf4f15b08c..41e82291e44f 100644 --- a/homeassistant/components/lovelace/dashboard.py +++ b/homeassistant/components/lovelace/dashboard.py @@ -287,7 +287,11 @@ class DashboardsCollection(collection.DictStorageCollection): raise vol.Invalid("Url path needs to contain a hyphen (-)") if url_path in self.hass.data[DATA_PANELS]: - raise vol.Invalid("Panel url path needs to be unique") + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="url_already_exists", + translation_placeholders={"url": url_path}, + ) return self.CREATE_SCHEMA(data) # type: ignore[no-any-return] diff --git a/homeassistant/components/lovelace/strings.json b/homeassistant/components/lovelace/strings.json index 98b5877cb50f..704e864c5678 100644 --- a/homeassistant/components/lovelace/strings.json +++ b/homeassistant/components/lovelace/strings.json @@ -1,4 +1,9 @@ { + "exceptions": { + "url_already_exists": { + "message": "The URL \"{url}\" is already in use. Please choose a different one." + } + }, "services": { "reload_resources": { "description": "Reloads dashboard resources from the YAML-configuration.", diff --git a/tests/components/lovelace/test_dashboard.py b/tests/components/lovelace/test_dashboard.py index 3a01e20c1fbc..5950fb7d62ac 100644 --- a/tests/components/lovelace/test_dashboard.py +++ b/tests/components/lovelace/test_dashboard.py @@ -374,7 +374,7 @@ async def test_storage_dashboards( assert response["success"] assert response["result"] == [] - # Add a wrong dashboard + # Add a wrong dashboard (no hyphen) await client.send_json( { "id": 6, @@ -484,16 +484,36 @@ async def test_storage_dashboards( assert response["result"][0]["show_in_sidebar"] is False assert response["result"][0]["require_admin"] is False - # Add dashboard with existing url path + # Add a wrong dashboard (missing title) await client.send_json( - {"id": 14, "type": "lovelace/dashboards/create", "url_path": "created-url-path"} + { + "id": 14, + "type": "lovelace/dashboards/create", + "url_path": "path", + } ) response = await client.receive_json() assert not response["success"] + assert response["error"]["code"] == "invalid_format" + + # Add dashboard with existing url path + await client.send_json( + { + "id": 15, + "type": "lovelace/dashboards/create", + "url_path": "created-url-path", + "title": "Another title", + } + ) + response = await client.receive_json() + assert not response["success"] + assert response["error"]["code"] == "home_assistant_error" + assert response["error"]["translation_key"] == "url_already_exists" + assert response["error"]["translation_placeholders"]["url"] == "created-url-path" # Delete dashboards await client.send_json( - {"id": 15, "type": "lovelace/dashboards/delete", "dashboard_id": dashboard_id} + {"id": 16, "type": "lovelace/dashboards/delete", "dashboard_id": dashboard_id} ) response = await client.receive_json() assert response["success"]