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