Skip to content

fix: resolve KeyError when YAML-mode resources lack 'id' field#561

Open
raman325 wants to merge 1 commit intoFutureTense:betafrom
raman325:fix/yaml-resource-keyerror
Open

fix: resolve KeyError when YAML-mode resources lack 'id' field#561
raman325 wants to merge 1 commit intoFutureTense:betafrom
raman325:fix/yaml-resource-keyerror

Conversation

@raman325
Copy link
Collaborator

Summary

Fixes a KeyError: 'id' exception during HA startup when users have YAML-mode Lovelace resources with the keymaster resource already manually added.

Proposed change

YAML-mode Lovelace resources don't include an 'id' field — they only have 'url' and 'type'. When users followed the warning message and manually added the keymaster resource to their YAML config, the generator expression in async_register_strategy_resource crashed on data['id'] before the StopIteration handler (which correctly handles YAML mode) could run.

Restructured the function to check resource existence by URL first using any() (works for both YAML and storage collection types), then branch on the result:

  • Already registered → log and return (no id access needed)
  • Not registered + YAML mode → warn user to add manually
  • Not registered + storage mode → create the resource automatically

Type of change

  • Bugfix (non-breaking change which fixes an issue)

Additional information

Copilot AI review requested due to automatic review settings February 17, 2026 16:17
@raman325 raman325 linked an issue Feb 17, 2026 that may be closed by this pull request
Copy link

Copilot AI left a comment

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 fixes a KeyError: 'id' crash that occurred during Home Assistant startup when users had YAML-mode Lovelace resources with the keymaster resource already manually added. The fix restructures the resource registration logic to check for existing resources by URL before attempting to access the 'id' field, which doesn't exist in YAML-mode resources.

Changes:

  • Replaced try-except StopIteration pattern with any() check for resource existence
  • Reordered logic to handle "already registered" case before checking YAML vs storage mode
  • Added test coverage for the YAML-mode scenario where the resource already exists

Reviewed changes

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

File Description
custom_components/keymaster/resources.py Restructured async_register_strategy_resource to check resource existence by URL first, avoiding KeyError when accessing non-existent 'id' field in YAML-mode resources
tests/test_resources.py Added test case to verify no error occurs when the resource already exists in YAML mode

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

YAML-mode Lovelace resources don't have an 'id' field, only 'url' and
'type'. When users manually added the keymaster resource (as instructed
by the warning message), the generator expression crashed on
data['id'] before the StopIteration handler could run.

Restructured async_register_strategy_resource to check resource
existence by URL first (works for both YAML and storage modes),
then branch on the result.

Fixes FutureTense#560

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov-commenter
Copy link

codecov-commenter commented Feb 17, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.57%. Comparing base (a9a30ef) to head (0f62573).
⚠️ Report is 18 commits behind head on beta.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             beta     #561      +/-   ##
==========================================
+ Coverage   80.86%   84.57%   +3.71%     
==========================================
  Files          19       25       +6     
  Lines        2341     2730     +389     
==========================================
+ Hits         1893     2309     +416     
+ Misses        448      421      -27     
Flag Coverage Δ
python 84.57% <100.00%> (?)

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

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

@raman325 raman325 force-pushed the fix/yaml-resource-keyerror branch from 3cb696c to 0f62573 Compare February 17, 2026 16:18
@raman325 raman325 added the bugfix Fixes a bug label Feb 17, 2026
Copy link
Collaborator

@firstof9 firstof9 left a comment

Choose a reason for hiding this comment

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

Looks good to me.

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

Labels

bugfix Fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ISSUE: KeyError: 'id' exception during HA start

3 participants