[time] Use set_interval for CronTrigger instead of loop() (#15433)

This commit is contained in:
J. Nick Koston
2026-04-04 01:07:16 -10:00
committed by GitHub
parent f51871fa6b
commit 297f9c134f
3 changed files with 12 additions and 2 deletions

View File

@@ -20,7 +20,12 @@ bool CronTrigger::matches(const ESPTime &time) {
return time.is_valid() && this->seconds_[time.second] && this->minutes_[time.minute] && this->hours_[time.hour] &&
this->days_of_month_[time.day_of_month] && this->months_[time.month] && this->days_of_week_[time.day_of_week];
}
void CronTrigger::loop() {
void CronTrigger::setup() {
// Cron resolution is 1 second — check once per second instead of every loop iteration
this->set_interval(1000, [this]() { this->check_time_(); });
}
void CronTrigger::check_time_() {
ESPTime time = this->rtc_->now();
if (!time.is_valid())
return;

View File

@@ -26,10 +26,11 @@ class CronTrigger : public Trigger<>, public Component {
void add_day_of_week(uint8_t day_of_week);
void add_days_of_week(const std::vector<uint8_t> &days_of_week);
bool matches(const ESPTime &time);
void loop() override;
void setup() override;
float get_setup_priority() const override;
protected:
void check_time_();
std::bitset<61> seconds_;
std::bitset<60> minutes_;
std::bitset<24> hours_;

View File

@@ -6,5 +6,9 @@ api:
time:
- platform: homeassistant
on_time:
- seconds: "0,10,20,30,40,50"
then:
- logger.log: "CronTrigger fired (every 10 seconds)"
- platform: sntp
id: sntp_time