ESPHome 2026.2.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::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 LOG_ENTITY_ICON(TAG, prefix, *(obj)); \
19 }
20
21class DateCall;
22class DateEntity;
23
25 uint16_t year;
26 uint8_t month;
27 uint8_t day;
28
30 void apply(DateEntity *date);
31} __attribute__((packed));
32
33class DateEntity : public DateTimeBase {
34 protected:
35 uint16_t year_;
36 uint8_t month_;
37 uint8_t day_;
38
39 public:
40 void publish_state();
42
43 ESPTime state_as_esptime() const override {
44 ESPTime obj;
45 obj.year = this->year_;
46 obj.month = this->month_;
47 obj.day_of_month = this->day_;
48 return obj;
49 }
50
51 const uint16_t &year = year_;
52 const uint8_t &month = month_;
53 const uint8_t &day = day_;
54
55 protected:
56 friend class DateCall;
58
59 virtual void control(const DateCall &call) = 0;
60};
61
62class DateCall {
63 public:
64 explicit DateCall(DateEntity *parent) : parent_(parent) {}
65 void perform();
66 DateCall &set_date(uint16_t year, uint8_t month, uint8_t day);
68 DateCall &set_date(const char *date, size_t len);
69 DateCall &set_date(const char *date) { return this->set_date(date, strlen(date)); }
70 DateCall &set_date(const std::string &date) { return this->set_date(date.c_str(), date.size()); }
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:1471
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
DateCall & set_date(const char *date)
Definition date_entity.h:69
optional< int16_t > year_
Definition date_entity.h:94
DateCall(DateEntity *parent)
Definition date_entity.h:64
optional< uint8_t > get_month() const
Definition date_entity.h:86
DateCall & set_month(uint8_t month)
Definition date_entity.h:76
DateCall & set_date(const std::string &date)
Definition date_entity.h:70
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:43
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__
std::string size_t len
Definition helpers.h:692
A more user-friendly version of struct tm from time.h.
Definition time.h:17
uint8_t day_of_month
day of the month [1-31]
Definition time.h:32
uint16_t year
year
Definition time.h:38
uint8_t month
month; january=1 [1-12]
Definition time.h:36
uint16_t x
Definition tt21100.cpp:5