Skip to content

Fix shorthand template conditions in choose blocks crashing all automations#171018

Merged
frenck merged 2 commits into
devfrom
frenck-2026-0583
May 17, 2026
Merged

Fix shorthand template conditions in choose blocks crashing all automations#171018
frenck merged 2 commits into
devfrom
frenck-2026-0583

Conversation

@frenck
Copy link
Copy Markdown
Member

@frenck frenck commented May 17, 2026

Proposed change

When a shorthand template condition (bare Jinja2 string) is used inside a choose, or, and, or not block, async_validate_condition_config crashes with TypeError: string indices must be integers, not 'str' because it expects a dict but receives a raw string.

The schema's dynamic_template_condition normally converts bare template strings to {condition: "template", value_template: "..."} dicts, but this conversion doesn't run when async_validate_condition_config recurses into nested condition lists. One malformed condition in one automation takes down all automations globally.

The fix handles the bare string case defensively in async_validate_condition_config by converting it to the proper template condition dict before accessing dict keys.

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

  • 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 May 17, 2026 19:24
@frenck frenck requested a review from a team as a code owner May 17, 2026 19:24
@home-assistant home-assistant Bot added bugfix cla-signed core small-pr PRs with less than 30 lines. labels May 17, 2026
@frenck frenck added this to the 2026.5.3 milestone May 17, 2026
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 aims to prevent shorthand template conditions inside nested condition blocks from crashing automation validation.

Changes:

  • Allows async_validate_condition_config to accept raw string shorthand conditions.
  • Converts raw shorthand strings into template condition dictionaries.
  • Adds a regression test for shorthand template conditions inside an or block.

Reviewed changes

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

File Description
homeassistant/helpers/condition.py Adds defensive handling for raw shorthand template condition strings.
tests/helpers/test_condition.py Adds coverage for validating a shorthand template condition nested in an or condition.

Comment thread homeassistant/helpers/condition.py Outdated
…emplate

The previous fix stored value_template as a plain string, which crashes
at runtime because async_template calls .async_render_to_info() on it.
Also extend the test to actually evaluate the condition.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@frenck frenck merged commit db0006c into dev May 17, 2026
48 checks passed
@frenck frenck deleted the frenck-2026-0583 branch May 17, 2026 21:25
@github-actions github-actions Bot locked and limited conversation to collaborators May 18, 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.

Shorthand template conditions in choose blocks cause TypeError in async_validate_condition_config

3 participants