From 906f070180fc895092bf89294f34f9233aa7b43a Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 20 Jan 2026 14:48:08 +0100 Subject: [PATCH 1/5] Redirect repairs to the first view with unknown entities --- .../repairs/unknown_entity_references.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py b/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py index 8e1bbebadb..5eef5f8909 100644 --- a/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py +++ b/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py @@ -62,11 +62,14 @@ async def async_inspect(self) -> None: LOGGER.debug("Config for dashboard %s not found, skipping", url_path) continue + extracted_entities = self.__async_extract_entities(config) if unknown_entities := async_filter_known_entity_ids( self.hass, - entity_ids=self.__async_extract_entities(config), + entity_ids=set(extracted_entities.keys()), known_entity_ids=known_entity_ids, ): + # Get the view path of the first unknown entity + first_view_path = extracted_entities[next(iter(unknown_entities))] title = "Overview" if dashboard.config: title = dashboard.config.get("title", url_path) @@ -77,7 +80,7 @@ async def async_inspect(self) -> None: f"- `{entity_id}`" for entity_id in unknown_entities ), "dashboard": title, - "edit": f"/{url_path}/0?edit=1", + "edit": f"/{url_path}/{first_view_path}?edit=1", }, ) LOGGER.debug( @@ -90,12 +93,15 @@ async def async_inspect(self) -> None: ) @callback - def __async_extract_entities(self, config: dict[str, Any]) -> set[str]: + def __async_extract_entities(self, config: dict[str, Any]) -> dict[str, int | str]: """Extract entities from a dashboard config.""" - entities = set() + entities: dict[str, int | str] = {} if isinstance(config, dict) and (views := config.get("views")): - for view in views: - entities.update(self.__async_extract_entities_from_view(view)) + for view_index, view in enumerate(views): + view_path: int | str = view.get("path", view_index) + for entity_id in self.__async_extract_entities_from_view(view): + if entity_id not in entities: + entities[entity_id] = view_path return entities @callback From d6469619d3eaddc21e188d4f95037a82ba863912 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 20 Jan 2026 14:54:46 +0100 Subject: [PATCH 2/5] Review --- .../lovelace/repairs/unknown_entity_references.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py b/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py index 5eef5f8909..6514dc7b02 100644 --- a/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py +++ b/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py @@ -68,8 +68,12 @@ async def async_inspect(self) -> None: entity_ids=set(extracted_entities.keys()), known_entity_ids=known_entity_ids, ): - # Get the view path of the first unknown entity - first_view_path = extracted_entities[next(iter(unknown_entities))] + # Get the view path of the first unknown entity (by view order) + first_view_path = next( + path + for entity_id, path in extracted_entities.items() + if entity_id in unknown_entities + ) title = "Overview" if dashboard.config: title = dashboard.config.get("title", url_path) From 395000a8de8652b12c846cbba5d7ae771eb2afd7 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 20 Jan 2026 15:16:41 +0100 Subject: [PATCH 3/5] Review --- .../lovelace/repairs/unknown_entity_references.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py b/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py index 6514dc7b02..add65e9301 100644 --- a/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py +++ b/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py @@ -14,7 +14,11 @@ from homeassistant.helpers import entity_registry as er from ....const import LOGGER -from ....entity_filtering import async_filter_known_entity_ids, async_get_all_entity_ids +from ....entity_filtering import ( + async_filter_known_entity_ids, + async_get_all_entity_ids, + split_comma_separated_entity_ids, +) from ....repairs import AbstractSpookRepair if TYPE_CHECKING: @@ -103,9 +107,10 @@ def __async_extract_entities(self, config: dict[str, Any]) -> dict[str, int | st if isinstance(config, dict) and (views := config.get("views")): for view_index, view in enumerate(views): view_path: int | str = view.get("path", view_index) - for entity_id in self.__async_extract_entities_from_view(view): - if entity_id not in entities: - entities[entity_id] = view_path + for entity_id_raw in self.__async_extract_entities_from_view(view): + for entity_id in split_comma_separated_entity_ids(entity_id_raw): + if entity_id not in entities: + entities[entity_id] = view_path return entities @callback From bc46c8142888fdc59831871edf66c80546d83d90 Mon Sep 17 00:00:00 2001 From: Frenckatron Date: Thu, 28 May 2026 09:20:27 +0200 Subject: [PATCH 4/5] Add tests for Lovelace repair view redirects --- .../repairs/test_unknown_entity_references.py | 63 ++++++++++++++++--- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py b/tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py index 9657a14402..dd80ded101 100644 --- a/tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py @@ -31,7 +31,7 @@ def repair_fixture(hass: HomeAssistant) -> SpookRepair: return SpookRepair(hass) -def _extract(repair: SpookRepair, config: dict[str, Any]) -> set[str]: +def _extract(repair: SpookRepair, config: dict[str, Any]) -> dict[str, int | str]: """Call the name-mangled dashboard-level extractor.""" return repair._SpookRepair__async_extract_entities(config) # noqa: SLF001 # type: ignore[attr-defined] @@ -53,20 +53,21 @@ def _extract_element(repair: SpookRepair, config: dict[str, Any]) -> set[str]: def test_dashboard_with_no_views_returns_empty(repair: SpookRepair) -> None: """A dashboard with no ``views`` key yields no entities.""" - assert _extract(repair, {}) == set() + assert _extract(repair, {}) == {} def test_dashboard_non_dict_returns_empty(repair: SpookRepair) -> None: """A non-dict config yields no entities.""" - assert _extract(repair, "not a dict") == set() # type: ignore[arg-type] + assert _extract(repair, "not a dict") == {} # type: ignore[arg-type] def test_dashboard_full_walk(repair: SpookRepair) -> None: - """Views, badges, cards, and sections are all walked.""" + """Views, badges, cards, and sections are all walked with their view path.""" config = { "views": [ { "title": "Home", + "path": "home", "badges": [{"entity": "sensor.outside_temperature"}], "cards": [{"type": "entity", "entity": "light.kitchen"}], "sections": [{"cards": [{"type": "entity", "entity": "switch.lamp"}]}], @@ -74,9 +75,57 @@ def test_dashboard_full_walk(repair: SpookRepair) -> None: ] } assert _extract(repair, config) == { - "sensor.outside_temperature", - "light.kitchen", - "switch.lamp", + "sensor.outside_temperature": "home", + "light.kitchen": "home", + "switch.lamp": "home", + } + + +def test_dashboard_uses_view_index_when_path_is_missing(repair: SpookRepair) -> None: + """A view without a path falls back to its index.""" + config = { + "views": [ + {"title": "Home", "cards": [{"type": "entity", "entity": "light.kitchen"}]}, + {"title": "Second", "cards": [{"type": "entity", "entity": "switch.lamp"}]}, + ] + } + assert _extract(repair, config) == { + "light.kitchen": 0, + "switch.lamp": 1, + } + + +def test_dashboard_keeps_first_view_for_duplicate_entity(repair: SpookRepair) -> None: + """A duplicate entity keeps the first view path by dashboard order.""" + config = { + "views": [ + {"path": "first", "cards": [{"type": "entity", "entity": "light.kitchen"}]}, + { + "path": "second", + "cards": [{"type": "entity", "entity": "light.kitchen"}], + }, + ] + } + assert _extract(repair, config) == {"light.kitchen": "first"} + + +def test_dashboard_splits_comma_separated_entities_per_view( + repair: SpookRepair, +) -> None: + """Comma-separated entity IDs keep the source view path.""" + config = { + "views": [ + { + "path": "home", + "cards": [ + {"type": "entity", "entity": "light.kitchen, switch.lamp"}, + ], + } + ] + } + assert _extract(repair, config) == { + "light.kitchen": "home", + "switch.lamp": "home", } From 78eb01ce5d05fe35e8ecfd764be77ac9e6a91b08 Mon Sep 17 00:00:00 2001 From: Frenckatron Date: Thu, 28 May 2026 09:33:27 +0200 Subject: [PATCH 5/5] Handle empty Lovelace view paths --- .../repairs/unknown_entity_references.py | 4 +++- .../repairs/test_unknown_entity_references.py | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py b/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py index add65e9301..0300febaaa 100644 --- a/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py +++ b/custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py @@ -106,7 +106,9 @@ def __async_extract_entities(self, config: dict[str, Any]) -> dict[str, int | st entities: dict[str, int | str] = {} if isinstance(config, dict) and (views := config.get("views")): for view_index, view in enumerate(views): - view_path: int | str = view.get("path", view_index) + if not isinstance(view, dict): + continue + view_path: int | str = view.get("path") or view_index for entity_id_raw in self.__async_extract_entities_from_view(view): for entity_id in split_comma_separated_entity_ids(entity_id_raw): if entity_id not in entities: diff --git a/tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py b/tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py index dd80ded101..0050f22bc8 100644 --- a/tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py @@ -95,6 +95,27 @@ def test_dashboard_uses_view_index_when_path_is_missing(repair: SpookRepair) -> } +def test_dashboard_uses_view_index_when_path_is_empty(repair: SpookRepair) -> None: + """An empty view path falls back to its index.""" + config = { + "views": [ + { + "path": "", + "cards": [{"type": "entity", "entity": "light.kitchen"}], + }, + "not a view", + { + "path": None, + "cards": [{"type": "entity", "entity": "switch.lamp"}], + }, + ] + } + assert _extract(repair, config) == { + "light.kitchen": 0, + "switch.lamp": 2, + } + + def test_dashboard_keeps_first_view_for_duplicate_entity(repair: SpookRepair) -> None: """A duplicate entity keeps the first view path by dashboard order.""" config = {