ESPHome  2024.10.2
datetime_base.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/time.h"
7 #ifdef USE_TIME
9 #endif
10 
11 namespace esphome {
12 namespace datetime {
13 
14 class DateTimeBase : public EntityBase {
15  public:
17  bool has_state() const { return this->has_state_; }
18 
19  virtual ESPTime state_as_esptime() const = 0;
20 
21  void add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
22 
23 #ifdef USE_TIME
24  void set_rtc(time::RealTimeClock *rtc) { this->rtc_ = rtc; }
25  time::RealTimeClock *get_rtc() const { return this->rtc_; }
26 #endif
27 
28  protected:
30 
31 #ifdef USE_TIME
33 #endif
34 
35  bool has_state_{false};
36 };
37 
38 #ifdef USE_TIME
39 class DateTimeStateTrigger : public Trigger<ESPTime> {
40  public:
41  explicit DateTimeStateTrigger(DateTimeBase *parent) {
42  parent->add_on_state_callback([this, parent]() { this->trigger(parent->state_as_esptime()); });
43  }
44 };
45 #endif
46 
47 } // namespace datetime
48 } // namespace esphome
void set_rtc(time::RealTimeClock *rtc)
Definition: datetime_base.h:24
time::RealTimeClock * rtc_
Definition: datetime_base.h:32
The RealTimeClock class exposes common timekeeping functions via the device&#39;s local real-time clock...
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
CallbackManager< void()> state_callback_
Definition: datetime_base.h:29
bool has_state() const
Return whether this Datetime has gotten a full state yet.
Definition: datetime_base.h:17
void add_on_state_callback(std::function< void()> &&callback)
Definition: datetime_base.h:21
virtual ESPTime state_as_esptime() const =0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
time::RealTimeClock * get_rtc() const
Definition: datetime_base.h:25
DateTimeStateTrigger(DateTimeBase *parent)
Definition: datetime_base.h:41