ESPHome 2025.6.3
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 {
14namespace datetime {
15
16#define LOG_DATETIME_DATETIME(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
24class DateTimeCall;
25class DateTimeEntity;
26
28 uint16_t year;
29 uint8_t month;
30 uint8_t day;
31 uint8_t hour;
32 uint8_t minute;
33 uint8_t second;
34
36 void apply(DateTimeEntity *datetime);
37} __attribute__((packed));
38
40 protected:
41 uint16_t year_;
42 uint8_t month_;
43 uint8_t day_;
44 uint8_t hour_;
45 uint8_t minute_;
46 uint8_t second_;
47
48 public:
49 void publish_state();
51
52 ESPTime state_as_esptime() const override;
53
54 const uint16_t &year = year_;
55 const uint8_t &month = month_;
56 const uint8_t &day = day_;
57 const uint8_t &hour = hour_;
58 const uint8_t &minute = minute_;
59 const uint8_t &second = second_;
60
61 protected:
62 friend class DateTimeCall;
64 friend class OnDateTimeTrigger;
65
66 virtual void control(const DateTimeCall &call) = 0;
67};
68
70 public:
71 explicit DateTimeCall(DateTimeEntity *parent) : parent_(parent) {}
72 void perform();
73 DateTimeCall &set_datetime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second);
75 DateTimeCall &set_datetime(const std::string &datetime);
76 DateTimeCall &set_datetime(time_t epoch_seconds);
77
79 this->year_ = year;
80 return *this;
81 }
83 this->month_ = month;
84 return *this;
85 }
87 this->day_ = day;
88 return *this;
89 }
91 this->hour_ = hour;
92 return *this;
93 }
95 this->minute_ = minute;
96 return *this;
97 }
99 this->second_ = second;
100 return *this;
101 }
102
103 optional<uint16_t> get_year() const { return this->year_; }
104 optional<uint8_t> get_month() const { return this->month_; }
105 optional<uint8_t> get_day() const { return this->day_; }
106 optional<uint8_t> get_hour() const { return this->hour_; }
107 optional<uint8_t> get_minute() const { return this->minute_; }
108 optional<uint8_t> get_second() const { return this->second_; }
109
110 protected:
111 void validate_();
112
114
121};
122
123template<typename... Ts> class DateTimeSetAction : public Action<Ts...>, public Parented<DateTimeEntity> {
124 public:
126
127 void play(Ts... x) override {
128 auto call = this->parent_->make_call();
129
130 if (this->datetime_.has_value()) {
131 call.set_datetime(this->datetime_.value(x...));
132 }
133 call.perform();
134 }
135};
136
137#ifdef USE_TIME
138class OnDateTimeTrigger : public Trigger<>, public Component, public Parented<DateTimeEntity> {
139 public:
140 void loop() override;
141
142 protected:
143 bool matches_(const ESPTime &time) const;
144
146};
147#endif
148
149} // namespace datetime
150} // namespace esphome
151
152#endif // USE_DATETIME_DATETIME
virtual void play(Ts... x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:539
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(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__
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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