Skip to content

Simplify trigger expr - #932

Merged
craigbarratt merged 3 commits into
custom-components:masterfrom
dmamelin:simplify-trigger-expr
Nov 27, 2026
Merged

craigbarratt merged 3 commits into
custom-components:masterfrom
dmamelin:simplify-trigger-expr

Conversation

@dmamelin

Copy link
Copy Markdown
Contributor

Follow-up to #921, as agreed there: keep stripping the blanks around a trigger expression, drop the implicit brackets around multi-line expressions, and document that a multi-line expression goes in brackets, as in Python.

Commits

It's easiest to review them one by one.

  1. Revert the second commit of Let trigger expressions start and end with blanks and span lines (#617) #921. Let trigger expressions start and end with blanks and span lines (#617) #921 was squashed, so a plain git revert isn't possible: this applies the second commit, a2e13b7, and the docs follow-up 933e177 in reverse, resolving the parts that Remove legacy decorators #927 changed since. The code ends up as in the first commit, 1e39bf5, adapted to Remove legacy decorators #927:

    • strip_trigger_expr() moves to decorators/base.py, since decorators/state.py imports from it;
    • the first commit's tests lose their legacy decorator variants.

    eval.py is back to its state before Let trigger expressions start and end with blanks and span lines (#617) #921.

  2. Drop trigger expression tests that only check Python. Some kept tests checked str.strip(), CPython's own errors and AstEval.parse(), or repeated the same code path. The tests that remain cover:

    • blanks around an expression in each kind of decorator;
    • a state name with blanks around it, which still means "any change";
    • a trailing # comment in one of several @state_trigger entries.

    strip_trigger_expr() loses its isinstance check: its arguments are already validated as strings.

  3. Document multi-line trigger expressions. A new section in the @state_trigger docs shows three ways to write a long expression:

    • brackets in a triple-quoted string;
    • adjacent strings;
    • named parts joined with an f-string.

Behavior compared with master

dmamelin and others added 3 commits November 26, 2026 22:23
…in trigger expressions)

custom-components#921 was squashed into 8b3fde3. This reverts its second commit, a2e13b7
("let multi-line trigger expressions span lines without brackets"), and
the docs follow-up 933e177, and keeps its first commit, 1e39bf5 ("let
trigger expressions start and end with blanks").

The implicit brackets read a multi-line trigger expression differently
from Python: a missing operator at a line break is joined with the next
line instead of being a SyntaxError. As in Python, a multi-line trigger
expression needs brackets of its own again.

Adapted to custom-components#927: strip_trigger_expr() now lives in decorators/base.py,
since decorators/state.py imports from it, and the first commit's tests
lose their legacy decorator variants.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tests kept from the first commit of custom-components#921 also checked str.strip(),
CPython's own errors and AstEval.parse(), which this change doesn't
touch, and repeated the same code path for several decorators. What
stays checks what the change adds: blanks around an expression or a
state name are ignored in each kind of decorator, and a trailing
# comment doesn't swallow the next @state_trigger entry.

strip_trigger_expr() no longer checks for non-strings: the arguments
it gets are already validated as strings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds a "Multi-line trigger expressions" section with the ways to write
a long trigger expression: brackets in a triple-quoted string, adjacent
strings, and named parts joined with an f-string. The note in
@state_trigger now points to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@craigbarratt craigbarratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @dmamelin, this matches what we agreed in #921. Approving.

@craigbarratt
craigbarratt merged commit 6a44385 into custom-components:master Nov 27, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants