fix: resolve KeyError when YAML-mode resources lack 'id' field#561
fix: resolve KeyError when YAML-mode resources lack 'id' field#561raman325 wants to merge 1 commit intoFutureTense:betafrom
Conversation
There was a problem hiding this comment.
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
StopIterationpattern withany()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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
3cb696c to
0f62573
Compare
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 inasync_register_strategy_resourcecrashed ondata['id']before theStopIterationhandler (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:idaccess needed)Type of change
Additional information