Purge orphaned database entities - #1313
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Pull request overview
This PR fixes incomplete orphaned-entity cleanup by ensuring orphaned recorder (states_meta) entities are actually purged via Home Assistant’s recorder.purge_entities service, while keeping the existing local cleanup of restored entities. It also moves the recorder DB query for listing orphaned database entities off the event loop by running it in an executor.
Changes:
- Update
delete_all_orphaned_entitiesto callrecorder.purge_entitiesfor orphaned recorder entities (keep_days=0), preserving context. - Refactor
list_orphaned_database_entitiesto compute orphaned IDs via an executor job helper instead of running SQLAlchemy work in the service handler. - Add tests for the delete action to verify recorder purge invocation and continued removal of restored entities.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
custom_components/spook/ectoplasms/homeassistant/services/delete_all_orphaned_entities.py |
Purges orphaned recorder entities via recorder.purge_entities before performing restored-entity local cleanup. |
custom_components/spook/ectoplasms/homeassistant/services/list_orphaned_database_entities.py |
Introduces helper(s) and runs the recorder DB query in an executor, returning set-difference results. |
tests/ectoplasms/homeassistant/services/test_orphaned_entities.py |
Adds coverage for delete behavior (purge call + restored-state removal remains intact). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
b51d8ca to
76d7335
Compare
|
|
I was seeing delete orphaned entries not work, applying the diff from this PR identified more than there was and worked. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1313 +/- ##
==========================================
+ Coverage 68.27% 68.66% +0.38%
==========================================
Files 126 126
Lines 3316 3370 +54
Branches 431 450 +19
==========================================
+ Hits 2264 2314 +50
+ Misses 978 968 -10
- Partials 74 88 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|



Description
The
homeassistant.delete_all_orphaned_entitiesaction now purges orphaned recorder database entities using Home Assistant'srecorder.purge_entitiesservice.The action still removes restored entities from the entity registry and state machine, so the existing cleanup behavior remains in place.
The matching list action now reads recorder metadata through an executor job instead of doing the SQLAlchemy query in the service handler directly.
Motivation and Context
The list action reports entity IDs that exist in recorder's
states_metatable but no longer exist in the state machine. The delete action was not deleting those rows. It only removed restored state entities, which is a different cleanup path.That made the action appear successful while
homeassistant.list_orphaned_database_entitiescould still return the same entries afterward.Fixes #1136.
How has this been tested?
uv run pytest tests/ectoplasms/homeassistant/services/test_orphaned_entities.py -quv run pytest tests/ectoplasms/homeassistant/services -quv run ruff format --check .uv run ruff check --output-format=github .uv run --with pre-commit pre-commit run pylint --files custom_components/spook/ectoplasms/homeassistant/services/delete_all_orphaned_entities.py custom_components/spook/ectoplasms/homeassistant/services/list_orphaned_database_entities.py tests/ectoplasms/homeassistant/services/test_orphaned_entities.pyScreenshots (if appropriate):
Not applicable.
Types of changes
Checklist