ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
template_time.cpp
Go to the documentation of this file.
1#include "template_time.h"
2
3#ifdef USE_DATETIME_TIME
4
5#include "esphome/core/log.h"
6
7namespace esphome::template_ {
8
9static const char *const TAG = "template.time";
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->hour_ = state.hour;
32 this->minute_ = state.minute;
33 this->second_ = state.second;
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->hour_ = val->hour;
44 this->minute_ = val->minute;
45 this->second_ = val->second;
46 this->publish_state();
47 }
48}
49
51 bool has_hour = call.get_hour().has_value();
52 bool has_minute = call.get_minute().has_value();
53 bool has_second = call.get_second().has_value();
54
55 ESPTime value = {};
56 if (has_hour)
57 value.hour = *call.get_hour();
58
59 if (has_minute)
60 value.minute = *call.get_minute();
61
62 if (has_second)
63 value.second = *call.get_second();
64
65 this->set_trigger_.trigger(value);
66
67 if (this->optimistic_) {
68 if (has_hour)
69 this->hour_ = *call.get_hour();
70 if (has_minute)
71 this->minute_ = *call.get_minute();
72 if (has_second)
73 this->second_ = *call.get_second();
74 this->publish_state();
75 }
76
77 if (this->restore_value_) {
79 if (has_hour) {
80 temp.hour = *call.get_hour();
81 } else {
82 temp.hour = this->hour_;
83 }
84 if (has_minute) {
85 temp.minute = *call.get_minute();
86 } else {
87 temp.minute = this->minute_;
88 }
89 if (has_second) {
90 temp.second = *call.get_second();
91 } else {
92 temp.second = this->second_;
93 }
94
95 this->pref_.save(&temp);
96 }
97}
98
100 LOG_DATETIME_TIME("", "Template Time", 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_TIME
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:325
optional< uint8_t > get_hour() const
Definition time_entity.h:87
optional< uint8_t > get_second() const
Definition time_entity.h:89
optional< uint8_t > get_minute() const
Definition time_entity.h:88
TemplateLambda< ESPTime > f_
void control(const datetime::TimeCall &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:21
uint8_t minute
minutes after the hour [0-59]
Definition time.h:30
uint8_t second
seconds after the minute [0-60]
Definition time.h:28
uint8_t hour
hours since midnight [0-23]
Definition time.h:32