ESPHome  2024.11.0
template_datetime.cpp
Go to the documentation of this file.
1 #include "template_datetime.h"
2 
3 #ifdef USE_DATETIME_DATETIME
4 
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace template_ {
9 
10 static const char *const TAG = "template.datetime";
11 
13  if (this->f_.has_value())
14  return;
15 
16  ESPTime state{};
17 
18  if (!this->restore_value_) {
19  state = this->initial_value_;
20  } else {
23  this->get_object_id_hash());
24  if (this->pref_.load(&temp)) {
25  temp.apply(this);
26  return;
27  } else {
28  // set to inital value if loading from pref failed
29  state = this->initial_value_;
30  }
31  }
32 
33  this->year_ = state.year;
34  this->month_ = state.month;
35  this->day_ = state.day_of_month;
36  this->hour_ = state.hour;
37  this->minute_ = state.minute;
38  this->second_ = state.second;
39  this->publish_state();
40 }
41 
43  if (!this->f_.has_value())
44  return;
45 
46  auto val = (*this->f_)();
47  if (!val.has_value())
48  return;
49 
50  this->year_ = val->year;
51  this->month_ = val->month;
52  this->day_ = val->day_of_month;
53  this->hour_ = val->hour;
54  this->minute_ = val->minute;
55  this->second_ = val->second;
56  this->publish_state();
57 }
58 
60  bool has_year = call.get_year().has_value();
61  bool has_month = call.get_month().has_value();
62  bool has_day = call.get_day().has_value();
63  bool has_hour = call.get_hour().has_value();
64  bool has_minute = call.get_minute().has_value();
65  bool has_second = call.get_second().has_value();
66 
67  ESPTime value = {};
68  if (has_year)
69  value.year = *call.get_year();
70 
71  if (has_month)
72  value.month = *call.get_month();
73 
74  if (has_day)
75  value.day_of_month = *call.get_day();
76 
77  if (has_hour)
78  value.hour = *call.get_hour();
79 
80  if (has_minute)
81  value.minute = *call.get_minute();
82 
83  if (has_second)
84  value.second = *call.get_second();
85 
86  this->set_trigger_->trigger(value);
87 
88  if (this->optimistic_) {
89  if (has_year)
90  this->year_ = *call.get_year();
91  if (has_month)
92  this->month_ = *call.get_month();
93  if (has_day)
94  this->day_ = *call.get_day();
95  if (has_hour)
96  this->hour_ = *call.get_hour();
97  if (has_minute)
98  this->minute_ = *call.get_minute();
99  if (has_second)
100  this->second_ = *call.get_second();
101  this->publish_state();
102  }
103 
104  if (this->restore_value_) {
106  if (has_year) {
107  temp.year = *call.get_year();
108  } else {
109  temp.year = this->year_;
110  }
111  if (has_month) {
112  temp.month = *call.get_month();
113  } else {
114  temp.month = this->month_;
115  }
116  if (has_day) {
117  temp.day = *call.get_day();
118  } else {
119  temp.day = this->day_;
120  }
121  if (has_hour) {
122  temp.hour = *call.get_hour();
123  } else {
124  temp.hour = this->hour_;
125  }
126  if (has_minute) {
127  temp.minute = *call.get_minute();
128  } else {
129  temp.minute = this->minute_;
130  }
131  if (has_second) {
132  temp.second = *call.get_second();
133  } else {
134  temp.second = this->second_;
135  }
136 
137  this->pref_.save(&temp);
138  }
139 }
140 
142  LOG_DATETIME_DATETIME("", "Template DateTime", this);
143  ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
144  LOG_UPDATE_INTERVAL(this);
145 }
146 
147 } // namespace template_
148 } // namespace esphome
149 
150 #endif // USE_DATETIME_DATETIME
optional< uint16_t > get_year() const
optional< uint8_t > get_day() const
optional< uint8_t > get_second() const
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
mopeka_std_values val[4]
bool has_value() const
Definition: optional.h:87
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
bool save(const T *src)
Definition: preferences.h:21
void control(const datetime::DateTimeCall &call) override
optional< uint8_t > get_month() const
optional< uint8_t > get_hour() const
const char *const TAG
Definition: spi.cpp:8
uint8_t second
seconds after the minute [0-60]
Definition: time.h:21
ESPPreferences * global_preferences
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:23
uint16_t year
year
Definition: time.h:35
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t month
month; january=1 [1-12]
Definition: time.h:33
optional< std::function< optional< ESPTime >)> > f_
uint8_t hour
hours since midnight [0-23]
Definition: time.h:25
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:29
optional< uint8_t > get_minute() const
bool state
Definition: fan.h:34