Skip to content

Purge orphaned database entities - #1313

Draft
frenckatron wants to merge 1 commit into
mainfrom
fix/delete-orphaned-database-entities
Draft

Purge orphaned database entities#1313
frenckatron wants to merge 1 commit into
mainfrom
fix/delete-orphaned-database-entities

Conversation

@frenckatron

Copy link
Copy Markdown
Collaborator

Description

The homeassistant.delete_all_orphaned_entities action now purges orphaned recorder database entities using Home Assistant's recorder.purge_entities service.

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_meta table 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_entities could still return the same entries afterward.

Fixes #1136.

How has this been tested?

  • uv run pytest tests/ectoplasms/homeassistant/services/test_orphaned_entities.py -q
  • uv run pytest tests/ectoplasms/homeassistant/services -q
  • uv 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.py

Screenshots (if appropriate):

Not applicable.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@frenckatron frenckatron added the bugfix Inconsistencies or issues which will cause a problem for users or implementors. label Jun 22, 2026
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e256ae8b-83b3-47bc-b3e2-8205b1a8dd45

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/delete-orphaned-database-entities

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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_entities to call recorder.purge_entities for orphaned recorder entities (keep_days=0), preserving context.
  • Refactor list_orphaned_database_entities to 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.

@frenckatron
frenckatron force-pushed the fix/delete-orphaned-database-entities branch from b51d8ca to 76d7335 Compare June 22, 2026 22:17
@sonarqubecloud

Copy link
Copy Markdown

@stevenh

stevenh commented Jul 11, 2026

Copy link
Copy Markdown

I was seeing delete orphaned entries not work, applying the diff from this PR identified more than there was and worked.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.90476% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.66%. Comparing base (f751a9e) to head (76d7335).
⚠️ Report is 267 commits behind head on main.

Files with missing lines Patch % Lines
...istant/services/list_orphaned_database_entities.py 42.85% 8 Missing ⚠️
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.
📢 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.

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

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Orphaned entities are not deleted

4 participants