ESPHome 2025.12.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 {
8namespace template_ {
9
10static 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 194434090U ^ this->get_preference_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 this->year_ = val->year;
49 this->month_ = val->month;
50 this->day_ = val->day_of_month;
51 this->hour_ = val->hour;
52 this->minute_ = val->minute;
53 this->second_ = val->second;
54 this->publish_state();
55 }
56}
57
59 bool has_year = call.get_year().has_value();
60 bool has_month = call.get_month().has_value();
61 bool has_day = call.get_day().has_value();
62 bool has_hour = call.get_hour().has_value();
63 bool has_minute = call.get_minute().has_value();
64 bool has_second = call.get_second().has_value();
65
66 ESPTime value = {};
67 if (has_year)
68 value.year = *call.get_year();
69
70 if (has_month)
71 value.month = *call.get_month();
72
73 if (has_day)
74 value.day_of_month = *call.get_day();
75
76 if (has_hour)
77 value.hour = *call.get_hour();
78
79 if (has_minute)
80 value.minute = *call.get_minute();
81
82 if (has_second)
83 value.second = *call.get_second();
84
85 this->set_trigger_->trigger(value);
86
87 if (this->optimistic_) {
88 if (has_year)
89 this->year_ = *call.get_year();
90 if (has_month)
91 this->month_ = *call.get_month();
92 if (has_day)
93 this->day_ = *call.get_day();
94 if (has_hour)
95 this->hour_ = *call.get_hour();
96 if (has_minute)
97 this->minute_ = *call.get_minute();
98 if (has_second)
99 this->second_ = *call.get_second();
100 this->publish_state();
101 }
102
103 if (this->restore_value_) {
105 if (has_year) {
106 temp.year = *call.get_year();
107 } else {
108 temp.year = this->year_;
109 }
110 if (has_month) {
111 temp.month = *call.get_month();
112 } else {
113 temp.month = this->month_;
114 }
115 if (has_day) {
116 temp.day = *call.get_day();
117 } else {
118 temp.day = this->day_;
119 }
120 if (has_hour) {
121 temp.hour = *call.get_hour();
122 } else {
123 temp.hour = this->hour_;
124 }
125 if (has_minute) {
126 temp.minute = *call.get_minute();
127 } else {
128 temp.minute = this->minute_;
129 }
130 if (has_second) {
131 temp.second = *call.get_second();
132 } else {
133 temp.second = this->second_;
134 }
135
136 this->pref_.save(&temp);
137 }
138}
139
141 LOG_DATETIME_DATETIME("", "Template DateTime", this);
142 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
143 LOG_UPDATE_INTERVAL(this);
144}
145
146} // namespace template_
147} // namespace esphome
148
149#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:169
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:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.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