ESPHome  2024.4.2
time_entity.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 
5 #ifdef USE_DATETIME_TIME
6 
8 #include "esphome/core/helpers.h"
9 #include "esphome/core/time.h"
10 
11 #include "datetime_base.h"
12 
13 #ifdef USE_TIME
15 #endif
16 
17 namespace esphome {
18 namespace datetime {
19 
20 #define LOG_DATETIME_TIME(prefix, type, obj) \
21  if ((obj) != nullptr) { \
22  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
23  if (!(obj)->get_icon().empty()) { \
24  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
25  } \
26  }
27 
28 class TimeCall;
29 class TimeEntity;
30 
32  uint8_t hour;
33  uint8_t minute;
34  uint8_t second;
35 
37  void apply(TimeEntity *time);
38 } __attribute__((packed));
39 
40 class TimeEntity : public DateTimeBase {
41  protected:
42  uint8_t hour_;
43  uint8_t minute_;
44  uint8_t second_;
45 
46  public:
47  void publish_state();
48  TimeCall make_call();
49 
50  ESPTime state_as_esptime() const override {
51  ESPTime obj;
52  obj.hour = this->hour_;
53  obj.minute = this->minute_;
54  obj.second = this->second_;
55  return obj;
56  }
57 
58  const uint8_t &hour = hour_;
59  const uint8_t &minute = minute_;
60  const uint8_t &second = second_;
61 
62  protected:
63  friend class TimeCall;
64  friend struct TimeEntityRestoreState;
65 
66  virtual void control(const TimeCall &call) = 0;
67 };
68 
69 class TimeCall {
70  public:
71  explicit TimeCall(TimeEntity *parent) : parent_(parent) {}
72  void perform();
73  TimeCall &set_time(uint8_t hour, uint8_t minute, uint8_t second);
74  TimeCall &set_time(ESPTime time);
75  TimeCall &set_time(const std::string &time);
76 
77  TimeCall &set_hour(uint8_t hour) {
78  this->hour_ = hour;
79  return *this;
80  }
81  TimeCall &set_minute(uint8_t minute) {
82  this->minute_ = minute;
83  return *this;
84  }
85  TimeCall &set_second(uint8_t second) {
86  this->second_ = second;
87  return *this;
88  }
89 
90  optional<uint8_t> get_hour() const { return this->hour_; }
91  optional<uint8_t> get_minute() const { return this->minute_; }
92  optional<uint8_t> get_second() const { return this->second_; }
93 
94  protected:
95  void validate_();
96 
98 
102 };
103 
104 template<typename... Ts> class TimeSetAction : public Action<Ts...>, public Parented<TimeEntity> {
105  public:
107 
108  void play(Ts... x) override {
109  auto call = this->parent_->make_call();
110 
111  if (this->time_.has_value()) {
112  call.set_time(this->time_.value(x...));
113  }
114  call.perform();
115  }
116 };
117 
118 #ifdef USE_TIME
119 
120 class OnTimeTrigger : public Trigger<>, public Component, public Parented<TimeEntity> {
121  public:
122  explicit OnTimeTrigger(time::RealTimeClock *rtc) : rtc_(rtc) {}
123  void loop() override;
124 
125  protected:
126  bool matches_(const ESPTime &time) const;
127 
130 };
131 
132 #endif
133 
134 } // namespace datetime
135 } // namespace esphome
136 
137 #endif // USE_DATETIME_TIME
TEMPLATABLE_VALUE(ESPTime, time) void play(Ts... x) override
Definition: time_entity.h:106
ESPTime state_as_esptime() const override
Definition: time_entity.h:50
time::RealTimeClock * rtc_
Definition: time_entity.h:128
void loop()
optional< uint8_t > get_hour() const
Definition: time_entity.h:90
optional< uint8_t > hour_
Definition: time_entity.h:99
The RealTimeClock class exposes common timekeeping functions via the device&#39;s local real-time clock...
uint16_t x
Definition: tt21100.cpp:17
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
OnTimeTrigger(time::RealTimeClock *rtc)
Definition: time_entity.h:122
TimeCall & set_minute(uint8_t minute)
Definition: time_entity.h:81
TimeCall(TimeEntity *parent)
Definition: time_entity.h:71
TimeCall & set_second(uint8_t second)
Definition: time_entity.h:85
uint8_t second
seconds after the minute [0-60]
Definition: time.h:21
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:23
optional< uint8_t > minute_
Definition: time_entity.h:100
optional< ESPTime > last_check_
Definition: time_entity.h:129
optional< uint8_t > get_minute() const
Definition: time_entity.h:91
TimeCall to_call(TimeEntity *time)
Definition: time_entity.cpp:84
esphome::datetime::DateEntity __attribute__
optional< uint8_t > get_second() const
Definition: time_entity.h:92
optional< uint8_t > second_
Definition: time_entity.h:101
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
TimeCall & set_hour(uint8_t hour)
Definition: time_entity.h:77
uint8_t hour
hours since midnight [0-23]
Definition: time.h:25
Helper class to easily give an object a parent of type T.
Definition: helpers.h:515