ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
date_entity.cpp
Go to the documentation of this file.
1#include "date_entity.h"
4#ifdef USE_DATETIME_DATE
5
6#include "esphome/core/log.h"
7
9
10static const char *const TAG = "datetime.date_entity";
11
13 if (this->year_ == 0 || this->month_ == 0 || this->day_ == 0) {
14 this->set_has_state(false);
15 return;
16 }
17 if (this->year_ < 1970 || this->year_ > 3000) {
18 this->set_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->set_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->set_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->set_has_state(true);
33 ESP_LOGV(TAG, "'%s' >> %d-%d-%d", this->get_name().c_str(), this->year_, this->month_, this->day_);
34 this->state_callback_.call();
35#if defined(USE_DATETIME_DATE) && defined(USE_CONTROLLER_REGISTRY)
36 ControllerRegistry::notify_date_update(this);
37#endif
38}
39
41 if (this->year_.has_value() && (this->year_ < 1970 || this->year_ > 3000)) {
42 ESP_LOGE(TAG, "Year must be between 1970 and 3000");
43 this->year_.reset();
44 this->month_.reset();
45 this->day_.reset();
46 }
47 if (this->month_.has_value() && (this->month_ < 1 || this->month_ > 12)) {
48 ESP_LOGE(TAG, "Month must be between 1 and 12");
49 this->month_.reset();
50 this->day_.reset();
51 }
52 if (this->day_.has_value()) {
53 uint16_t year = 0;
54 uint8_t month = 0;
55 if (this->month_.has_value()) {
56 month = *this->month_;
57 } else {
58 if (this->parent_->month != 0) {
59 month = this->parent_->month;
60 } else {
61 ESP_LOGE(TAG, "Month must be set to validate day");
62 this->day_.reset();
63 }
64 }
65 if (this->year_.has_value()) {
66 year = *this->year_;
67 } else {
68 if (this->parent_->year != 0) {
69 year = this->parent_->year;
70 } else {
71 ESP_LOGE(TAG, "Year must be set to validate day");
72 this->day_.reset();
73 }
74 }
75 if (this->day_.has_value() && *this->day_ > days_in_month(month, year)) {
76 ESP_LOGE(TAG, "Day must be between 1 and %d for month %d", days_in_month(month, year), month);
77 this->day_.reset();
78 }
79 }
80}
81
83 this->validate_();
84 ESP_LOGV(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
85
86 if (this->year_.has_value()) {
87 ESP_LOGV(TAG, " Year: %d", *this->year_);
88 }
89 if (this->month_.has_value()) {
90 ESP_LOGV(TAG, " Month: %d", *this->month_);
91 }
92 if (this->day_.has_value()) {
93 ESP_LOGV(TAG, " Day: %d", *this->day_);
94 }
95 this->parent_->control(*this);
96}
97
98DateCall &DateCall::set_date(uint16_t year, uint8_t month, uint8_t day) {
99 this->year_ = year;
100 this->month_ = month;
101 this->day_ = day;
102 return *this;
103};
104
105DateCall &DateCall::set_date(ESPTime time) { return this->set_date(time.year, time.month, time.day_of_month); };
106
107DateCall &DateCall::set_date(const char *date, size_t len) {
108 ESPTime val{};
109 if (!ESPTime::strptime(date, len, val)) {
110 ESP_LOGE(TAG, "Could not convert the date string to an ESPTime object");
111 return *this;
112 }
113 return this->set_date(val);
114}
115
117 DateCall call = date->make_call();
118 call.set_date(this->year, this->month, this->day);
119 return call;
120}
121
123 date->year_ = this->year;
124 date->month_ = this->month;
125 date->day_ = this->day;
126 date->publish_state();
127}
128
129} // namespace esphome::datetime
130
131#endif // USE_DATETIME_DATE
const StringRef & get_name() const
Definition entity_base.h:71
void set_has_state(bool state)
constexpr const char * c_str() const
Definition string_ref.h:73
DateCall & set_date(uint16_t year, uint8_t month, uint8_t day)
optional< uint8_t > day_
Definition date_entity.h:96
optional< uint16_t > year_
Definition date_entity.h:94
optional< uint8_t > month_
Definition date_entity.h:95
virtual void control(const DateCall &call)=0
LazyCallbackManager< void()> state_callback_
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t day
Definition date_entity.h:2
mopeka_std_values val[3]
uint8_t days_in_month(uint8_t month, uint16_t year)
Definition time.cpp:11
const void size_t len
Definition hal.h:64
A more user-friendly version of struct tm from time.h.
Definition time.h:23
static bool strptime(const char *time_to_parse, size_t len, ESPTime &esp_time)
Convert a string to ESPTime struct as specified by the format argument.
Definition time.cpp:137
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42