Skip to content

Include referenced labels when searching automations and scripts#175459

Merged
balloob merged 1 commit into
devfrom
frenck/search-automation-script-labels
Jul 8, 2026
Merged

Include referenced labels when searching automations and scripts#175459
balloob merged 1 commit into
devfrom
frenck/search-automation-script-labels

Conversation

@frenck

@frenck frenck commented Jul 3, 2026

Copy link
Copy Markdown
Member

Proposed change

Searching a label in the search integration returns the automations and scripts that target it, through automations_with_label and scripts_with_label. The reverse direction is missing: searching an automation or script returns the floors, areas, devices, and entities it targets, but not the labels it targets, even though the labels_in_automation and labels_in_script helpers exist. The label edge only worked one way.

This change adds the missing lookups to _async_search_automation and _async_search_script, following the exact same pattern the floor lookups already use. Since _async_search_script is also invoked for scripts embedded in automations and for nested scripts, those cases inherit the targeted labels as well.

Tests: new assertions for searching automation.label and script.label, fixtures that already existed in the test but were never searched from this direction. Both fail without the fix.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Copilot AI review requested due to automatic review settings July 3, 2026 09:27
@frenck frenck requested a review from a team as a code owner July 3, 2026 09:27
@home-assistant

home-assistant Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (search) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of search can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign search Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

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 an asymmetry in the search integration. Searching a label already returns the automations and scripts that target it (via automations_with_label / scripts_with_label), but searching an automation or script did not return the labels it targets, even though the labels_in_automation and labels_in_script helpers already existed. This change adds the missing reverse lookups, following the same pattern used for floors.

Changes:

  • Add ItemType.LABEL results from labels_in_automation to _async_search_automation.
  • Add ItemType.LABEL results from labels_in_script to _async_search_script (which also propagates to scripts embedded in automations and nested scripts).
  • Add test assertions for searching automation.label and script.label, reusing existing fixtures.

Reviewed changes

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

File Description
homeassistant/components/search/init.py Adds the missing targeted-label lookups to the automation and script search paths, mirroring the floor lookup pattern.
tests/components/search/test_init.py Adds assertions verifying that searching an automation/script returns its targeted labels.

I verified that:

  • The automation.labels_in_automation and script.labels_in_script helpers exist and return list[str], which _add handles correctly.
  • The placement is consistent with the adjacent floor lookups (floors_in_automation / floors_in_script), including that referenced labels are added unconditionally (not gated on entry_point), so nested/embedded scripts contribute their labels — matching the floor behavior and the PR's stated intent.
  • The edge is now symmetrical with _async_search_label, which already returns automations/scripts referencing the label.
  • The test fixtures (automation.label targeting label_christmas, script.label targeting label_other) match the asserted results, and neither fixture targets additional entities that would change the expected result set.

No issues found.

@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown

Caution

Review failed

An error occurred during the review process. Please try again later.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch frenck/search-automation-script-labels

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.

1 similar comment
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown

Caution

Review failed

An error occurred during the review process. Please try again later.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch frenck/search-automation-script-labels

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.

@balloob balloob merged commit 72c3f90 into dev Jul 8, 2026
34 checks passed
@balloob balloob deleted the frenck/search-automation-script-labels branch July 8, 2026 07:00
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants