ESPHome  2024.11.2
datetime_entity.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 
5 #ifdef USE_DATETIME_DATETIME
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_DATETIME(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 DateTimeCall;
25 class DateTimeEntity;
26 
28  uint16_t year;
29  uint8_t month;
30  uint8_t day;
31  uint8_t hour;
32  uint8_t minute;
33  uint8_t second;
34 
36  void apply(DateTimeEntity *datetime);
37 } __attribute__((packed));
38 
39 class DateTimeEntity : public DateTimeBase {
40  protected:
41  uint16_t year_;
42  uint8_t month_;
43  uint8_t day_;
44  uint8_t hour_;
45  uint8_t minute_;
46  uint8_t second_;
47 
48  public:
49  void publish_state();
50  DateTimeCall make_call();
51 
52  ESPTime state_as_esptime() const override;
53 
54  const uint16_t &year = year_;
55  const uint8_t &month = month_;
56  const uint8_t &day = day_;
57  const uint8_t &hour = hour_;
58  const uint8_t &minute = minute_;
59  const uint8_t &second = second_;
60 
61  protected:
62  friend class DateTimeCall;
64  friend class OnDateTimeTrigger;
65 
66  virtual void control(const DateTimeCall &call) = 0;
67 };
68 
69 class DateTimeCall {
70  public:
71  explicit DateTimeCall(DateTimeEntity *parent) : parent_(parent) {}
72  void perform();
73  DateTimeCall &set_datetime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second);
74  DateTimeCall &set_datetime(ESPTime datetime);
75  DateTimeCall &set_datetime(const std::string &datetime);
76  DateTimeCall &set_datetime(time_t epoch_seconds);
77 
78  DateTimeCall &set_year(uint16_t year) {
79  this->year_ = year;
80  return *this;
81  }
82  DateTimeCall &set_month(uint8_t month) {
83  this->month_ = month;
84  return *this;
85  }
86  DateTimeCall &set_day(uint8_t day) {
87  this->day_ = day;
88  return *this;
89  }
90  DateTimeCall &set_hour(uint8_t hour) {
91  this->hour_ = hour;
92  return *this;
93  }
94  DateTimeCall &set_minute(uint8_t minute) {
95  this->minute_ = minute;
96  return *this;
97  }
98  DateTimeCall &set_second(uint8_t second) {
99  this->second_ = second;
100  return *this;
101  }
102 
103  optional<uint16_t> get_year() const { return this->year_; }
104  optional<uint8_t> get_month() const { return this->month_; }
105  optional<uint8_t> get_day() const { return this->day_; }
106  optional<uint8_t> get_hour() const { return this->hour_; }
107  optional<uint8_t> get_minute() const { return this->minute_; }
108  optional<uint8_t> get_second() const { return this->second_; }
109 
110  protected:
111  void validate_();
112 
114 
121 };
122 
123 template<typename... Ts> class DateTimeSetAction : public Action<Ts...>, public Parented<DateTimeEntity> {
124  public:
126 
127  void play(Ts... x) override {
128  auto call = this->parent_->make_call();
129 
130  if (this->datetime_.has_value()) {
131  call.set_datetime(this->datetime_.value(x...));
132  }
133  call.perform();
134  }
135 };
136 
137 #ifdef USE_TIME
138 class OnDateTimeTrigger : public Trigger<>, public Component, public Parented<DateTimeEntity> {
139  public:
140  void loop() override;
141 
142  protected:
143  bool matches_(const ESPTime &time) const;
144 
146 };
147 #endif
148 
149 } // namespace datetime
150 } // namespace esphome
151 
152 #endif // USE_DATETIME_DATETIME
void loop()
DateTimeCall & set_minute(uint8_t minute)
optional< uint16_t > get_year() const
DateTimeCall & set_hour(uint8_t hour)
DateTimeCall & set_second(uint8_t second)
optional< uint8_t > get_day() const
DateTimeCall & set_year(uint16_t year)
optional< uint8_t > get_second() const
uint16_t x
Definition: tt21100.cpp:17
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
TEMPLATABLE_VALUE(ESPTime, datetime) void play(Ts... x) override
DateTimeCall & set_day(uint8_t day)
DateTimeCall(DateTimeEntity *parent)
optional< uint8_t > get_month() const
optional< uint8_t > get_hour() const
DateTimeCall & set_month(uint8_t month)
esphome::datetime::DateEntity __attribute__
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
DateTimeCall to_call(DateTimeEntity *datetime)
optional< uint8_t > get_minute() const
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522