ESPHome  2024.4.2
date_entity.cpp
Go to the documentation of this file.
1 #include "date_entity.h"
2 
3 #ifdef USE_DATETIME_DATE
4 
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace datetime {
9 
10 static const char *const TAG = "datetime.date_entity";
11 
13  if (this->year_ == 0 || this->month_ == 0 || this->day_ == 0) {
14  this->has_state_ = false;
15  return;
16  }
17  if (this->year_ < 1970 || this->year_ > 3000) {
18  this->has_state_ = false;
19  ESP_LOGE(TAG, "Year must be between 1970 and 3000");
20  return;
21  }
22  if (this->month_ < 1 || this->month_ > 12) {
23  this->has_state_ = false;
24  ESP_LOGE(TAG, "Month must be between 1 and 12");
25  return;
26  }
27  if (this->day_ > days_in_month(this->month_, this->year_)) {
28  this->has_state_ = false;
29  ESP_LOGE(TAG, "Day must be between 1 and %d for month %d", days_in_month(this->month_, this->year_), this->month_);
30  return;
31  }
32  this->has_state_ = true;
33  ESP_LOGD(TAG, "'%s': Sending date %d-%d-%d", this->get_name().c_str(), this->year_, this->month_, this->day_);
34  this->state_callback_.call();
35 }
36 
38 
40  if (this->year_.has_value() && (this->year_ < 1970 || this->year_ > 3000)) {
41  ESP_LOGE(TAG, "Year must be between 1970 and 3000");
42  this->year_.reset();
43  }
44  if (this->month_.has_value() && (this->month_ < 1 || this->month_ > 12)) {
45  ESP_LOGE(TAG, "Month must be between 1 and 12");
46  this->month_.reset();
47  }
48  if (this->day_.has_value()) {
49  uint16_t year = 0;
50  uint8_t month = 0;
51  if (this->month_.has_value()) {
52  month = *this->month_;
53  } else {
54  if (this->parent_->month != 0) {
55  month = this->parent_->month;
56  } else {
57  ESP_LOGE(TAG, "Month must be set to validate day");
58  this->day_.reset();
59  }
60  }
61  if (this->year_.has_value()) {
62  year = *this->year_;
63  } else {
64  if (this->parent_->year != 0) {
65  year = this->parent_->year;
66  } else {
67  ESP_LOGE(TAG, "Year must be set to validate day");
68  this->day_.reset();
69  }
70  }
71  if (this->day_.has_value() && *this->day_ > days_in_month(month, year)) {
72  ESP_LOGE(TAG, "Day must be between 1 and %d for month %d", days_in_month(month, year), month);
73  this->day_.reset();
74  }
75  }
76 }
77 
79  this->validate_();
80  this->parent_->control(*this);
81 }
82 
83 DateCall &DateCall::set_date(uint16_t year, uint8_t month, uint8_t day) {
84  this->year_ = year;
85  this->month_ = month;
86  this->day_ = day;
87  return *this;
88 };
89 
90 DateCall &DateCall::set_date(ESPTime time) { return this->set_date(time.year, time.month, time.day_of_month); };
91 
92 DateCall &DateCall::set_date(const std::string &date) {
93  ESPTime val{};
94  if (!ESPTime::strptime(date, val)) {
95  ESP_LOGE(TAG, "Could not convert the date string to an ESPTime object");
96  return *this;
97  }
98  return this->set_date(val);
99 }
100 
102  DateCall call = date->make_call();
103  call.set_date(this->year, this->month, this->day);
104  return call;
105 }
106 
108  date->year_ = this->year;
109  date->month_ = this->month;
110  date->day_ = this->day;
111  date->publish_state();
112 }
113 
114 } // namespace datetime
115 } // namespace esphome
116 
117 #endif // USE_DATETIME_DATE
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
mopeka_std_values val[4]
CallbackManager< void()> state_callback_
Definition: datetime_base.h:21
uint8_t days_in_month(uint8_t month, uint16_t year)
Definition: time.cpp:10
DateCall & set_date(uint16_t year, uint8_t month, uint8_t day)
Definition: date_entity.cpp:83
uint16_t year
year
Definition: time.h:35
DateCall to_call(DateEntity *date)
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
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:29
const StringRef & get_name() const
Definition: entity_base.cpp:10
static bool strptime(const std::string &time_to_parse, ESPTime &esp_time)
Convert a string to ESPTime struct as specified by the format argument.
Definition: time.cpp:67