ESPHome 2025.12.3
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 {
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->hour_ = state.hour;
33 this->minute_ = state.minute;
34 this->second_ = state.second;
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->hour_ = val->hour;
45 this->minute_ = val->minute;
46 this->second_ = val->second;
47 this->publish_state();
48 }
49}
50
52 bool has_hour = call.get_hour().has_value();
53 bool has_minute = call.get_minute().has_value();
54 bool has_second = call.get_second().has_value();
55
56 ESPTime value = {};
57 if (has_hour)
58 value.hour = *call.get_hour();
59
60 if (has_minute)
61 value.minute = *call.get_minute();
62
63 if (has_second)
64 value.second = *call.get_second();
65
66 this->set_trigger_->trigger(value);
67
68 if (this->optimistic_) {
69 if (has_hour)
70 this->hour_ = *call.get_hour();
71 if (has_minute)
72 this->minute_ = *call.get_minute();
73 if (has_second)
74 this->second_ = *call.get_second();
75 this->publish_state();
76 }
77
78 if (this->restore_value_) {
80 if (has_hour) {
81 temp.hour = *call.get_hour();
82 } else {
83 temp.hour = this->hour_;
84 }
85 if (has_minute) {
86 temp.minute = *call.get_minute();
87 } else {
88 temp.minute = this->minute_;
89 }
90 if (has_second) {
91 temp.second = *call.get_second();
92 } else {
93 temp.second = this->second_;
94 }
95
96 this->pref_.save(&temp);
97 }
98}
99
101 LOG_DATETIME_TIME("", "Template Time", 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_TIME
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_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
Trigger< ESPTime > * set_trigger_
bool state
Definition fan.h:0
mopeka_std_values val[4]
const char *const TAG
Definition spi.cpp:8
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