Add startup_delay to interval. (#5327)

This commit is contained in:
Clyde Stubbs 2023-11-23 18:31:23 +11:00 committed by GitHub
parent 8738cef5a3
commit 3ac59180ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View File

@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.const import CONF_ID, CONF_INTERVAL
from esphome.const import CONF_ID, CONF_INTERVAL, CONF_STARTUP_DELAY
CODEOWNERS = ["@esphome/core"]
interval_ns = cg.esphome_ns.namespace("interval")
@ -13,6 +13,9 @@ CONFIG_SCHEMA = automation.validate_automation(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(IntervalTrigger),
cv.Optional(
CONF_STARTUP_DELAY, default="0s"
): cv.positive_time_period_milliseconds,
cv.Required(CONF_INTERVAL): cv.positive_time_period_milliseconds,
}
).extend(cv.COMPONENT_SCHEMA)
@ -26,3 +29,4 @@ async def to_code(config):
await automation.build_automation(var, [], conf)
cg.add(var.set_update_interval(conf[CONF_INTERVAL]))
cg.add(var.set_startup_delay(conf[CONF_STARTUP_DELAY]))

View File

@ -8,8 +8,26 @@ namespace interval {
class IntervalTrigger : public Trigger<>, public PollingComponent {
public:
void update() override { this->trigger(); }
void update() override {
if (this->started_)
this->trigger();
}
void setup() override {
if (this->startup_delay_ == 0) {
this->started_ = true;
} else {
this->set_timeout(this->startup_delay_, [this] { this->started_ = true; });
}
}
void set_startup_delay(const uint32_t startup_delay) { this->startup_delay_ = startup_delay; }
float get_setup_priority() const override { return setup_priority::DATA; }
protected:
uint32_t startup_delay_{0};
bool started_{false};
};
} // namespace interval

View File

@ -718,6 +718,7 @@ stepper:
interval:
interval: 5s
startup_delay: 10s
then:
- logger.log: Interval Run