ESPHome 2025.12.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 {
8namespace template_ {
9
10static const char *const TAG = "template.time";
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->hour_ = state.hour;
34 this->minute_ = state.minute;
35 this->second_ = state.second;
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->hour_ = val->hour;
46 this->minute_ = val->minute;
47 this->second_ = val->second;
48 this->publish_state();
49 }
50}
51
53 bool has_hour = call.get_hour().has_value();
54 bool has_minute = call.get_minute().has_value();
55 bool has_second = call.get_second().has_value();
56
57 ESPTime value = {};
58 if (has_hour)
59 value.hour = *call.get_hour();
60
61 if (has_minute)
62 value.minute = *call.get_minute();
63
64 if (has_second)
65 value.second = *call.get_second();
66
67 this->set_trigger_->trigger(value);
68
69 if (this->optimistic_) {
70 if (has_hour)
71 this->hour_ = *call.get_hour();
72 if (has_minute)
73 this->minute_ = *call.get_minute();
74 if (has_second)
75 this->second_ = *call.get_second();
76 this->publish_state();
77 }
78
79 if (this->restore_value_) {
81 if (has_hour) {
82 temp.hour = *call.get_hour();
83 } else {
84 temp.hour = this->hour_;
85 }
86 if (has_minute) {
87 temp.minute = *call.get_minute();
88 } else {
89 temp.minute = this->minute_;
90 }
91 if (has_second) {
92 temp.second = *call.get_second();
93 } else {
94 temp.second = this->second_;
95 }
96
97 this->pref_.save(&temp);
98 }
99}
100
102 LOG_DATETIME_TIME("", "Template Time", 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_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:169
optional< uint8_t > get_hour() const
Definition time_entity.h:88
optional< uint8_t > get_second() const
Definition time_entity.h:90
optional< uint8_t > get_minute() const
Definition time_entity.h:89
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
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 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