-
Notifications
You must be signed in to change notification settings - Fork 54
refactor: use Home Assistant Store for data persistence #550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: beta
Are you sure you want to change the base?
Conversation
Replace custom JSON file handling with Home Assistant's built-in Store helper for data persistence. This provides: - Atomic writes (prevents corruption) - Proper async operations - Standard `.storage/keymaster.locks` location - Built-in versioning for migrations Migration support included: on first startup, any existing `json_kmlocks/keymaster_kmlocks.json` file is automatically migrated to the new Store format and deleted. Changes: - Add Store import and STORAGE_VERSION/STORAGE_KEY constants - Replace _json_folder/_json_filename with _store instance - Add _async_load_data() with legacy JSON migration - Add _async_save_data() and async_remove_data() methods - Remove _create_json_folder, _get_dict_from_json_file, _write_config_to_json, and delete_json methods - Update __init__.py to use async_remove_data() - Update test to patch new method name Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## beta #550 +/- ##
==========================================
- Coverage 80.86% 80.48% -0.38%
==========================================
Files 19 21 +2
Lines 2341 2501 +160
==========================================
+ Hits 1893 2013 +120
- Misses 448 488 +40
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:
|
There was a problem hiding this 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 refactors the keymaster integration to use Home Assistant's built-in Store helper for data persistence instead of custom JSON file handling. The change includes automatic migration from the legacy JSON format located in custom_components/keymaster/json_kmlocks/keymaster_kmlocks.json to the new .storage/keymaster.locks location.
Changes:
- Replaced manual JSON file operations with
StoreAPI for atomic, async data persistence - Added migration logic to automatically convert legacy JSON files to the new storage format
- Updated all method calls from
_write_config_to_json()to_async_save_data()anddelete_json()toasync_remove_data()
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| custom_components/keymaster/coordinator.py | Replaces custom JSON file handling with Store API, adds migration logic, removes old sync file operation methods |
| custom_components/keymaster/init.py | Updates coordinator cleanup to use new async_remove_data() method |
| tests/test_coordinator_lifecycle.py | Updates test mock to patch new _async_save_data method |
| custom_components/keymaster/time.py | Removes unused KeymasterCodeSlot import |
| custom_components/keymaster/services.py | Removes unused functools import |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Change 'if config:' to 'if config is not None:' to ensure empty legacy JSON files are properly migrated and deleted. An empty dict is a valid state (no configured locks) and should be migrated. Addresses Copilot review comment.
Combine the two separate executor calls for file deletion and folder removal into a single _cleanup_legacy_files() method. Reduces context switching overhead during migration.
Separate migration logic from cleanup - now the legacy file is always deleted when found, regardless of whether it contained valid data. This prevents users with corrupted legacy files from hitting the same error repeatedly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Combine load and cleanup into one `_migrate_legacy_json` method to reduce context switches between async and sync code. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary
StorehelperBackground
Previously, keymaster persisted lock data (PINs, code slot settings, etc.) to a custom JSON file at
custom_components/keymaster/json_kmlocks/keymaster_kmlocks.json. This worked but had several issues:async_add_executor_job()wrappers for file operations.storage/directoryWhat Changed
New Architecture
The new implementation uses
homeassistant.helpers.storage.Store:_create_json_folder()_get_dict_from_json_file()_async_load_data()_write_config_to_json()_async_save_data()delete_json()async_remove_data()Why Store is Better
.storage/alongside other HA data, making backups easierSTORAGE_VERSIONconstant enables future schema migrationsMigration
On first startup after this update:
json_kmlocks/keymaster_kmlocks.jsonexistsUsers don't need to do anything - migration is automatic and seamless.
Type of change
Test plan
🤖 Generated with Claude Code