Extract base classes for unknown-reference repair boilerplate#1255
Extract base classes for unknown-reference repair boilerplate#1255frenckatron wants to merge 1 commit into
Conversation
Ten repair modules ground out near-identical async_inspect bodies — six under ectoplasms/automation/repairs/unknown_*_references.py and four unknown_source.py files across switch_as_x, integration, utility_meter and trend. Each variant differed only in the registry queried, the attribute looked up on the entity, and a placeholder key. This pulls the boilerplate into two abstract bases on repairs.py: - AbstractSpookEntityComponentUnknownReferencesRepair walks an EntityComponent (DATA_INSTANCES) and exposes three hooks: _async_setup_inspection (cache known IDs once per cycle), _should_inspect_entity (defaults True, overridden for "skip disabled automations"), and _async_compute_unknown_references (per-entity lookup). The issue payload and debug logging are built once at the base. - AbstractSpookEntityPlatformUnknownSourceRepair walks an EntityPlatform (DATA_ENTITY_PLATFORM), optionally filtered to a single source_platform_domain, and asks subclasses only for the per-entity _get_source_entity_id. Net: ~440 lines deleted, ~155 of base infra added. The 10 leaf modules now read as the ~5 attributes that actually differentiate them. A side effect is that pylint's duplicate-code can be re-enabled without flooding the report. The four remaining pre-existing duplicates outside this mission's scope (person services pair, proximity repairs pair) carry a local pylint: disable=duplicate-code explaining the intent; min-similarity-lines is also bumped to 12 so trivial header overlap (imports, inspect_events sets) doesn't trigger. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
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 |
|



What
Pulls the duplicated
async_inspectboilerplate out of 10 repair modules into two abstract base classes onrepairs.py, and re-enables pylint'sduplicate-codecheck that was disabled because of it.Why
Six
ectoplasms/automation/repairs/unknown_*_references.pyfiles and fourunknown_source.pyfiles (switch_as_x, integration, utility_meter, trend) were structurally identical — they differed only in the registry queried, the attribute looked up on the entity, and one placeholder key. The shared code blockedduplicate-codefrom being usefully enabled.How
Two abstract bases in
custom_components/spook/repairs.py:AbstractSpookEntityComponentUnknownReferencesRepairwalks anEntityComponent(DATA_INSTANCES). Subclasses provide three hooks:_async_setup_inspection— cache known IDs once per inspection cycle._should_inspect_entity— defaultsTrue; overridden inunknown_entity_referencesto skip disabled automations._async_compute_unknown_references— per-entity lookup, returns the set of unknown IDs.The base owns the unavailable-entity skip, the issue payload (with configurable
entity_label,reference_label,edit_url_pattern) and the debug log.AbstractSpookEntityPlatformUnknownSourceRepairwalks anEntityPlatform(DATA_ENTITY_PLATFORM), optionally filtered to a singlesource_platform_domain. Subclasses only implement_get_source_entity_idto expose the private source attribute.Each leaf module is now ~45 lines that read as the 5 attributes that actually differentiate them.
Side effects:
duplicate-codeis back on the active pylint checks.min-similarity-lines = 12andignore-imports/ignore-signaturesare set so trivial header overlap (imports,inspect_events = {...}) doesn't trigger.person/services/{add,remove}_device_tracker.pypair and theproximity/repairs/unknown_{ignored_zones,tracked_entities}.pypair). They carry a one-line# pylint: disable=duplicate-codewith the reason; deduping them is a separate concern.Testing
uv run ruff check custom_components/→ clean.uv run ruff format --check custom_components/→ clean.uv run pre-commit run pylint --all-files→ only the pre-existingasync_timeoutimport-errorinblueprint/services/importer.py(unrelated, depends on Python <3.13.2; CI installs it transitively). 0duplicate-codewarnings after the refactor; main has the rule disabled altogether.uv run python -c "import …".Quality Report
Changes: 16 files changed, 357 insertions(+), 436 deletions(-)
Code scan: clean
Tests: failed (command not found)
Branch hygiene: 1 issue(s)
Generated by Kōan post-mission quality pipeline