Skip to content

Return null from RegistryCache.GetLabelById for unknown labels - #1448

Merged
helto4real merged 2 commits into
net-daemon:mainfrom
DevJasperNL:fix/registry-cache-unknown-label
Sep 6, 2026
Merged

Return null from RegistryCache.GetLabelById for unknown labels#1448
helto4real merged 2 commits into
net-daemon:mainfrom
DevJasperNL:fix/registry-cache-unknown-label

Conversation

@DevJasperNL

Copy link
Copy Markdown
Contributor

Proposed change

RegistryCache.GetLabelById used the dictionary indexer, so looking up a label id that is not in the label cache threw a KeyNotFoundException. Every sibling lookup (GetHassEntityById, GetDeviceById, GetAreaById, GetFloorById) already returns null for unknown ids, and HassObjectMapper filters those nulls out with OfType<Label>() when it maps the Labels collections of entities, devices and areas.

Each registry (entities, devices, areas, floors, labels) is reloaded independently and each has its own 5-minute throttle on its *_registry_updated event. This means the entity or area registry can reference a label the label registry has not loaded yet, for example right after a label is created and assigned in the Home Assistant UI. Any app that then touched IHaContext.Registry, entity.Registration, or navigated to an Area/Device threw instead of seeing the entity without that label.

This PR:

  • Changes GetLabelById to use GetValueOrDefault, in line with the other lookups. An unknown label is now simply omitted from the mapped Labels lists, and IHaRegistry.GetLabel returns null for it.
  • Adds TestUnknownLabelIsIgnored to RegistryNavigationTest. An entity and an area reference a label id that is not in the label registry; the test asserts GetLabel returns null, the entity registration keeps only the known label, and the area has no labels. Without the fix it fails with KeyNotFoundException: The given key 'unknown_label' was not present in the dictionary.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • The code compiles without warnings (code quality check)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration are added/changed:

@helto4real

Copy link
Copy Markdown
Collaborator

if you are using an agent for these fixes please make it use correct label

@codecov

codecov Bot commented Sep 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83%. Comparing base (22d654a) to head (e355bc2).

Additional details and impacted files
@@         Coverage Diff          @@
##           main   #1448   +/-   ##
====================================
  Coverage    83%     83%           
====================================
  Files       201     201           
  Lines      4170    4170           
  Branches    486     486           
====================================
+ Hits       3468    3470    +2     
- Misses      497     499    +2     
+ Partials    205     201    -4     
Flag Coverage Δ
unittests 83% <100%> (+<1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

@helto4real helto4real left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The GetValueOrDefault fix is correct and the unknown-label regression test works: all 153 HassModel tests passed locally, and reverting the production change made the new test fail with KeyNotFoundException. Before approval, please add a focused assertion/test that RegistryCache.GetLabelById(null) returns null (or exercise that path through the concrete HaRegistry.GetLabel method). The modified line currently has only partial branch coverage: local coverage recorded 19 executions but zero hits for the null-id branch, consistent with the failing codecov/patch check. Keep the existing known-label and unknown-label assertions, rerun the HassModel suite with coverage, and ensure codecov/patch is green. No production-code change or coverage-threshold reduction is needed; this request is for the missing null-input test.

DevJasperNL and others added 2 commits September 6, 2026 06:39
GetLabelById used the dictionary indexer while every sibling lookup uses
GetValueOrDefault. Each registry is reloaded independently, so an entity,
device or area can reference a label the label cache has not loaded yet,
which made any access to Registry or entity.Registration throw a
KeyNotFoundException. HassObjectMapper already filters null labels.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DGjT5J93Q8hpyZ8RzrCJhk
Covers the null-id branch of RegistryCache.GetLabelById, as requested in review.

Claude-Session: https://claude.ai/code/session_01X4cpEyF6m8EX2EMNweyy1M
@helto4real
helto4real force-pushed the fix/registry-cache-unknown-label branch from 3796bc0 to e355bc2 Compare September 6, 2026 04:39
@helto4real
helto4real enabled auto-merge (squash) September 6, 2026 04:39
@helto4real
helto4real merged commit 6533d12 into net-daemon:main Sep 6, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants