Skip to content

Detect unknown actions in template helpers - #1397

Merged
frenck merged 7 commits into
frenck:mainfrom
cnotin:agent/template-helper-unknown-actions
Aug 20, 2026
Merged

frenck merged 7 commits into
frenck:mainfrom
cnotin:agent/template-helper-unknown-actions

Conversation

@cnotin

@cnotin cnotin commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

  • Detect unavailable actions referenced by Template helper action options.
  • Link the affected Template helper from both Template repair notifications to Home Assistant's Helpers page.
  • Add a Template helpers documentation page. It documents this new action detection and the pre-existing unknown-entity detection.

Motivation and Context

The existing Template repair only inspects Jinja template strings for unknown entities. It does not inspect Home Assistant action sequences stored in Template helper options, such as a Template Button's press option.

This fixes a missing script action (script.something) that was referenced by one of my Template Button but was not reported by Spook.

Related discussion

Discussion #1285 identified the companion problem of unknown entity references in UI-created Template helpers. That coverage was added in #1367. This PR complements it by detecting unavailable actions/services in Template helper action sequences, including press, turn_on, and turn_off; action names templated at runtime are intentionally skipped.

How has this been tested?

  • Added regression coverage for a Template Button with a missing script action, a Template Switch with turn_on/turn_off actions, and a templated action name.
  • Ran Ruff and Python compilation checks.
  • Built the documentation site with myst build --html.
  • Tested on my own Home Assistant installation: Spook detected the previously missed missing action above. No other new Repairs appeared, so there was no observed regression on that installation.
  • Verified that the repair link opens the Helpers page.

Screenshots (if appropriate):

Repair notification

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.

Copilot AI balanced review requested due to automatic review settings August 3, 2026 12:15
@coderabbitai

coderabbitai Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 79292b9b-854c-4dc8-9619-316e7158d5c8

📥 Commits

Reviewing files that changed from the base of the PR and between e292ef3 and 35c6044.

📒 Files selected for processing (4)
  • custom_components/spook/ectoplasms/template/repairs/unknown_entity_references.py
  • custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py
  • custom_components/spook/translations/en.json
  • tests/ectoplasms/template/repairs/test_unknown_service_references.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • custom_components/spook/translations/en.json

Included review availability: Your plan provides up to 4 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

Template helper repairs now inspect structured action configurations for unknown entity and service references. They distinguish disabled-only entity references, validate action options, update repair issues as services change, and document the resulting diagnostics.

Changes

Template helper repairs

Layer / File(s) Summary
Structured entity reference handling
custom_components/spook/ectoplasms/template/repairs/unknown_entity_references.py
Entity extraction now scans structured actions and distinguishes references found only in disabled steps.
Unknown service detection and validation
custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py, tests/ectoplasms/template/repairs/test_unknown_service_references.py
A new repair validates action options, detects unavailable services in nested configurations, ignores templated actions, and clears issues when services become available.
Repair presentation and documentation
custom_components/spook/translations/en.json, documentation/_toc.yml, documentation/integrations/template.md
Translations and documentation describe unknown entity and action repairs, remediation steps, and helper edit links.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 35c60

This change adds detection for unavailable actions in Template helpers and links the affected helper from repair notifications; no actionable merge-blocking risk remains beyond normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant HomeAssistantEvents
  participant SpookRepair
  participant async_find_services_in_sequence
  participant RepairIssueRegistry
  HomeAssistantEvents->>SpookRepair: trigger configuration, reload, or service changes
  SpookRepair->>async_find_services_in_sequence: inspect normalized helper actions
  async_find_services_in_sequence-->>SpookRepair: referenced services
  SpookRepair->>RepairIssueRegistry: create or remove unknown-service issues
Loading

Possibly related PRs

  • frenck/spook#1076: Addresses service and action names that were classified as unknown entities.
  • frenck/spook#1369: Updates issue descriptions in unknown_entity_references.py.
  • frenck/spook#1399: Introduces the related template helper action-reference repair.

Suggested reviewers: frenck

Poem

I hop through actions, neat and bright,
And flag unknown services in sight.
Disabled entities get a careful note,
While repaired helpers smoothly float.
Documentation blooms—what a delight!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: detecting unknown actions in Template helpers.
Description check ✅ Passed The description directly explains the changes, motivation, testing, documentation, and intended handling of templated actions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Adds unknown-action detection to Template integration helpers.

Changes:

  • Scans template-helper action sequences for unavailable services.
  • Adds repair translations and regression tests.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
unknown_service_references.py Implements action-reference inspection.
en.json Adds repair messaging.
test_unknown_entity_references.py Tests button and switch actions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings August 3, 2026 12:18

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (1)

custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py:51

  • Template helper options are raw config, so a templated action such as {"action": "{{ action_name }}"} remains a plain string. This adds that Jinja expression to services, and async_filter_known_services consequently reports it as an unavailable action. Skip is_template_string(...) values when walking these raw sequences (and add a regression case), as the existing raw-config warning in tests/test_entity_filtering.py:73-75 requires.
                services.update(async_find_services_in_sequence(option))

Copilot AI review requested due to automatic review settings August 3, 2026 12:25

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 3, 2026 12:36
coderabbitai[bot]
coderabbitai Bot previously approved these changes Aug 3, 2026

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 20, 2026 06:59
@codecov-commenter

codecov-commenter commented Aug 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.85%. Comparing base (f5c6b8b) to head (35c6044).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1397      +/-   ##
==========================================
+ Coverage   84.49%   84.85%   +0.35%     
==========================================
  Files         152      153       +1     
  Lines        4419     4449      +30     
  Branches      564      567       +3     
==========================================
+ Hits         3734     3775      +41     
+ Misses        576      568       -8     
+ Partials      109      106       -3     

☔ 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.

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

frenck added 2 commits August 20, 2026 09:42
Two conflicts, both from frenck#1399 and frenck#1400 landing:

- en.json keeps main's "Its configuration references" wording, with the
  Helpers link layered on top.
- The tests move into test_unknown_service_references.py, one file per
  repair as the script ectoplasm already does. That is also what stops
  this conflict recurring, since both sides were appending to the same
  file.
async_find_services_in_sequence was written for validated script
sequences and indexes keys that only exist after cv.SCRIPT_SCHEMA has
run. This repair was the first caller handing it raw config entry
options, and it raised on shapes the action editor writes every day:

  parallel shorthand   KeyError: 'sequence'
  bare repeat          KeyError: 'sequence'
  if without then      KeyError: 'then'
  a step with an alias and nothing else   ValueError

The exception escaped async_inspect and repairs.py re-raises it, so one
such helper meant every config entry after it went uninspected and the
repair reported nothing at all, with a traceback on every trigger.

Validating first fixes all of them, and turns parallel shorthand from
fatal into a real detection. It also replaces two heuristics with a real
answer: the isinstance() shape guard, and the is_template_string filter,
since a templated action name validates into a Template and a Template is
not a string, so the known-services filter drops it already.

Also drop the entity registry subscription. Service availability does not
track the entity registry, script entities appearing already fire
EVENT_SERVICE_REGISTERED, and neither the script nor the automation
service repair listens for it.

And say "It references the following actions" rather than "Its actions
reference the following actions", matching the script repair.
Copilot AI review requested due to automatic review settings August 20, 2026 09:43
@frenck frenck added the new-feature New features or options. label Aug 20, 2026
@sonarqubecloud

Copy link
Copy Markdown

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Suppressed comments (3)

custom_components/spook/ectoplasms/template/repairs/unknown_service_references.py:62

  • This walker does not recurse into Home Assistant's valid sequence action type, so a helper option such as [{"sequence": [{"action": "script.missing"}]}] validates successfully but yields no service and no repair. Extend async_find_services_in_sequence to handle cv.SCRIPT_ACTION_SEQUENCE via step[CONF_SEQUENCE], and add this shape to the regression test.
                services.update(async_find_services_in_sequence(sequence))

documentation/integrations/template.md:46

  • Use the singular attributive noun “Feature” in this heading.
## Features requests, ideas, and support

custom_components/spook/translations/en.json:500

  • “Crossed path” should use the idiom’s plural form, “crossed paths.”
      "description": "Spook has found a ghost in your template helpers \ud83d\udc7b\n\nWhile floating around, Spook crossed path with the following template helper:\n\n[{helper}]({edit})\n\nIt references the following actions, which are unknown to Home Assistant:\n\n{services}\n\n\n\nThis often happens when an action provider was removed, or a script was renamed or deleted. To fix this error, reconfigure the template helper to use existing actions.\n\nSpook \ud83d\udc7b Your homie.",

@frenck frenck left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @cnotin 👍

../Frenck

                       

Blogging my personal ramblings at frenck.dev

@frenck
frenck merged commit 1da158f into frenck:main Aug 20, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature New features or options.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants