ESPHome  2024.11.0
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 
7 namespace esphome {
8 namespace template_ {
9 
10 static 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  return;
46 
47  this->hour_ = val->hour;
48  this->minute_ = val->minute;
49  this->second_ = val->second;
50  this->publish_state();
51 }
52 
54  bool has_hour = call.get_hour().has_value();
55  bool has_minute = call.get_minute().has_value();
56  bool has_second = call.get_second().has_value();
57 
58  ESPTime value = {};
59  if (has_hour)
60  value.hour = *call.get_hour();
61 
62  if (has_minute)
63  value.minute = *call.get_minute();
64 
65  if (has_second)
66  value.second = *call.get_second();
67 
68  this->set_trigger_->trigger(value);
69 
70  if (this->optimistic_) {
71  if (has_hour)
72  this->hour_ = *call.get_hour();
73  if (has_minute)
74  this->minute_ = *call.get_minute();
75  if (has_second)
76  this->second_ = *call.get_second();
77  this->publish_state();
78  }
79 
80  if (this->restore_value_) {
82  if (has_hour) {
83  temp.hour = *call.get_hour();
84  } else {
85  temp.hour = this->hour_;
86  }
87  if (has_minute) {
88  temp.minute = *call.get_minute();
89  } else {
90  temp.minute = this->minute_;
91  }
92  if (has_second) {
93  temp.second = *call.get_second();
94  } else {
95  temp.second = this->second_;
96  }
97 
98  this->pref_.save(&temp);
99  }
100 }
101 
103  LOG_DATETIME_TIME("", "Template Time", this);
104  ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
105  LOG_UPDATE_INTERVAL(this);
106 }
107 
108 } // namespace template_
109 } // namespace esphome
110 
111 #endif // USE_DATETIME_TIME
optional< uint8_t > get_hour() const
Definition: time_entity.h:88
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
optional< std::function< optional< ESPTime >)> > f_
Definition: template_time.h:38
mopeka_std_values val[4]
bool has_value() const
Definition: optional.h:87
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
bool save(const T *src)
Definition: preferences.h:21
const char *const TAG
Definition: spi.cpp:8
uint8_t second
seconds after the minute [0-60]
Definition: time.h:21
ESPPreferences * global_preferences
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:23
optional< uint8_t > get_minute() const
Definition: time_entity.h:89
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
optional< uint8_t > get_second() const
Definition: time_entity.h:90
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t hour
hours since midnight [0-23]
Definition: time.h:25
Trigger< ESPTime > * set_trigger_
Definition: template_time.h:37
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
void control(const datetime::TimeCall &call) override
bool state
Definition: fan.h:34