ESPHome  2024.8.3
datetime_base.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/time.h"
7 
9 
10 namespace esphome {
11 namespace datetime {
12 
13 class DateTimeBase : public EntityBase {
14  public:
16  bool has_state() const { return this->has_state_; }
17 
18  virtual ESPTime state_as_esptime() const = 0;
19 
20  void add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
21 
22  void set_rtc(time::RealTimeClock *rtc) { this->rtc_ = rtc; }
23  time::RealTimeClock *get_rtc() const { return this->rtc_; }
24 
25  protected:
27 
29 
30  bool has_state_{false};
31 };
32 
33 class DateTimeStateTrigger : public Trigger<ESPTime> {
34  public:
35  explicit DateTimeStateTrigger(DateTimeBase *parent) {
36  parent->add_on_state_callback([this, parent]() { this->trigger(parent->state_as_esptime()); });
37  }
38 };
39 
40 } // namespace datetime
41 } // namespace esphome
void set_rtc(time::RealTimeClock *rtc)
Definition: datetime_base.h:22
time::RealTimeClock * rtc_
Definition: datetime_base.h:28
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:26
bool has_state() const
Return whether this Datetime has gotten a full state yet.
Definition: datetime_base.h:16
void add_on_state_callback(std::function< void()> &&callback)
Definition: datetime_base.h:20
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:23
DateTimeStateTrigger(DateTimeBase *parent)
Definition: datetime_base.h:35