ESPHome  2024.12.2
automation.cpp
Go to the documentation of this file.
1 #include "automation.h"
2 
3 #include "esphome/core/log.h"
4 
5 #include <cinttypes>
6 
7 namespace esphome {
8 namespace time {
9 
10 static const char *const TAG = "automation";
11 static const int MAX_TIMESTAMP_DRIFT = 900; // how far can the clock drift before we consider
12  // there has been a drastic time synchronization
13 
14 void CronTrigger::add_second(uint8_t second) { this->seconds_[second] = true; }
15 void CronTrigger::add_minute(uint8_t minute) { this->minutes_[minute] = true; }
16 void CronTrigger::add_hour(uint8_t hour) { this->hours_[hour] = true; }
17 void CronTrigger::add_day_of_month(uint8_t day_of_month) { this->days_of_month_[day_of_month] = true; }
18 void CronTrigger::add_month(uint8_t month) { this->months_[month] = true; }
19 void CronTrigger::add_day_of_week(uint8_t day_of_week) { this->days_of_week_[day_of_week] = true; }
20 bool CronTrigger::matches(const ESPTime &time) {
21  return time.is_valid() && this->seconds_[time.second] && this->minutes_[time.minute] && this->hours_[time.hour] &&
22  this->days_of_month_[time.day_of_month] && this->months_[time.month] && this->days_of_week_[time.day_of_week];
23 }
25  ESPTime time = this->rtc_->now();
26  if (!time.is_valid())
27  return;
28 
29  if (this->last_check_.has_value()) {
30  if (*this->last_check_ > time && this->last_check_->timestamp - time.timestamp > MAX_TIMESTAMP_DRIFT) {
31  // We went back in time (a lot), probably caused by time synchronization
32  ESP_LOGW(TAG, "Time has jumped back!");
33  } else if (*this->last_check_ >= time) {
34  // already handled this one
35  return;
36  } else if (time > *this->last_check_ && time.timestamp - this->last_check_->timestamp > MAX_TIMESTAMP_DRIFT) {
37  // We went ahead in time (a lot), probably caused by time synchronization
38  ESP_LOGW(TAG, "Time has jumped ahead!");
39  this->last_check_ = time;
40  return;
41  }
42 
43  while (true) {
44  this->last_check_->increment_second();
45  if (*this->last_check_ >= time)
46  break;
47 
48  if (this->matches(*this->last_check_))
49  this->trigger();
50  }
51  }
52 
53  this->last_check_ = time;
54  if (!time.fields_in_range()) {
55  ESP_LOGW(TAG, "Time is out of range!");
56  ESP_LOGD(TAG, "Second=%02u Minute=%02u Hour=%02u DayOfWeek=%u DayOfMonth=%u DayOfYear=%u Month=%u time=%" PRId64,
57  time.second, time.minute, time.hour, time.day_of_week, time.day_of_month, time.day_of_year, time.month,
58  (int64_t) time.timestamp);
59  }
60 
61  if (this->matches(time))
62  this->trigger();
63 }
65 void CronTrigger::add_seconds(const std::vector<uint8_t> &seconds) {
66  for (uint8_t it : seconds)
67  this->add_second(it);
68 }
69 void CronTrigger::add_minutes(const std::vector<uint8_t> &minutes) {
70  for (uint8_t it : minutes)
71  this->add_minute(it);
72 }
73 void CronTrigger::add_hours(const std::vector<uint8_t> &hours) {
74  for (uint8_t it : hours)
75  this->add_hour(it);
76 }
77 void CronTrigger::add_days_of_month(const std::vector<uint8_t> &days_of_month) {
78  for (uint8_t it : days_of_month)
79  this->add_day_of_month(it);
80 }
81 void CronTrigger::add_months(const std::vector<uint8_t> &months) {
82  for (uint8_t it : months)
83  this->add_month(it);
84 }
85 void CronTrigger::add_days_of_week(const std::vector<uint8_t> &days_of_week) {
86  for (uint8_t it : days_of_week)
87  this->add_day_of_week(it);
88 }
90 
92  rtc->add_on_time_sync_callback([this]() { this->trigger(); });
93 }
94 
95 } // namespace time
96 } // namespace esphome
void add_days_of_week(const std::vector< uint8_t > &days_of_week)
Definition: automation.cpp:85
void add_hour(uint8_t hour)
Definition: automation.cpp:16
ESPTime now()
Get the time in the currently defined timezone.
bool matches(const ESPTime &time)
Definition: automation.cpp:20
SyncTrigger(RealTimeClock *rtc)
Definition: automation.cpp:91
The RealTimeClock class exposes common timekeeping functions via the device&#39;s local real-time clock...
A more user-friendly version of struct tm from time.h.
Definition: time.h:15
void add_minutes(const std::vector< uint8_t > &minutes)
Definition: automation.cpp:69
std::bitset< 61 > seconds_
Definition: automation.h:34
float get_setup_priority() const override
Definition: automation.cpp:89
void add_on_time_sync_callback(std::function< void()> callback)
void add_seconds(const std::vector< uint8_t > &seconds)
Definition: automation.cpp:65
uint16_t day_of_year
day of the year [1-366]
Definition: time.h:29
uint8_t minute
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
CronTrigger(RealTimeClock *rtc)
Definition: automation.cpp:64
std::bitset< 13 > months_
Definition: automation.h:38
uint8_t second
seconds after the minute [0-60]
Definition: time.h:19
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition: time.h:37
void add_month(uint8_t month)
Definition: automation.cpp:18
uint8_t hour
std::bitset< 60 > minutes_
Definition: automation.h:35
void add_day_of_month(uint8_t day_of_month)
Definition: automation.cpp:17
uint8_t second
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:21
RealTimeClock * rtc_
Definition: automation.h:40
void add_months(const std::vector< uint8_t > &months)
Definition: automation.cpp:81
void add_days_of_month(const std::vector< uint8_t > &days_of_month)
Definition: automation.cpp:77
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
Definition: time.h:59
std::bitset< 8 > days_of_week_
Definition: automation.h:39
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: time.h:25
std::bitset< 32 > days_of_month_
Definition: automation.h:37
optional< ESPTime > last_check_
Definition: automation.h:41
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
std::bitset< 24 > hours_
Definition: automation.h:36
void add_second(uint8_t second)
Definition: automation.cpp:14
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void add_day_of_week(uint8_t day_of_week)
Definition: automation.cpp:19
uint8_t month
month; january=1 [1-12]
Definition: time.h:31
uint8_t hour
hours since midnight [0-23]
Definition: time.h:23
void add_hours(const std::vector< uint8_t > &hours)
Definition: automation.cpp:73
void add_minute(uint8_t minute)
Definition: automation.cpp:15
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:27
uint8_t month
Definition: date_entity.h:123
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition: time.h:62