Skip to content

Redirect repairs to the first view with unknown entities - #1172

Merged
frenck merged 6 commits into
frenck:mainfrom
piitaya:unknown_entity_redirect_view
May 28, 2026
Merged

frenck merged 6 commits into
frenck:mainfrom
piitaya:unknown_entity_redirect_view

Conversation

@piitaya

@piitaya piitaya commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

Description

Redirect repairs to the first view with unknown entities

Motivation and Context

Previously, the "unknown entity references" repair always linked to view 0 regardless of which view actually contained the missing entity. This made it harder for users to find and fix the issue, especially in dashboards with many views.

Now the repair link points directly to the first view that contains an unknown entity, using the view's path if set or falling back to the view index.

How has this been tested?

Manual testing with dashboards containing multiple views where unknown entities exist in views other than the first one.

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.

@coderabbitai

coderabbitai Bot commented Jan 20, 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: 0ecd2e74-863d-49b0-a54f-4d4fb14118c9

📥 Commits

Reviewing files that changed from the base of the PR and between 5ff1b85 and 78eb01c.

📒 Files selected for processing (2)
  • custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py
  • tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py

📝 Walkthrough

Walkthrough

The extractor now returns a mapping entity_id → view_path (int|string). The inspector calls the extractor once per dashboard, uses the mapping keys to identify unknown entities, and builds edit URLs using the mapping’s first_view_path for the first unknown entity.

Changes

Entity extraction & inspector

Layer / File(s) Summary
Extractor return type and logic
custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py
__async_extract_entities now returns `dict[str, int
Imports & inspector update
custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py
Precompute extracted_entities = __async_extract_entities(config), use extracted_entities.keys() for unknown-entity checks, derive first_view_path from extracted_entities[first_unknown], and format edit URL as /{url_path}/{first_view_path}?edit=1.
Tests updated for mapping return
tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py
Change test helper _extract signature/return to `dict[str, int

Sequence Diagram(s)

sequenceDiagram
  participant Inspector
  participant Extractor
  participant IssueCreator
  Inspector->>Extractor: __async_extract_entities(config) -> dict(entity_id -> view_path)
  Inspector->>Inspector: compute unknown IDs from extracted.keys()
  Inspector->>IssueCreator: create issue with edit URL using first_view_path
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • frenck/spook#1275: Adds/locks golden-path tests for the Lovelace unknown-entity extractor touching the same extractor/tests.

Suggested labels

enhancement

Suggested reviewers

  • frenck

Poem

🐰 I hopped through dashboards, keen and bright,
Collected entities, noted each sight,
From sets to maps my findings grew,
Paths and IDs now tied true,
A tiny hop — one tidy fix! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: redirecting repairs to the first view with unknown entities instead of always linking to view 0.
Description check ✅ Passed The description is relevant and provides context about the change, motivation, testing approach, and impact on the repair linking behavior.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In
`@custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py`:
- Around line 71-72: unknown_entities is a set so using
next(iter(unknown_entities)) yields an arbitrary entity; to pick the first
unknown entity by view order, iterate the insertion-ordered mapping
extracted_entities and select the first key present in unknown_entities, then
assign its value to first_view_path (update the code around first_view_path =
extracted_entities[next(iter(unknown_entities))] to search
extracted_entities.keys() for the first key in unknown_entities).

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In
`@custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py`:
- Around line 71-76: The StopIteration is caused by keys in extracted_entities
containing comma-separated raw strings while async_filter_known_entity_ids
returns split entity IDs; update __async_extract_entities to normalize/split and
strip comma-separated entity strings into individual keys (e.g., produce entries
for "light.a" and "light.b" rather than "light.a, light.b") so the lookup used
by the next(...) that computes first_view_path against unknown_entities
succeeds; ensure extracted_entities remains a mapping from single entity_id ->
path and adjust any deduplication logic accordingly.

@sonarqubecloud

Copy link
Copy Markdown

@frenckatron frenckatron added the bugfix Inconsistencies or issues which will cause a problem for users or implementors. label May 28, 2026
@frenckatron
frenckatron force-pushed the unknown_entity_redirect_view branch from b444268 to bc46c81 Compare May 28, 2026 07:21
@frenckatron

Copy link
Copy Markdown
Collaborator

I rebased this onto current main, resolved the import conflict caused by the helper split, and added tests for the view-path behavior so this is covered in CI.

@codecov-commenter

codecov-commenter commented May 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 56.99%. Comparing base (a332a66) to head (df695b0).

Files with missing lines Patch % Lines
...asms/lovelace/repairs/unknown_entity_references.py 84.61% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1172      +/-   ##
==========================================
+ Coverage   56.84%   56.99%   +0.14%     
==========================================
  Files         119      119              
  Lines        3008     3016       +8     
  Branches      382      386       +4     
==========================================
+ Hits         1710     1719       +9     
- Misses       1260     1261       +1     
+ Partials       38       36       -2     

☔ View full report in Codecov by Sentry.
📢 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 added bugfix Inconsistencies or issues which will cause a problem for users or implementors. and removed bugfix Inconsistencies or issues which will cause a problem for users or implementors. labels May 28, 2026

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 updates the Lovelace “unknown entity references” repair so the generated “edit” link targets the first view that actually contains an unknown entity, instead of always linking to view 0. This improves usability for dashboards with multiple views by taking users directly to the relevant view (using the view’s path when available, otherwise the view index).

Changes:

  • Update entity extraction to return a mapping of entity_id -> view_path_or_index to enable view-aware repair links.
  • Build the repair “edit” URL using the first view that contains an unknown entity.
  • Expand tests to validate view-path/index behavior, duplicate entity handling, and comma-separated entities.

Reviewed changes

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

File Description
custom_components/spook/ectoplasms/lovelace/repairs/unknown_entity_references.py Extracts entities with associated view path/index and uses that to build the repair edit link.
tests/ectoplasms/lovelace/repairs/test_unknown_entity_references.py Updates/extends tests to assert view-aware extraction behavior and the new mapping shape.

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

@sonarqubecloud

Copy link
Copy Markdown

@frenck frenck left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks, @piitaya 👍

../Frenck

                       

Blogging my personal ramblings at frenck.dev

@frenck
frenck merged commit 91512cc into frenck:main May 28, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants