ESPHome 2026.2.4
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 bool has_year = call.get_year().has_value();
58 bool has_month = call.get_month().has_value();
59 bool has_day = call.get_day().has_value();
60 bool has_hour = call.get_hour().has_value();
61 bool has_minute = call.get_minute().has_value();
62 bool has_second = call.get_second().has_value();
63
64 ESPTime value = {};
65 if (has_year)
66 value.year = *call.get_year();
67
68 if (has_month)
69 value.month = *call.get_month();
70
71 if (has_day)
72 value.day_of_month = *call.get_day();
73
74 if (has_hour)
75 value.hour = *call.get_hour();
76
77 if (has_minute)
78 value.minute = *call.get_minute();
79
80 if (has_second)
81 value.second = *call.get_second();
82
83 this->set_trigger_.trigger(value);
84
85 if (this->optimistic_) {
86 if (has_year)
87 this->year_ = *call.get_year();
88 if (has_month)
89 this->month_ = *call.get_month();
90 if (has_day)
91 this->day_ = *call.get_day();
92 if (has_hour)
93 this->hour_ = *call.get_hour();
94 if (has_minute)
95 this->minute_ = *call.get_minute();
96 if (has_second)
97 this->second_ = *call.get_second();
98 this->publish_state();
99 }
100
101 if (this->restore_value_) {
103 if (has_year) {
104 temp.year = *call.get_year();
105 } else {
106 temp.year = this->year_;
107 }
108 if (has_month) {
109 temp.month = *call.get_month();
110 } else {
111 temp.month = this->month_;
112 }
113 if (has_day) {
114 temp.day = *call.get_day();
115 } else {
116 temp.day = this->day_;
117 }
118 if (has_hour) {
119 temp.hour = *call.get_hour();
120 } else {
121 temp.hour = this->hour_;
122 }
123 if (has_minute) {
124 temp.minute = *call.get_minute();
125 } else {
126 temp.minute = this->minute_;
127 }
128 if (has_second) {
129 temp.second = *call.get_second();
130 } else {
131 temp.second = this->second_;
132 }
133
134 this->pref_.save(&temp);
135 }
136}
137
139 LOG_DATETIME_DATETIME("", "Template DateTime", this);
140 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
141 LOG_UPDATE_INTERVAL(this);
142}
143
144} // namespace esphome::template_
145
146#endif // USE_DATETIME_DATETIME
bool save(const T *src)
Definition preferences.h:21
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:279
optional< uint16_t > get_year() const
optional< uint8_t > get_hour() const
optional< uint8_t > get_month() const
optional< uint8_t > get_minute() const
optional< uint8_t > get_day() const
optional< uint8_t > get_second() const
void control(const datetime::DateTimeCall &call) override
bool state
Definition fan.h:2
mopeka_std_values val[4]
const char *const TAG
Definition spi.cpp:7
A more user-friendly version of struct tm from time.h.
Definition time.h:17
uint8_t minute
minutes after the hour [0-59]
Definition time.h:26
uint8_t second
seconds after the minute [0-60]
Definition time.h:24
uint8_t hour
hours since midnight [0-23]
Definition time.h:28
uint8_t day_of_month
day of the month [1-31]
Definition time.h:32
uint16_t year
year
Definition time.h:38
uint8_t month
month; january=1 [1-12]
Definition time.h:36