Skip to content

Commit 773cb74

Browse files
authored
Translatable error msg to frontend if new dashboard url already in use (home-assistant#153501)
1 parent eefab75 commit 773cb74

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

homeassistant/components/lovelace/dashboard.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,11 @@ async def _process_create_data(self, data: dict) -> dict:
287287
raise vol.Invalid("Url path needs to contain a hyphen (-)")
288288

289289
if url_path in self.hass.data[DATA_PANELS]:
290-
raise vol.Invalid("Panel url path needs to be unique")
290+
raise HomeAssistantError(
291+
translation_domain=DOMAIN,
292+
translation_key="url_already_exists",
293+
translation_placeholders={"url": url_path},
294+
)
291295

292296
return self.CREATE_SCHEMA(data) # type: ignore[no-any-return]
293297

homeassistant/components/lovelace/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{
2+
"exceptions": {
3+
"url_already_exists": {
4+
"message": "The URL \"{url}\" is already in use. Please choose a different one."
5+
}
6+
},
27
"services": {
38
"reload_resources": {
49
"description": "Reloads dashboard resources from the YAML-configuration.",

tests/components/lovelace/test_dashboard.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ async def test_storage_dashboards(
374374
assert response["success"]
375375
assert response["result"] == []
376376

377-
# Add a wrong dashboard
377+
# Add a wrong dashboard (no hyphen)
378378
await client.send_json(
379379
{
380380
"id": 6,
@@ -484,16 +484,36 @@ async def test_storage_dashboards(
484484
assert response["result"][0]["show_in_sidebar"] is False
485485
assert response["result"][0]["require_admin"] is False
486486

487+
# Add a wrong dashboard (missing title)
488+
await client.send_json(
489+
{
490+
"id": 14,
491+
"type": "lovelace/dashboards/create",
492+
"url_path": "path",
493+
}
494+
)
495+
response = await client.receive_json()
496+
assert not response["success"]
497+
assert response["error"]["code"] == "invalid_format"
498+
487499
# Add dashboard with existing url path
488500
await client.send_json(
489-
{"id": 14, "type": "lovelace/dashboards/create", "url_path": "created-url-path"}
501+
{
502+
"id": 15,
503+
"type": "lovelace/dashboards/create",
504+
"url_path": "created-url-path",
505+
"title": "Another title",
506+
}
490507
)
491508
response = await client.receive_json()
492509
assert not response["success"]
510+
assert response["error"]["code"] == "home_assistant_error"
511+
assert response["error"]["translation_key"] == "url_already_exists"
512+
assert response["error"]["translation_placeholders"]["url"] == "created-url-path"
493513

494514
# Delete dashboards
495515
await client.send_json(
496-
{"id": 15, "type": "lovelace/dashboards/delete", "dashboard_id": dashboard_id}
516+
{"id": 16, "type": "lovelace/dashboards/delete", "dashboard_id": dashboard_id}
497517
)
498518
response = await client.receive_json()
499519
assert response["success"]

0 commit comments

Comments
 (0)