ESPHome 2026.1.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::template_ {
8
9static const char *const TAG = "template.date";
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 {
21 this->pref_ =
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->publish_state();
36}
37
39 if (!this->f_.has_value())
40 return;
41
42 auto val = this->f_();
43 if (val.has_value()) {
44 this->year_ = val->year;
45 this->month_ = val->month;
46 this->day_ = val->day_of_month;
47 this->publish_state();
48 }
49}
50
52 bool has_year = call.get_year().has_value();
53 bool has_month = call.get_month().has_value();
54 bool has_day = call.get_day().has_value();
55
56 ESPTime value = {};
57 if (has_year)
58 value.year = *call.get_year();
59
60 if (has_month)
61 value.month = *call.get_month();
62
63 if (has_day)
64 value.day_of_month = *call.get_day();
65
66 this->set_trigger_->trigger(value);
67
68 if (this->optimistic_) {
69 if (has_year)
70 this->year_ = *call.get_year();
71 if (has_month)
72 this->month_ = *call.get_month();
73 if (has_day)
74 this->day_ = *call.get_day();
75 this->publish_state();
76 }
77
78 if (this->restore_value_) {
80 if (has_year) {
81 temp.year = *call.get_year();
82 } else {
83 temp.year = this->year_;
84 }
85 if (has_month) {
86 temp.month = *call.get_month();
87 } else {
88 temp.month = this->month_;
89 }
90 if (has_day) {
91 temp.day = *call.get_day();
92 } else {
93 temp.day = this->day_;
94 }
95
96 this->pref_.save(&temp);
97 }
98}
99
101 LOG_DATETIME_DATE("", "Template Date", this);
102 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
103 LOG_UPDATE_INTERVAL(this);
104}
105
106} // namespace esphome::template_
107
108#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:204
optional< uint8_t > get_month() const
Definition date_entity.h:86
optional< uint8_t > get_day() const
Definition date_entity.h:87
optional< uint16_t > get_year() const
Definition date_entity.h:85
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: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