ESPHome  2024.11.1
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 namespace esphome {
14 namespace datetime {
15 
16 #define LOG_DATETIME_TIME(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 TimeCall;
25 class TimeEntity;
26 class OnTimeTrigger;
27 
29  uint8_t hour;
30  uint8_t minute;
31  uint8_t second;
32 
34  void apply(TimeEntity *time);
35 } __attribute__((packed));
36 
37 class TimeEntity : public DateTimeBase {
38  protected:
39  uint8_t hour_;
40  uint8_t minute_;
41  uint8_t second_;
42 
43  public:
44  void publish_state();
45  TimeCall make_call();
46 
47  ESPTime state_as_esptime() const override {
48  ESPTime obj;
49  obj.hour = this->hour_;
50  obj.minute = this->minute_;
51  obj.second = this->second_;
52  return obj;
53  }
54 
55  const uint8_t &hour = hour_;
56  const uint8_t &minute = minute_;
57  const uint8_t &second = second_;
58 
59  protected:
60  friend class TimeCall;
61  friend struct TimeEntityRestoreState;
62  friend class OnTimeTrigger;
63 
64  virtual void control(const TimeCall &call) = 0;
65 };
66 
67 class TimeCall {
68  public:
69  explicit TimeCall(TimeEntity *parent) : parent_(parent) {}
70  void perform();
71  TimeCall &set_time(uint8_t hour, uint8_t minute, uint8_t second);
72  TimeCall &set_time(ESPTime time);
73  TimeCall &set_time(const std::string &time);
74 
75  TimeCall &set_hour(uint8_t hour) {
76  this->hour_ = hour;
77  return *this;
78  }
79  TimeCall &set_minute(uint8_t minute) {
80  this->minute_ = minute;
81  return *this;
82  }
83  TimeCall &set_second(uint8_t second) {
84  this->second_ = second;
85  return *this;
86  }
87 
88  optional<uint8_t> get_hour() const { return this->hour_; }
89  optional<uint8_t> get_minute() const { return this->minute_; }
90  optional<uint8_t> get_second() const { return this->second_; }
91 
92  protected:
93  void validate_();
94 
96 
100 };
101 
102 template<typename... Ts> class TimeSetAction : public Action<Ts...>, public Parented<TimeEntity> {
103  public:
105 
106  void play(Ts... x) override {
107  auto call = this->parent_->make_call();
108 
109  if (this->time_.has_value()) {
110  call.set_time(this->time_.value(x...));
111  }
112  call.perform();
113  }
114 };
115 
116 #ifdef USE_TIME
117 class OnTimeTrigger : public Trigger<>, public Component, public Parented<TimeEntity> {
118  public:
119  void loop() override;
120 
121  protected:
122  bool matches_(const ESPTime &time) const;
123 
125 };
126 #endif
127 
128 } // namespace datetime
129 } // namespace esphome
130 
131 #endif // USE_DATETIME_TIME
TEMPLATABLE_VALUE(ESPTime, time) void play(Ts... x) override
Definition: time_entity.h:104
ESPTime state_as_esptime() const override
Definition: time_entity.h:47
void loop()
optional< uint8_t > get_hour() const
Definition: time_entity.h:88
optional< uint8_t > hour_
Definition: time_entity.h:97
uint16_t x
Definition: tt21100.cpp:17
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
TimeCall & set_minute(uint8_t minute)
Definition: time_entity.h:79
TimeCall(TimeEntity *parent)
Definition: time_entity.h:69
TimeCall & set_second(uint8_t second)
Definition: time_entity.h:83
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:98
optional< ESPTime > last_check_
Definition: time_entity.h:124
optional< uint8_t > get_minute() const
Definition: time_entity.h:89
TimeCall to_call(TimeEntity *time)
Definition: time_entity.cpp:84
esphome::datetime::DateEntity __attribute__
optional< uint8_t > get_second() const
Definition: time_entity.h:90
optional< uint8_t > second_
Definition: time_entity.h:99
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
TimeCall & set_hour(uint8_t hour)
Definition: time_entity.h:75
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:522