ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
date_entity.h
Go to the documentation of this file.
1#pragma once
5#ifdef USE_DATETIME_DATE
6
9#include "esphome/core/time.h"
10
11#include "datetime_base.h"
12
13namespace esphome::datetime {
14
15#define LOG_DATETIME_DATE(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 DateCall;
24class DateEntity;
25
27 uint16_t year;
28 uint8_t month;
29 uint8_t day;
30
32 void apply(DateEntity *date);
33} __attribute__((packed));
34
35class DateEntity : public DateTimeBase {
36 protected:
37 uint16_t year_;
38 uint8_t month_;
39 uint8_t day_;
40
41 public:
42 void publish_state();
44
45 ESPTime state_as_esptime() const override {
46 ESPTime obj;
47 obj.year = this->year_;
48 obj.month = this->month_;
49 obj.day_of_month = this->day_;
50 return obj;
51 }
52
53 const uint16_t &year = year_;
54 const uint8_t &month = month_;
55 const uint8_t &day = day_;
56
57 protected:
58 friend class DateCall;
60
61 virtual void control(const DateCall &call) = 0;
62};
63
64class DateCall {
65 public:
66 explicit DateCall(DateEntity *parent) : parent_(parent) {}
67 void perform();
68 DateCall &set_date(uint16_t year, uint8_t month, uint8_t day);
70 DateCall &set_date(const std::string &date);
71
72 DateCall &set_year(uint16_t year) {
73 this->year_ = year;
74 return *this;
75 }
77 this->month_ = month;
78 return *this;
79 }
80 DateCall &set_day(uint8_t day) {
81 this->day_ = day;
82 return *this;
83 }
84
85 optional<uint16_t> get_year() const { return this->year_; }
86 optional<uint8_t> get_month() const { return this->month_; }
87 optional<uint8_t> get_day() const { return this->day_; }
88
89 protected:
90 void validate_();
91
93
97};
98
99template<typename... Ts> class DateSetAction : public Action<Ts...>, public Parented<DateEntity> {
100 public:
102
103 void play(const Ts &...x) override {
104 auto call = this->parent_->make_call();
105
106 if (this->date_.has_value()) {
107 call.set_date(this->date_.value(x...));
108 }
109 call.perform();
110 }
111};
112
113} // namespace esphome::datetime
114
115#endif // USE_DATETIME_DATE
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:1102
DateCall & set_date(uint16_t year, uint8_t month, uint8_t day)
optional< uint8_t > day_
Definition date_entity.h:96
optional< uint8_t > month_
Definition date_entity.h:95
optional< int16_t > year_
Definition date_entity.h:94
DateCall(DateEntity *parent)
Definition date_entity.h:66
optional< uint8_t > get_month() const
Definition date_entity.h:86
DateCall & set_month(uint8_t month)
Definition date_entity.h:76
optional< uint8_t > get_day() const
Definition date_entity.h:87
DateCall & set_day(uint8_t day)
Definition date_entity.h:80
optional< uint16_t > get_year() const
Definition date_entity.h:85
DateCall & set_year(uint16_t year)
Definition date_entity.h:72
ESPTime state_as_esptime() const override
Definition date_entity.h:45
virtual void control(const DateCall &call)=0
TEMPLATABLE_VALUE(ESPTime, date) void play(const Ts &...x) override
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t day
Definition date_entity.h:2
esphome::datetime::DateEntity __attribute__
A more user-friendly version of struct tm from time.h.
Definition time.h:15
uint8_t day_of_month
day of the month [1-31]
Definition time.h:27
uint16_t year
year
Definition time.h:33
uint8_t month
month; january=1 [1-12]
Definition time.h:31
uint16_t x
Definition tt21100.cpp:5