Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -62,11 +66,18 @@
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 (by view order)
first_view_path = next(
path
for entity_id, path in extracted_entities.items()
if entity_id in unknown_entities
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
title = "Overview"
if dashboard.config:
title = dashboard.config.get("title", url_path)
Expand All @@ -77,7 +88,7 @@
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(
Expand All @@ -90,12 +101,16 @@
)

@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]:

Check failure on line 104 in custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=frenck_spook&issues=AZ5tdiRYEOv_2b6Fmxvk&open=AZ5tdiRYEOv_2b6Fmxvk&pullRequest=1172
"""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_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
Comment thread
frenckatron marked this conversation as resolved.
Resolved
return entities

@callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -53,30 +53,79 @@ 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"}]}],
}
]
}
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",
}


Expand Down
Loading