mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 05:24:53 +01:00
Add startup_delay to interval. (#5327)
This commit is contained in:
parent
8738cef5a3
commit
3ac59180ab
3 changed files with 25 additions and 2 deletions
|
@ -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]))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -718,6 +718,7 @@ stepper:
|
|||
|
||||
interval:
|
||||
interval: 5s
|
||||
startup_delay: 10s
|
||||
then:
|
||||
- logger.log: Interval Run
|
||||
|
||||
|
|
Loading…
Reference in a new issue