Commit 1e08f73
* feat: detect last_changed/last_updated duration math and suggest for: field (#1157)
Adds _RE_DURATION_MATH to best_practice_checker.py to detect template
conditions and triggers using now() - X.last_changed/last_updated arithmetic.
These patterns should use the native `for:` field on state triggers instead
(event-driven, no template re-evaluation overhead). Fires in both condition
templates (_check_template_string) and template trigger value_templates
(_check_triggers). Includes 6 new tests covering conditions, triggers,
the positive clean case, skill ref presence, and no-generic-double-flag.
* fix: tighten _RE_DURATION_MATH to require dotted qualifier on both alternations
The second alternation previously matched bare Jinja variables named
`last_changed` (e.g. `{{ last_changed < now() }}`). Require at least one
dotted qualifier on both patterns so only entity attribute access is flagged.
Add two new tests:
- test_trigger_warning_uses_trigger_types_anchor: verifies trigger-path warning
links to #trigger-types (not #native-conditions)
- test_no_false_positive_bare_last_changed_variable: verifies bare Jinja
variable `last_changed < now()` does not trigger the detector
* feat(internal): extend duration math detector to numeric_state value_template
numeric_state triggers can carry a value_template field; duration/recency
math inside that field was previously not flagged. Add a dedicated check
block for platform: numeric_state so the detector covers both template and
numeric_state trigger platforms.
Add test_numeric_state_trigger_value_template_duration_math to verify.
* style: ruff format best_practice_checker and test file
* test: add reversed-comparison duration-math regression tests (#1157)
Cover the `X.last_changed/last_updated < now()` alternation of
_RE_DURATION_MATH in both condition and trigger value_template contexts.
Each test asserts exactly one warning fires, which also guards against the
two alternations double-flagging the same template.
Addresses review feedback on PR #1264.
---------
Co-authored-by: khoanguyentester-lgtm <nguyenanhkhoa301199@gmail.com>
Co-authored-by: kingpanther13 <25392815+kingpanther13@users.noreply.github.com>
1 parent ea16661 commit 1e08f73
2 files changed
Lines changed: 884 additions & 366 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
80 | 87 | | |
81 | 88 | | |
82 | 89 | | |
| |||
273 | 280 | | |
274 | 281 | | |
275 | 282 | | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
276 | 293 | | |
277 | 294 | | |
278 | 295 | | |
| |||
505 | 522 | | |
506 | 523 | | |
507 | 524 | | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
508 | 538 | | |
509 | 539 | | |
510 | 540 | | |
| |||
518 | 548 | | |
519 | 549 | | |
520 | 550 | | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
521 | 566 | | |
522 | 567 | | |
523 | 568 | | |
| |||
0 commit comments