ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
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
7namespace esphome::template_ {
8
9static const char *const TAG = "template.datetime";
10
12 if (this->f_.has_value())
13 return;
14
15 ESPTime state{};
16
17 if (!this->restore_value_) {
18 state = this->initial_value_;
19 } else {
22 if (this->pref_.load(&temp)) {
23 temp.apply(this);
24 return;
25 } else {
26 // set to inital value if loading from pref failed
27 state = this->initial_value_;
28 }
29 }
30
31 this->year_ = state.year;
32 this->month_ = state.month;
33 this->day_ = state.day_of_month;
34 this->hour_ = state.hour;
35 this->minute_ = state.minute;
36 this->second_ = state.second;
37 this->publish_state();
38}
39
41 if (!this->f_.has_value())
42 return;
43
44 auto val = this->f_();
45 if (val.has_value()) {
46 this->year_ = val->year;
47 this->month_ = val->month;
48 this->day_ = val->day_of_month;
49 this->hour_ = val->hour;
50 this->minute_ = val->minute;
51 this->second_ = val->second;
52 this->publish_state();
53 }
54}
55
57 auto opt_year = call.get_year();
58 auto opt_month = call.get_month();
59 auto opt_day = call.get_day();
60 auto opt_hour = call.get_hour();
61 auto opt_minute = call.get_minute();
62 auto opt_second = call.get_second();
63 bool has_year = opt_year.has_value();
64 bool has_month = opt_month.has_value();
65 bool has_day = opt_day.has_value();
66 bool has_hour = opt_hour.has_value();
67 bool has_minute = opt_minute.has_value();
68 bool has_second = opt_second.has_value();
69
70 ESPTime value = {};
71 if (has_year)
72 value.year = *opt_year;
73
74 if (has_month)
75 value.month = *opt_month;
76
77 if (has_day)
78 value.day_of_month = *opt_day;
79
80 if (has_hour)
81 value.hour = *opt_hour;
82
83 if (has_minute)
84 value.minute = *opt_minute;
85
86 if (has_second)
87 value.second = *opt_second;
88
89 this->set_trigger_.trigger(value);
90
91 if (this->optimistic_) {
92 if (has_year)
93 this->year_ = *opt_year;
94 if (has_month)
95 this->month_ = *opt_month;
96 if (has_day)
97 this->day_ = *opt_day;
98 if (has_hour)
99 this->hour_ = *opt_hour;
100 if (has_minute)
101 this->minute_ = *opt_minute;
102 if (has_second)
103 this->second_ = *opt_second;
104 this->publish_state();
105 }
106
107 if (this->restore_value_) {
109 if (has_year) {
110 temp.year = *opt_year;
111 } else {
112 temp.year = this->year_;
113 }
114 if (has_month) {
115 temp.month = *opt_month;
116 } else {
117 temp.month = this->month_;
118 }
119 if (has_day) {
120 temp.day = *opt_day;
121 } else {
122 temp.day = this->day_;
123 }
124 if (has_hour) {
125 temp.hour = *opt_hour;
126 } else {
127 temp.hour = this->hour_;
128 }
129 if (has_minute) {
130 temp.minute = *opt_minute;
131 } else {
132 temp.minute = this->minute_;
133 }
134 if (has_second) {
135 temp.second = *opt_second;
136 } else {
137 temp.second = this->second_;
138 }
139
140 this->pref_.save(&temp);
141 }
142}
143
145 LOG_DATETIME_DATETIME("", "Template DateTime", this);
146 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
147 LOG_UPDATE_INTERVAL(this);
148}
149
150} // namespace esphome::template_
151
152#endif // USE_DATETIME_DATETIME
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:482
void control(const datetime::DateTimeCall &call) override
bool state
Definition fan.h:2
mopeka_std_values val[3]
const char *const TAG
Definition spi.cpp:7
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42