Skip to content

fix: resolve dashboard entity_ids from the registry (HA 2026.6 area naming)#125

Merged
dewet22 merged 1 commit into
mainfrom
claude/priceless-cerf-3608b8
Jun 5, 2026
Merged

fix: resolve dashboard entity_ids from the registry (HA 2026.6 area naming)#125
dewet22 merged 1 commit into
mainfrom
claude/priceless-cerf-3608b8

Conversation

@dewet22

@dewet22 dewet22 commented Jun 5, 2026

Copy link
Copy Markdown
Owner

What

The generate_dashboard service predicted entity IDs as {domain}.givenergy_<kind>_<serial>_<slug>. That stopped matching reality, so generated dashboards point at non-existent entities and every tile shows "Entity not available".

Why

Home Assistant 2026.6 changed entity-id generation to fold in the device's area — upstream home-assistant/core#170560 "Prefix area to entity ID" (merge commit f823ef6, 2026-05-19). Comparing core _async_get_full_entity_name:

  • 2026.5.x: f"{device_name} {name}"
  • 2026.6.0: " ".join(p for p in (area_name, device_name, entity_name) if p), and _async_generate_entity_id gained an area_id arg defaulting to device.area_id.

So a device in area "Loft" gets sensor.loft_givenergy_inverter_<serial>_…. entity IDs are sticky once generated, so only entities created/recreated under 2026.6 (or after Recreate entity IDs) pick up the prefix — which is why a single plant can show mixed prefixes. The generator can't predict any of this (area varies per device, and users can rename entities), so it has to read the real IDs from the registry.

How

  • generate_dashboard(…, resolve_entity_id=…) rewrites every entity reference to its actual registry ID in a final pass; output is unchanged when no resolver is passed (keeps the standalone CLI / tests working).
  • The service handler builds the resolver from the entity + device registries, mapping each entity's canonical ID (slugify(device.name) + slugify(original_name)) to its real entity_id. Robust to area prefixes, per-device area differences, device renames, and user entity renames.
  • DASHBOARD_VERSION → 11 so installs with an already-generated (broken) dashboard get the existing "outdated → regenerate" Repairs prompt.
  • _migrate_unique_ids made idempotent: the p_grid_out → grid_power entity_id rename now fires whenever the old name-slug is still present — including installs where an earlier release already moved the unique_id (it was gated on the now-new unique_id and silently no-op'd; this was the rc1→rc2 regression). The dashboard resolves correctly regardless, but it keeps entity IDs tidy.
  • README: a note that the generated YAML is a point-in-time snapshot — re-run after renames / area moves.

Tests

New coverage for the resolver rewrite (incl. the greedy-sibling case), a registry-rename regression that simulates the area prefix, and the rc1→rc2 migration path. Full suite green.

Caveats

  • Diagnosis was confirmed against a live 2026.6 install (registry inspection) and the HA-core source diff, but I haven't yet run generate_dashboard end-to-end on live EMS/inverter hardware with this build installed — that's the real ground-truth and worth a check once it's on a test instance.
  • Supersedes the yanked v1.1.3rc1 / v1.1.3rc2 pre-releases. A fresh pre-release should follow once this merges.
  • Relates to Add EMS device support — sensor descriptors and config-entry handling #52.

🤖 Generated with Claude Code

…aming)

HA 2026.6 folds a device's area into generated entity_ids
(`sensor.loft_givenergy_inverter_…`), and users may rename entities — so the
dashboard generator's predicted `givenergy_<kind>_<serial>_<slug>` ids no longer
match what HA registers, leaving every tile "Entity not available".

generate_dashboard now takes a resolve_entity_id callback and rewrites each
reference to the actual registry id in a final pass; the generate_dashboard
service builds the resolver from the entity+device registries (canonical id ->
real id, keyed on the stable device name + original entity name). Bump
DASHBOARD_VERSION to 11 so existing installs are prompted to regenerate.

Also make _migrate_unique_ids idempotent: the p_grid_out -> grid_power
entity_id rename now fires whenever the old name-slug is still present,
including installs where an earlier release already migrated the unique_id.
That rename was gated on the now-new unique_id and silently no-op'd for anyone
who had installed a prior rc (the rc1->rc2 breakage).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR introduces entity ID resolution for dashboard generation to handle Home Assistant 2026.6+ area-prefixed entity IDs and user renames. The integration now builds a canonical-to-actual entity ID mapping and applies it when generating Lovelace YAML, while simultaneously reworking entity migrations to be idempotent and collision-safe.

Changes

Entity ID Resolution System

Layer / File(s) Summary
Dashboard Generator Contract & Version Update
custom_components/givenergy_local/__init__.py, custom_components/givenergy_local/dashboard.py
Added Callable and slugify imports; bumped DASHBOARD_VERSION to 11; defined _ENTITY_REF regex to match canonical entity IDs; added `resolve_entity_id: Callable[[str], str]
Entity ID Rewriting in Generated YAML
custom_components/givenergy_local/dashboard.py
Post-processing pass in generate_dashboard that substitutes canonical givenergy_* entity references with actual registry IDs when resolver is provided, using regex matching to avoid partial/greedy rewrites.
Resolver Building & Idempotent Entity Migration
custom_components/givenergy_local/__init__.py
Introduced _build_entity_id_resolver helper that maps canonical dashboard entity IDs (from device/integration names and entity original names) to actual registered entity IDs via config-entry entity registry. Reworked _migrate_unique_ids to handle both unique_id suffix renames and entity_id slug renames idempotently, with collision checks that skip clobbering existing registry entries.
Dashboard Service Wiring
custom_components/givenergy_local/__init__.py
Updated SERVICE_GENERATE_DASHBOARD handler to iterate over (entry_id, coordinator) pairs, construct per-entry resolver, and pass resolve_entity_id into generate_dashboard so produced YAML references actual entity IDs.
Resolver & Migration Test Coverage
tests/test_dashboard.py, tests/test_script_entity_refs.py, tests/test_sensor.py
Added _ANY_REF regex to match HA entity references across domains including area-prefixed forms. Tests verify: canonical-only emission without resolver, rewrite-to-registry with resolver while leaving unknown refs unchanged, no truncation of longer sibling IDs, integration-level resolution under HA area-prefixing, and idempotent migration when unique_id is already updated.
User Documentation
README.md
Clarified that generated dashboard YAML is a snapshot of entity IDs at generation time; explained that HA 2026.6+ derives entity IDs from device area; instructed users to re-run dashboard generation after device area changes, entity renames, or entity ID recreation.

Sequence Diagram

sequenceDiagram
  participant User
  participant Service as SERVICE_GENERATE_DASHBOARD
  participant Resolver as _build_entity_id_resolver
  participant Registry as Entity Registry
  participant Dashboard as generate_dashboard
  participant YAML as Lovelace YAML
  
  User->>Service: Request dashboard generation
  Service->>Registry: Get config-entry entity registry
  Service->>Resolver: Build resolver mapping (canonical → actual)
  Resolver->>Registry: Read device names, entity IDs, unique IDs
  Resolver-->>Service: Return resolver function
  Service->>Dashboard: Call with resolve_entity_id=resolver
  Dashboard->>YAML: Generate YAML with canonical IDs
  Dashboard->>Dashboard: Rewrite canonical IDs via regex
  Dashboard->>YAML: Apply resolver mapping to references
  Dashboard-->>Service: Return YAML with actual registered IDs
  Service-->>User: Deliver dashboard YAML
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • dewet22/givenergy-hass#113: Updates the inverter-output→PV generation unique-id suffix remaps alongside the main PR's reworked _migrate_unique_ids logic and dashboard entity-id resolution.
  • dewet22/givenergy-hass#96: Also modifies generate_dashboard to produce correct entity references in EMS-related Lovelace YAML; main PR adds canonical→registered entity-ID rewriting while #96 focuses on EMS-specific dashboard selection and repair.
  • dewet22/givenergy-hass#121: Also updates SERVICE_GENERATE_DASHBOARD and generate_dashboard signature/version, but focuses on capability-gated cards rather than entity-id resolution.

Poem

🐰 The dashboard now speaks the language of Home Assistant's heart,
Converting canonical names to registered IDs, perfectly smart.
When areas shift and entities rename, no fear—
Regenerate once, and all references are clear!
Entity migrations march idempotently forth,
No collisions to trip up, just seamless rebirth. 🌱

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 72.22% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing dashboard entity ID resolution to handle Home Assistant 2026.6's new area-based naming scheme.
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ 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 claude/priceless-cerf-3608b8

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the dashboard generation to support Home Assistant 2026.6 area-prefixed entity IDs and user renames by resolving canonical entity IDs to actual registry IDs. It also refactors unique ID and entity ID migrations to be independent and idempotent. A review comment suggests using device.original_name instead of device.name in the entity ID resolver to avoid breaking dashboard cards when a user renames their device.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread custom_components/givenergy_local/__init__.py
@deepsource-io

deepsource-io Bot commented Jun 5, 2026

Copy link
Copy Markdown

DeepSource Code Review

We reviewed changes in 570d50c...cc6121d on this pull request. Below is the summary for the review, and you can see the individual issues we found as inline review comments.

See full review on DeepSource ↗

PR Report Card

Overall Grade   Security  

Reliability  

Complexity  

Hygiene  

Code Review Summary

Analyzer Status Updated (UTC) Details
Python Jun 5, 2026 10:18a.m. Review ↗

Important

AI Review is run only on demand for your team. We're only showing results of static analysis review right now. To trigger AI Review, comment @deepsourcebot review on this thread.

@dewet22 dewet22 merged commit 8d0432a into main Jun 5, 2026
10 checks passed
@dewet22 dewet22 deleted the claude/priceless-cerf-3608b8 branch June 5, 2026 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant