ESPHome 2025.12.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
8namespace esphome {
9namespace datetime {
10
11static const char *const TAG = "datetime.date_entity";
12
14 if (this->year_ == 0 || this->month_ == 0 || this->day_ == 0) {
15 this->set_has_state(false);
16 return;
17 }
18 if (this->year_ < 1970 || this->year_ > 3000) {
19 this->set_has_state(false);
20 ESP_LOGE(TAG, "Year must be between 1970 and 3000");
21 return;
22 }
23 if (this->month_ < 1 || this->month_ > 12) {
24 this->set_has_state(false);
25 ESP_LOGE(TAG, "Month must be between 1 and 12");
26 return;
27 }
28 if (this->day_ > days_in_month(this->month_, this->year_)) {
29 this->set_has_state(false);
30 ESP_LOGE(TAG, "Day must be between 1 and %d for month %d", days_in_month(this->month_, this->year_), this->month_);
31 return;
32 }
33 this->set_has_state(true);
34 ESP_LOGD(TAG, "'%s': Sending date %d-%d-%d", this->get_name().c_str(), this->year_, this->month_, this->day_);
35 this->state_callback_.call();
36#if defined(USE_DATETIME_DATE) && defined(USE_CONTROLLER_REGISTRY)
38#endif
39}
40
42
44 if (this->year_.has_value() && (this->year_ < 1970 || this->year_ > 3000)) {
45 ESP_LOGE(TAG, "Year must be between 1970 and 3000");
46 this->year_.reset();
47 this->month_.reset();
48 this->day_.reset();
49 }
50 if (this->month_.has_value() && (this->month_ < 1 || this->month_ > 12)) {
51 ESP_LOGE(TAG, "Month must be between 1 and 12");
52 this->month_.reset();
53 this->day_.reset();
54 }
55 if (this->day_.has_value()) {
56 uint16_t year = 0;
57 uint8_t month = 0;
58 if (this->month_.has_value()) {
59 month = *this->month_;
60 } else {
61 if (this->parent_->month != 0) {
62 month = this->parent_->month;
63 } else {
64 ESP_LOGE(TAG, "Month must be set to validate day");
65 this->day_.reset();
66 }
67 }
68 if (this->year_.has_value()) {
69 year = *this->year_;
70 } else {
71 if (this->parent_->year != 0) {
72 year = this->parent_->year;
73 } else {
74 ESP_LOGE(TAG, "Year must be set to validate day");
75 this->day_.reset();
76 }
77 }
78 if (this->day_.has_value() && *this->day_ > days_in_month(month, year)) {
79 ESP_LOGE(TAG, "Day must be between 1 and %d for month %d", days_in_month(month, year), month);
80 this->day_.reset();
81 }
82 }
83}
84
86 this->validate_();
87 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
88
89 if (this->year_.has_value()) {
90 ESP_LOGD(TAG, " Year: %d", *this->year_);
91 }
92 if (this->month_.has_value()) {
93 ESP_LOGD(TAG, " Month: %d", *this->month_);
94 }
95 if (this->day_.has_value()) {
96 ESP_LOGD(TAG, " Day: %d", *this->day_);
97 }
98 this->parent_->control(*this);
99}
100
101DateCall &DateCall::set_date(uint16_t year, uint8_t month, uint8_t day) {
102 this->year_ = year;
103 this->month_ = month;
104 this->day_ = day;
105 return *this;
106};
107
108DateCall &DateCall::set_date(ESPTime time) { return this->set_date(time.year, time.month, time.day_of_month); };
109
110DateCall &DateCall::set_date(const std::string &date) {
111 ESPTime val{};
112 if (!ESPTime::strptime(date, val)) {
113 ESP_LOGE(TAG, "Could not convert the date string to an ESPTime object");
114 return *this;
115 }
116 return this->set_date(val);
117}
118
120 DateCall call = date->make_call();
121 call.set_date(this->year, this->month, this->day);
122 return call;
123}
124
126 date->year_ = this->year;
127 date->month_ = this->month;
128 date->day_ = this->day;
129 date->publish_state();
130}
131
132} // namespace datetime
133} // namespace esphome
134
135#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:93
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:97
optional< uint8_t > month_
Definition date_entity.h:96
optional< int16_t > year_
Definition date_entity.h:95
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]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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