Skip to content

Accept more templates in service fields#150239

Merged
emontnemery merged 3 commits into
home-assistant:devfrom
arturpragacz:h/config-validation/accept-more-templates-in-service-fields
Nov 3, 2025
Merged

Accept more templates in service fields#150239
emontnemery merged 3 commits into
home-assistant:devfrom
arturpragacz:h/config-validation/accept-more-templates-in-service-fields

Conversation

@arturpragacz
Copy link
Copy Markdown
Member

@arturpragacz arturpragacz commented Aug 7, 2025

Proposed change

The templated initial schema was made to allow templates also on the level of individual keys.

Currently both of the following work correctly:

target: "{{ {'device_id': [var1, var2]} }}"
target:
  device_id:
    - "{{ var1 }}"
    - "{{ var2 }}"

but the following fails:

target:
  device_id: "{{ [var1, var2] }}"

This PR fixes that problem.

Alternative to #149911.

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

Checklist

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

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented Aug 7, 2025

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

Code owner commands

Code owners of websocket_api can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign websocket_api Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 6, 2025

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@arturpragacz arturpragacz force-pushed the h/config-validation/accept-more-templates-in-service-fields branch from 0561931 to faa2402 Compare October 30, 2025 17:49
@arturpragacz arturpragacz marked this pull request as ready for review October 30, 2025 18:04
@arturpragacz arturpragacz requested a review from a team as a code owner October 30, 2025 18:04
Copilot AI review requested due to automatic review settings October 30, 2025 18:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 adds support for using dynamic templates that evaluate to lists directly in target service fields (area_id, device_id, floor_id, label_id), without requiring a mixed list syntax. Previously, only entity_id supported this pattern where a single template could return a list.

Key Changes

  • Extended the _TARGET_SERVICE_FIELDS_TEMPLATED schema to allow dynamic_template as a standalone option for area_id, device_id, floor_id, and label_id fields
  • Added test coverage for the new functionality showing templates that evaluate to lists (e.g., "{{ ['area-42', 'area-51'] }}")

Reviewed Changes

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

File Description
homeassistant/helpers/config_validation.py Added dynamic_template as a valid schema option for target service fields (ATTR_DEVICE_ID, ATTR_AREA_ID, ATTR_FLOOR_ID, ATTR_LABEL_ID) to allow templates that return lists
tests/helpers/test_service.py Added test case verifying that templates evaluating to lists work correctly for all target fields

Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Looks good, but consider improving comments in the tests.
Also, the new functionality should be documented.

Comment thread tests/helpers/test_service.py
Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @arturpragacz 👍

@emontnemery emontnemery merged commit fad2178 into home-assistant:dev Nov 3, 2025
227 of 230 checks passed
@arturpragacz arturpragacz deleted the h/config-validation/accept-more-templates-in-service-fields branch November 3, 2025 18:48
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 4, 2025
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