ESPHome  2024.9.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  this->month_.reset();
44  this->day_.reset();
45  }
46  if (this->month_.has_value() && (this->month_ < 1 || this->month_ > 12)) {
47  ESP_LOGE(TAG, "Month must be between 1 and 12");
48  this->month_.reset();
49  this->day_.reset();
50  }
51  if (this->day_.has_value()) {
52  uint16_t year = 0;
53  uint8_t month = 0;
54  if (this->month_.has_value()) {
55  month = *this->month_;
56  } else {
57  if (this->parent_->month != 0) {
58  month = this->parent_->month;
59  } else {
60  ESP_LOGE(TAG, "Month must be set to validate day");
61  this->day_.reset();
62  }
63  }
64  if (this->year_.has_value()) {
65  year = *this->year_;
66  } else {
67  if (this->parent_->year != 0) {
68  year = this->parent_->year;
69  } else {
70  ESP_LOGE(TAG, "Year must be set to validate day");
71  this->day_.reset();
72  }
73  }
74  if (this->day_.has_value() && *this->day_ > days_in_month(month, year)) {
75  ESP_LOGE(TAG, "Day must be between 1 and %d for month %d", days_in_month(month, year), month);
76  this->day_.reset();
77  }
78  }
79 }
80 
82  this->validate_();
83  ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
84 
85  if (this->year_.has_value()) {
86  ESP_LOGD(TAG, " Year: %d", *this->year_);
87  }
88  if (this->month_.has_value()) {
89  ESP_LOGD(TAG, " Month: %d", *this->month_);
90  }
91  if (this->day_.has_value()) {
92  ESP_LOGD(TAG, " Day: %d", *this->day_);
93  }
94  this->parent_->control(*this);
95 }
96 
97 DateCall &DateCall::set_date(uint16_t year, uint8_t month, uint8_t day) {
98  this->year_ = year;
99  this->month_ = month;
100  this->day_ = day;
101  return *this;
102 };
103 
104 DateCall &DateCall::set_date(ESPTime time) { return this->set_date(time.year, time.month, time.day_of_month); };
105 
106 DateCall &DateCall::set_date(const std::string &date) {
107  ESPTime val{};
108  if (!ESPTime::strptime(date, val)) {
109  ESP_LOGE(TAG, "Could not convert the date string to an ESPTime object");
110  return *this;
111  }
112  return this->set_date(val);
113 }
114 
116  DateCall call = date->make_call();
117  call.set_date(this->year, this->month, this->day);
118  return call;
119 }
120 
122  date->year_ = this->year;
123  date->month_ = this->month;
124  date->day_ = this->day;
125  date->publish_state();
126 }
127 
128 } // namespace datetime
129 } // namespace esphome
130 
131 #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:26
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:97
uint16_t year
year
Definition: time.h:35
DateCall to_call(DateEntity *date)
Implementation of SPI Controller mode.
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