ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
datetime_entity.h
Go to the documentation of this file.
1#pragma once
5#ifdef USE_DATETIME_DATETIME
6
9#include "esphome/core/time.h"
10
11#include "datetime_base.h"
12
13namespace esphome::datetime {
14
15#define LOG_DATETIME_DATETIME(prefix, type, obj) \
16 if ((obj) != nullptr) { \
17 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
18 if (!(obj)->get_icon_ref().empty()) { \
19 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
20 } \
21 }
22
23class DateTimeCall;
24class DateTimeEntity;
25
27 uint16_t year;
28 uint8_t month;
29 uint8_t day;
30 uint8_t hour;
31 uint8_t minute;
32 uint8_t second;
33
35 void apply(DateTimeEntity *datetime);
36} __attribute__((packed));
37
39 protected:
40 uint16_t year_;
41 uint8_t month_;
42 uint8_t day_;
43 uint8_t hour_;
44 uint8_t minute_;
45 uint8_t second_;
46
47 public:
48 void publish_state();
50
51 ESPTime state_as_esptime() const override;
52
53 const uint16_t &year = year_;
54 const uint8_t &month = month_;
55 const uint8_t &day = day_;
56 const uint8_t &hour = hour_;
57 const uint8_t &minute = minute_;
58 const uint8_t &second = second_;
59
60 protected:
61 friend class DateTimeCall;
63 friend class OnDateTimeTrigger;
64
65 virtual void control(const DateTimeCall &call) = 0;
66};
67
69 public:
70 explicit DateTimeCall(DateTimeEntity *parent) : parent_(parent) {}
71 void perform();
72 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(const std::string &datetime);
75 DateTimeCall &set_datetime(time_t epoch_seconds);
76
78 this->year_ = year;
79 return *this;
80 }
82 this->month_ = month;
83 return *this;
84 }
86 this->day_ = day;
87 return *this;
88 }
90 this->hour_ = hour;
91 return *this;
92 }
94 this->minute_ = minute;
95 return *this;
96 }
98 this->second_ = second;
99 return *this;
100 }
101
102 optional<uint16_t> get_year() const { return this->year_; }
103 optional<uint8_t> get_month() const { return this->month_; }
104 optional<uint8_t> get_day() const { return this->day_; }
105 optional<uint8_t> get_hour() const { return this->hour_; }
106 optional<uint8_t> get_minute() const { return this->minute_; }
107 optional<uint8_t> get_second() const { return this->second_; }
108
109 protected:
110 void validate_();
111
113
120};
121
122template<typename... Ts> class DateTimeSetAction : public Action<Ts...>, public Parented<DateTimeEntity> {
123 public:
125
126 void play(const Ts &...x) override {
127 auto call = this->parent_->make_call();
128
129 if (this->datetime_.has_value()) {
130 call.set_datetime(this->datetime_.value(x...));
131 }
132 call.perform();
133 }
134};
135
136#ifdef USE_TIME
137class OnDateTimeTrigger : public Trigger<>, public Component, public Parented<DateTimeEntity> {
138 public:
139 void loop() override;
140
141 protected:
142 bool matches_(const ESPTime &time) const;
143
145};
146#endif
147
148} // namespace esphome::datetime
149
150#endif // USE_DATETIME_DATETIME
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:1102
optional< uint16_t > get_year() const
DateTimeCall(DateTimeEntity *parent)
optional< uint8_t > get_hour() const
optional< uint8_t > get_month() const
DateTimeCall & set_year(uint16_t year)
DateTimeCall & set_day(uint8_t day)
optional< uint8_t > get_minute() const
optional< uint8_t > get_day() const
optional< uint8_t > get_second() const
DateTimeCall & set_month(uint8_t month)
DateTimeCall & set_datetime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
DateTimeCall & set_second(uint8_t second)
DateTimeCall & set_minute(uint8_t minute)
DateTimeCall & set_hour(uint8_t hour)
virtual void control(const DateTimeCall &call)=0
ESPTime state_as_esptime() const override
TEMPLATABLE_VALUE(ESPTime, datetime) void play(const Ts &...x) override
bool matches_(const ESPTime &time) const
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t day
Definition date_entity.h:2
uint8_t second
uint8_t minute
uint8_t hour
esphome::datetime::DateEntity __attribute__
A more user-friendly version of struct tm from time.h.
Definition time.h:15
DateTimeCall to_call(DateTimeEntity *datetime)
uint16_t x
Definition tt21100.cpp:5