mirror of
https://github.com/esphome/esphome.git
synced 2026-05-21 10:51:34 +08:00
[time] Use set_interval for CronTrigger instead of loop()
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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_;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user