ESPHome 2025.12.2
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_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#if defined(USE_DATETIME_DATE) && defined(USE_CONTROLLER_REGISTRY)
37#endif
38}
39
41
43 if (this->year_.has_value() && (this->year_ < 1970 || this->year_ > 3000)) {
44 ESP_LOGE(TAG, "Year must be between 1970 and 3000");
45 this->year_.reset();
46 this->month_.reset();
47 this->day_.reset();
48 }
49 if (this->month_.has_value() && (this->month_ < 1 || this->month_ > 12)) {
50 ESP_LOGE(TAG, "Month must be between 1 and 12");
51 this->month_.reset();
52 this->day_.reset();
53 }
54 if (this->day_.has_value()) {
55 uint16_t year = 0;
56 uint8_t month = 0;
57 if (this->month_.has_value()) {
58 month = *this->month_;
59 } else {
60 if (this->parent_->month != 0) {
61 month = this->parent_->month;
62 } else {
63 ESP_LOGE(TAG, "Month must be set to validate day");
64 this->day_.reset();
65 }
66 }
67 if (this->year_.has_value()) {
68 year = *this->year_;
69 } else {
70 if (this->parent_->year != 0) {
71 year = this->parent_->year;
72 } else {
73 ESP_LOGE(TAG, "Year must be set to validate day");
74 this->day_.reset();
75 }
76 }
77 if (this->day_.has_value() && *this->day_ > days_in_month(month, year)) {
78 ESP_LOGE(TAG, "Day must be between 1 and %d for month %d", days_in_month(month, year), month);
79 this->day_.reset();
80 }
81 }
82}
83
85 this->validate_();
86 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
87
88 if (this->year_.has_value()) {
89 ESP_LOGD(TAG, " Year: %d", *this->year_);
90 }
91 if (this->month_.has_value()) {
92 ESP_LOGD(TAG, " Month: %d", *this->month_);
93 }
94 if (this->day_.has_value()) {
95 ESP_LOGD(TAG, " Day: %d", *this->day_);
96 }
97 this->parent_->control(*this);
98}
99
100DateCall &DateCall::set_date(uint16_t year, uint8_t month, uint8_t day) {
101 this->year_ = year;
102 this->month_ = month;
103 this->day_ = day;
104 return *this;
105};
106
107DateCall &DateCall::set_date(ESPTime time) { return this->set_date(time.year, time.month, time.day_of_month); };
108
109DateCall &DateCall::set_date(const std::string &date) {
110 ESPTime val{};
111 if (!ESPTime::strptime(date, val)) {
112 ESP_LOGE(TAG, "Could not convert the date string to an ESPTime object");
113 return *this;
114 }
115 return this->set_date(val);
116}
117
119 DateCall call = date->make_call();
120 call.set_date(this->year, this->month, this->day);
121 return call;
122}
123
125 date->year_ = this->year;
126 date->month_ = this->month;
127 date->day_ = this->day;
128 date->publish_state();
129}
130
131} // namespace esphome::datetime
132
133#endif // USE_DATETIME_DATE
static void notify_date_update(datetime::DateEntity *obj)
const StringRef & get_name() const
void set_has_state(bool state)
Definition entity_base.h:96
constexpr const char * c_str() const
Definition string_ref.h:69
DateCall & set_date(uint16_t year, uint8_t month, uint8_t day)
optional< uint8_t > day_
Definition date_entity.h:96
optional< uint8_t > month_
Definition date_entity.h:95
optional< int16_t > year_
Definition date_entity.h:94
virtual void control(const DateCall &call)=0
CallbackManager< void()> state_callback_
bool has_value() const
Definition optional.h:92
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[4]
uint8_t days_in_month(uint8_t month, uint16_t year)
Definition time.cpp:8
A more user-friendly version of struct tm from time.h.
Definition time.h:15
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:61
uint8_t day_of_month
day of the month [1-31]
Definition time.h:27
uint16_t year
year
Definition time.h:33
uint8_t month
month; january=1 [1-12]
Definition time.h:31