ESPHome 2026.1.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 194434090U ^ this->get_preference_hash());
23 if (this->pref_.load(&temp)) {
24 temp.apply(this);
25 return;
26 } else {
27 // set to inital value if loading from pref failed
28 state = this->initial_value_;
29 }
30 }
31
32 this->year_ = state.year;
33 this->month_ = state.month;
34 this->day_ = state.day_of_month;
35 this->hour_ = state.hour;
36 this->minute_ = state.minute;
37 this->second_ = state.second;
38 this->publish_state();
39}
40
42 if (!this->f_.has_value())
43 return;
44
45 auto val = this->f_();
46 if (val.has_value()) {
47 this->year_ = val->year;
48 this->month_ = val->month;
49 this->day_ = val->day_of_month;
50 this->hour_ = val->hour;
51 this->minute_ = val->minute;
52 this->second_ = val->second;
53 this->publish_state();
54 }
55}
56
58 bool has_year = call.get_year().has_value();
59 bool has_month = call.get_month().has_value();
60 bool has_day = call.get_day().has_value();
61 bool has_hour = call.get_hour().has_value();
62 bool has_minute = call.get_minute().has_value();
63 bool has_second = call.get_second().has_value();
64
65 ESPTime value = {};
66 if (has_year)
67 value.year = *call.get_year();
68
69 if (has_month)
70 value.month = *call.get_month();
71
72 if (has_day)
73 value.day_of_month = *call.get_day();
74
75 if (has_hour)
76 value.hour = *call.get_hour();
77
78 if (has_minute)
79 value.minute = *call.get_minute();
80
81 if (has_second)
82 value.second = *call.get_second();
83
84 this->set_trigger_->trigger(value);
85
86 if (this->optimistic_) {
87 if (has_year)
88 this->year_ = *call.get_year();
89 if (has_month)
90 this->month_ = *call.get_month();
91 if (has_day)
92 this->day_ = *call.get_day();
93 if (has_hour)
94 this->hour_ = *call.get_hour();
95 if (has_minute)
96 this->minute_ = *call.get_minute();
97 if (has_second)
98 this->second_ = *call.get_second();
99 this->publish_state();
100 }
101
102 if (this->restore_value_) {
104 if (has_year) {
105 temp.year = *call.get_year();
106 } else {
107 temp.year = this->year_;
108 }
109 if (has_month) {
110 temp.month = *call.get_month();
111 } else {
112 temp.month = this->month_;
113 }
114 if (has_day) {
115 temp.day = *call.get_day();
116 } else {
117 temp.day = this->day_;
118 }
119 if (has_hour) {
120 temp.hour = *call.get_hour();
121 } else {
122 temp.hour = this->hour_;
123 }
124 if (has_minute) {
125 temp.minute = *call.get_minute();
126 } else {
127 temp.minute = this->minute_;
128 }
129 if (has_second) {
130 temp.second = *call.get_second();
131 } else {
132 temp.second = this->second_;
133 }
134
135 this->pref_.save(&temp);
136 }
137}
138
140 LOG_DATETIME_DATETIME("", "Template DateTime", this);
141 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
142 LOG_UPDATE_INTERVAL(this);
143}
144
145} // namespace esphome::template_
146
147#endif // USE_DATETIME_DATETIME
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:204
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:0
mopeka_std_values val[4]
const char *const TAG
Definition spi.cpp:7
ESPPreferences * global_preferences
A more user-friendly version of struct tm from time.h.
Definition time.h:15
uint8_t minute
minutes after the hour [0-59]
Definition time.h:21
uint8_t second
seconds after the minute [0-60]
Definition time.h:19
uint8_t hour
hours since midnight [0-23]
Definition time.h:23
uint8_t day_of_month
day of the month [1-31]
Definition time.h:27
uint16_t year
year
Definition time.h:33
uint8_t month
month; january=1 [1-12]
Definition time.h:31