ESPHome  2024.10.2
date_entity.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 
5 #ifdef USE_DATETIME_DATE
6 
8 #include "esphome/core/helpers.h"
9 #include "esphome/core/time.h"
10 
11 #include "datetime_base.h"
12 
13 namespace esphome {
14 namespace datetime {
15 
16 #define LOG_DATETIME_DATE(prefix, type, obj) \
17  if ((obj) != nullptr) { \
18  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
19  if (!(obj)->get_icon().empty()) { \
20  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
21  } \
22  }
23 
24 class DateCall;
25 class DateEntity;
26 
28  uint16_t year;
29  uint8_t month;
30  uint8_t day;
31 
33  void apply(DateEntity *date);
34 } __attribute__((packed));
35 
36 class DateEntity : public DateTimeBase {
37  protected:
38  uint16_t year_;
39  uint8_t month_;
40  uint8_t day_;
41 
42  public:
43  void publish_state();
44  DateCall make_call();
45 
46  ESPTime state_as_esptime() const override {
47  ESPTime obj;
48  obj.year = this->year_;
49  obj.month = this->month_;
50  obj.day_of_month = this->day_;
51  return obj;
52  }
53 
54  const uint16_t &year = year_;
55  const uint8_t &month = month_;
56  const uint8_t &day = day_;
57 
58  protected:
59  friend class DateCall;
60  friend struct DateEntityRestoreState;
61 
62  virtual void control(const DateCall &call) = 0;
63 };
64 
65 class DateCall {
66  public:
67  explicit DateCall(DateEntity *parent) : parent_(parent) {}
68  void perform();
69  DateCall &set_date(uint16_t year, uint8_t month, uint8_t day);
70  DateCall &set_date(ESPTime time);
71  DateCall &set_date(const std::string &date);
72 
73  DateCall &set_year(uint16_t year) {
74  this->year_ = year;
75  return *this;
76  }
77  DateCall &set_month(uint8_t month) {
78  this->month_ = month;
79  return *this;
80  }
81  DateCall &set_day(uint8_t day) {
82  this->day_ = day;
83  return *this;
84  }
85 
86  optional<uint16_t> get_year() const { return this->year_; }
87  optional<uint8_t> get_month() const { return this->month_; }
88  optional<uint8_t> get_day() const { return this->day_; }
89 
90  protected:
91  void validate_();
92 
94 
98 };
99 
100 template<typename... Ts> class DateSetAction : public Action<Ts...>, public Parented<DateEntity> {
101  public:
103 
104  void play(Ts... x) override {
105  auto call = this->parent_->make_call();
106 
107  if (this->date_.has_value()) {
108  call.set_date(this->date_.value(x...));
109  }
110  call.perform();
111  }
112 };
113 
114 } // namespace datetime
115 } // namespace esphome
116 
117 #endif // USE_DATETIME_DATE
DateCall & set_year(uint16_t year)
Definition: date_entity.h:73
DateCall & set_month(uint8_t month)
Definition: date_entity.h:77
optional< uint8_t > month_
Definition: date_entity.h:96
uint16_t x
Definition: tt21100.cpp:17
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
optional< uint8_t > day_
Definition: date_entity.h:97
DateCall(DateEntity *parent)
Definition: date_entity.h:67
TEMPLATABLE_VALUE(ESPTime, date) void play(Ts... x) override
Definition: date_entity.h:102
optional< uint8_t > get_day() const
Definition: date_entity.h:88
optional< int16_t > year_
Definition: date_entity.h:95
ESPTime state_as_esptime() const override
Definition: date_entity.h:46
uint16_t year
year
Definition: time.h:35
DateCall & set_day(uint8_t day)
Definition: date_entity.h:81
optional< uint16_t > get_year() const
Definition: date_entity.h:86
esphome::datetime::DateEntity __attribute__
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
optional< uint8_t > get_month() const
Definition: date_entity.h:87
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:29
Helper class to easily give an object a parent of type T.
Definition: helpers.h:521