Skip to content

Use the generic walker in the Lovelace repair - #1364

Merged
frenck merged 1 commit into
mainfrom
frenck/lovelace-use-walker
Jul 3, 2026
Merged

frenck merged 1 commit into
mainfrom
frenck/lovelace-use-walker

Conversation

@frenck

@frenck frenck commented Jul 3, 2026

Copy link
Copy Markdown
Owner

Description

Replaces the Lovelace unknown entity references repair's ten hand-rolled per-card extraction methods with the generic extract_entities_from_dashboard_node walker from #1363, called once per view. The repair keeps its view-path bookkeeping (each entity is mapped to the first view it appears in, for the issue's edit link) and its comma-splitting, but the extraction is now generic.

Net effect: the repair drops ~250 lines, standard card behavior is unchanged, and references buried in custom cards or new built-in card shapes are now found without per-card maintenance.

The 21 card/badge/element tests that exercised the now-deleted private methods are removed; their behavioral coverage lives in tests/test_dashboard_extraction.py (added with the walker). The repair's own view-path tests and issue-message test are kept, and a regression test is added proving a reference inside a custom: card structure is now surfaced.

Motivation and Context

A custom card burying an entity key was invisible to the old walker, and every new built-in card shape needed a code change. One generic walk fixes both, and is far less code.

How has this been tested?

The kept view-path tests confirm the repair's bookkeeping is unchanged; the new custom-card test confirms the coverage improvement; the walker itself is covered by tests/test_dashboard_extraction.py. Full suite passes (571 tests), ruff, pylint (10.00/10), and all prek hooks pass.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Copilot AI review requested due to automatic review settings July 3, 2026 19:13
@frenck frenck added the refactor Improvement of existing code, not introducing new features. label Jul 3, 2026
@coderabbitai

coderabbitai Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c70d147c-aeb2-4156-92a4-a8879bc21bce

📥 Commits

Reviewing files that changed from the base of the PR and between c7807bc and eeb7ba6.

📒 Files selected for processing (2)
  • custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py
  • tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py

📝 Walkthrough

Walkthrough

The Lovelace unknown-entity-references repair is refactored to extract entities using a shared extract_entities_from_dashboard_node helper instead of its own view-traversal and comma-splitting logic. Corresponding tests are updated: obsolete helper wrappers and imports are removed, and a new test covers custom card structures.

Changes

Shared Dashboard Extraction Refactor

Layer / File(s) Summary
Switch view extraction to shared helper
custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py
Imports extract_entities_from_dashboard_node and replaces the internal view-extraction/comma-splitting logic with a call to this shared helper, populating the entities dict via setdefault.
Update tests for dashboard-level extraction
tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py
Updates docstring and imports, removes _extract_card, _extract_badge, _extract_element wrappers, and adds test_dashboard_covers_custom_card_structures for custom card entity extraction.

Estimated code review effort: 2 (Simple) | ~12 minutes

Possibly related PRs

  • frenck/spook#798: Both PRs modify __async_extract_entities in the same file to change Lovelace view entity extraction logic.
  • frenck/spook#1172: Both PRs refactor the same file to extract entities as an entity_id-to-view_path mapping, updating corresponding tests.
  • frenck/spook#1291: Both PRs relate to dashboard-level entity extraction exercised by the same repair tests.

Poem

A rabbit hopped through cards and views,
Found one path where entities cruise,
No more splitting commas by hand,
One shared helper now takes command,
Hop, hop, hooray — the code's refined! 🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main refactor to use a generic walker in the Lovelace repair.
Description check ✅ Passed The description clearly matches the changeset and explains the refactor, test updates, and behavior impact.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch frenck/lovelace-use-walker

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

sonarqubecloud Bot commented Jul 3, 2026

Copy link
Copy Markdown

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.36%. Comparing base (c7807bc) to head (eeb7ba6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1364      +/-   ##
==========================================
- Coverage   81.60%   81.36%   -0.25%     
==========================================
  Files         132      132              
  Lines        3730     3585     -145     
  Branches      509      447      -62     
==========================================
- Hits         3044     2917     -127     
+ Misses        586      581       -5     
+ Partials      100       87      -13     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@frenck
frenck merged commit 02846fb into main Jul 3, 2026
21 checks passed
@frenck
frenck deleted the frenck/lovelace-use-walker branch July 3, 2026 19:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces the Lovelace unknown_entity_references repair's ~250 lines of hand-rolled, per-card entity-extraction methods with the generic extract_entities_from_dashboard_node walker (introduced in #1363), invoked once per view. The repair keeps its view-path bookkeeping (each entity maps to the first view it appears in, used for the issue's edit link), and comma-splitting now happens inside the walker via split_comma_separated_entity_ids. This fits Spook's broader pattern of centralizing extraction logic and relying on async_filter_known_entity_ids for downstream validation, and it also lets references buried in custom cards be discovered without per-card maintenance.

Changes:

  • Swapped the per-card/badge/element/section/action extraction methods for a single call to the generic dashboard walker, keyed to view paths via setdefault.
  • Updated imports (dropped the now-unused split_comma_separated_entity_ids, added extract_entities_from_dashboard_node).
  • Removed the card/badge/element unit tests and added a custom-card regression test; also removed the two state-only entity event-recheck tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py Replaces per-card extraction helpers with the generic walker while preserving per-view path bookkeeping.
tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py Drops card/badge/element and state-only tests; adds a custom-card regression test and keeps view-path/issue tests.

Notes:

  • The generic walker's behavioral parity (badges, cards, sections, action targets/service_data, visibility conditions, chips, nested elements, comma-splitting) is preserved through recursion plus recognized keys, and is well-covered by tests/test_dashboard_extraction.py.
  • The two removed state-only entity event tests cover behavior in async_activate that this PR does not change and that remains present, so that lovelace-specific event wiring is now untested (the parallel automation-repair tests are still kept).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

assert calls == 0

await repair.async_deactivate()
assert _extract(repair, config) == {"sensor.custom": "home"}
Comment on lines 14 to 15

from homeassistant.const import EVENT_STATE_CHANGED
from homeassistant.core import State

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Improvement of existing code, not introducing new features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants