Add a cooldown condition#1387
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis PR adds a new "cooldown" condition to the Spook integration. It defines a YAML config schema and duration selector, implements ChangesCooldown Condition
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant Automation
participant SpookCondition
participant LastTriggeredHelper
Automation->>SpookCondition: async_validate_config(config)
SpookCondition-->>Automation: validated config with duration
Automation->>SpookCondition: _async_check(variables)
SpookCondition->>LastTriggeredHelper: _last_triggered(variables)
LastTriggeredHelper-->>SpookCondition: datetime or None
alt no last_triggered
SpookCondition-->>Automation: True (pass)
else last_triggered present
SpookCondition->>SpookCondition: compare utcnow() - last_triggered vs duration
SpookCondition-->>Automation: True/False
end
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
There was a problem hiding this comment.
Pull request overview
This PR adds a second Spook condition, spook.cooldown, on top of the condition platform introduced in #1386. It passes only when the running automation/script has not triggered within a configured duration (or has never run), replacing the commonly copy-pasted now() - this.attributes.last_triggered > timedelta(...) template. It is implemented purely as a leaf module under ectoplasms/spook/conditions/, so no platform plumbing (condition.py) was touched.
Changes:
- New
SpookCondition(cooldown) that readsthis.attributes.last_triggered, handlingdatetime, restored ISO-string, and no-context cases. - Registers the condition's
durationfield selector inconditions.yamland adds its translation entry inen.json. - Adds full test coverage for discovery, config validation, and each cooldown branch.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| custom_components/spook/ectoplasms/spook/conditions/cooldown.py | New leaf-module condition implementing the per-run cooldown logic and config schema |
| custom_components/spook/conditions.yaml | Adds the cooldown field metadata with a duration selector for the automation editor |
| custom_components/spook/translations/en.json | Adds cooldown name/description and duration field strings (alphabetically ordered) |
| tests/ectoplasms/spook/conditions/test_cooldown.py | Tests discovery, validation, and every cooldown evaluation branch |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.



Description
Adds a second Spook condition:
spook.cooldown.It passes only if this automation or script has not run within a configured duration (or has never run). It is a built-in cooldown, so an automation does not re-fire too often. This replaces the copy-pasted
{{ now() - this.attributes.last_triggered > timedelta(...) }}template that shows up everywhere.It reads the
thisvariable (the running automation or script's own state), which Home Assistant captures before the current run updateslast_triggered, so the comparison is against the previous run and the cooldown actually holds.last_triggeredis handled whether it arrives as a datetime or as a restored ISO string, and the condition passes gracefully when there is no run context.This is the first condition added as a pure leaf module on top of the condition platform from #1386: a single file under
ectoplasms/spook/conditions/plus itsconditions.yamland translation entry.condition.pywas not touched.Motivation and Context
A per-automation cooldown is one of the most copy-pasted template conditions in Home Assistant. Making it a first-class condition removes that boilerplate.
How has this been tested?
spook.cooldown.Types of changes
Checklist