ESPHome 2025.5.0
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 {
14namespace datetime {
15
16#define LOG_DATETIME_DATE(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 DateCall;
25class DateEntity;
26
28 uint16_t year;
29 uint8_t month;
30 uint8_t day;
31
33 void apply(DateEntity *date);
34} __attribute__((packed));
35
36class DateEntity : public DateTimeBase {
37 protected:
38 uint16_t year_;
39 uint8_t month_;
40 uint8_t day_;
41
42 public:
43 void publish_state();
45
46 ESPTime state_as_esptime() const override {
47 ESPTime obj;
48 obj.year = this->year_;
49 obj.month = this->month_;
50 obj.day_of_month = this->day_;
51 return obj;
52 }
53
54 const uint16_t &year = year_;
55 const uint8_t &month = month_;
56 const uint8_t &day = day_;
57
58 protected:
59 friend class DateCall;
61
62 virtual void control(const DateCall &call) = 0;
63};
64
65class DateCall {
66 public:
67 explicit DateCall(DateEntity *parent) : parent_(parent) {}
68 void perform();
69 DateCall &set_date(uint16_t year, uint8_t month, uint8_t day);
71 DateCall &set_date(const std::string &date);
72
73 DateCall &set_year(uint16_t year) {
74 this->year_ = year;
75 return *this;
76 }
78 this->month_ = month;
79 return *this;
80 }
81 DateCall &set_day(uint8_t day) {
82 this->day_ = day;
83 return *this;
84 }
85
86 optional<uint16_t> get_year() const { return this->year_; }
87 optional<uint8_t> get_month() const { return this->month_; }
88 optional<uint8_t> get_day() const { return this->day_; }
89
90 protected:
91 void validate_();
92
94
98};
99
100template<typename... Ts> class DateSetAction : public Action<Ts...>, public Parented<DateEntity> {
101 public:
103
104 void play(Ts... x) override {
105 auto call = this->parent_->make_call();
106
107 if (this->date_.has_value()) {
108 call.set_date(this->date_.value(x...));
109 }
110 call.perform();
111 }
112};
113
114} // namespace datetime
115} // namespace esphome
116
117#endif // USE_DATETIME_DATE
virtual void play(Ts... x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
DateCall & set_date(uint16_t year, uint8_t month, uint8_t day)
optional< uint8_t > day_
Definition date_entity.h:97
optional< uint8_t > month_
Definition date_entity.h:96
optional< int16_t > year_
Definition date_entity.h:95
DateCall(DateEntity *parent)
Definition date_entity.h:67
optional< uint8_t > get_month() const
Definition date_entity.h:87
DateCall & set_month(uint8_t month)
Definition date_entity.h:77
optional< uint8_t > get_day() const
Definition date_entity.h:88
DateCall & set_day(uint8_t day)
Definition date_entity.h:81
optional< uint16_t > get_year() const
Definition date_entity.h:86
DateCall & set_year(uint16_t year)
Definition date_entity.h:73
ESPTime state_as_esptime() const override
Definition date_entity.h:46
virtual void control(const DateCall &call)=0
TEMPLATABLE_VALUE(ESPTime, date) void play(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__
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
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