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