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

This commit is contained in:
J. Nick Koston
2026-04-03 18:08:50 -10:00
parent 2337767c38
commit f6f07ef147
3 changed files with 12 additions and 2 deletions
+6 -1
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;
+2 -1
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_;
+4
View File
@@ -6,5 +6,9 @@ api:
time:
- platform: homeassistant
on_time:
- cron: "*/10 * * * * *"
then:
- logger.log: "CronTrigger fired (every 10 seconds)"
- platform: sntp
id: sntp_time