ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
template_date.cpp
Go to the documentation of this file.
1#include "template_date.h"
2
3#ifdef USE_DATETIME_DATE
4
5#include "esphome/core/log.h"
6
7namespace esphome {
8namespace template_ {
9
10static const char *const TAG = "template.date";
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 {
22 this->pref_ =
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->publish_state();
37}
38
40 if (!this->f_.has_value())
41 return;
42
43 auto val = this->f_();
44 if (val.has_value()) {
45 this->year_ = val->year;
46 this->month_ = val->month;
47 this->day_ = val->day_of_month;
48 this->publish_state();
49 }
50}
51
53 bool has_year = call.get_year().has_value();
54 bool has_month = call.get_month().has_value();
55 bool has_day = call.get_day().has_value();
56
57 ESPTime value = {};
58 if (has_year)
59 value.year = *call.get_year();
60
61 if (has_month)
62 value.month = *call.get_month();
63
64 if (has_day)
65 value.day_of_month = *call.get_day();
66
67 this->set_trigger_->trigger(value);
68
69 if (this->optimistic_) {
70 if (has_year)
71 this->year_ = *call.get_year();
72 if (has_month)
73 this->month_ = *call.get_month();
74 if (has_day)
75 this->day_ = *call.get_day();
76 this->publish_state();
77 }
78
79 if (this->restore_value_) {
81 if (has_year) {
82 temp.year = *call.get_year();
83 } else {
84 temp.year = this->year_;
85 }
86 if (has_month) {
87 temp.month = *call.get_month();
88 } else {
89 temp.month = this->month_;
90 }
91 if (has_day) {
92 temp.day = *call.get_day();
93 } else {
94 temp.day = this->day_;
95 }
96
97 this->pref_.save(&temp);
98 }
99}
100
102 LOG_DATETIME_DATE("", "Template Date", this);
103 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
104 LOG_UPDATE_INTERVAL(this);
105}
106
107} // namespace template_
108} // namespace esphome
109
110#endif // USE_DATETIME_DATE
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< uint8_t > get_month() const
Definition date_entity.h:87
optional< uint8_t > get_day() const
Definition date_entity.h:88
optional< uint16_t > get_year() const
Definition date_entity.h:86
Trigger< ESPTime > * set_trigger_
TemplateLambda< ESPTime > f_
void control(const datetime::DateCall &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 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