ESPHome  2024.5.2
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 
7 namespace esphome {
8 namespace template_ {
9 
10 static const char *const TAG = "template.date";
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->year_ = state.year;
34  this->month_ = state.month;
35  this->day_ = state.day_of_month;
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->year_ = val->year;
48  this->month_ = val->month;
49  this->day_ = val->day_of_month;
50  this->publish_state();
51 }
52 
54  bool has_year = call.get_year().has_value();
55  bool has_month = call.get_month().has_value();
56  bool has_day = call.get_day().has_value();
57 
58  ESPTime value = {};
59  if (has_year)
60  value.year = *call.get_year();
61 
62  if (has_month)
63  value.month = *call.get_month();
64 
65  if (has_day)
66  value.day_of_month = *call.get_day();
67 
68  this->set_trigger_->trigger(value);
69 
70  if (this->optimistic_) {
71  if (has_year)
72  this->year_ = *call.get_year();
73  if (has_month)
74  this->month_ = *call.get_month();
75  if (has_day)
76  this->day_ = *call.get_day();
77  this->publish_state();
78  }
79 
80  if (this->restore_value_) {
82  if (has_year) {
83  temp.year = *call.get_year();
84  } else {
85  temp.year = this->year_;
86  }
87  if (has_month) {
88  temp.month = *call.get_month();
89  } else {
90  temp.month = this->month_;
91  }
92  if (has_day) {
93  temp.day = *call.get_day();
94  } else {
95  temp.day = this->day_;
96  }
97 
98  this->pref_.save(&temp);
99  }
100 }
101 
103  LOG_DATETIME_DATE("", "Template Date", 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_DATE
Trigger< ESPTime > * set_trigger_
Definition: template_date.h:37
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
mopeka_std_values val[4]
bool has_value() const
Definition: optional.h:87
void control(const datetime::DateCall &call) override
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
optional< uint8_t > get_day() const
Definition: date_entity.h:88
ESPPreferences * global_preferences
uint16_t year
year
Definition: time.h:35
optional< uint16_t > get_year() const
Definition: date_entity.h:86
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
uint8_t month
month; january=1 [1-12]
Definition: time.h:33
optional< uint8_t > get_month() const
Definition: date_entity.h:87
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:29
bool state
Definition: fan.h:34
optional< std::function< optional< ESPTime >)> > f_
Definition: template_date.h:38