ESPHome 2026.11.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 (log_entity_header(TAG, prefix, LOG_STR_LITERAL(type), (obj))) { \
17 LOG_ENTITY_ICON(TAG, prefix, *(obj)); \
18 }
19
20class DateCall;
21class DateEntity;
22
24 uint16_t year;
25 uint8_t month;
26 uint8_t day;
27
29 void apply(DateEntity *date);
30} __attribute__((packed));
31
32class DateEntity : public DateTimeBase {
33 protected:
34 uint16_t year_;
35 uint8_t month_;
36 uint8_t day_;
37
38 public:
39 void publish_state();
41
42 ESPTime state_as_esptime() const override {
43 ESPTime obj;
44 obj.year = this->year_;
45 obj.month = this->month_;
46 obj.day_of_month = this->day_;
47 return obj;
48 }
49
50 const uint16_t &year = year_;
51 const uint8_t &month = month_;
52 const uint8_t &day = day_;
53
54 protected:
55 friend class DateCall;
57
58 virtual void control(const DateCall &call) = 0;
59};
60
61class DateCall {
62 public:
63 explicit DateCall(DateEntity *parent) : parent_(parent) {}
64 void perform();
65 DateCall &set_date(uint16_t year, uint8_t month, uint8_t day);
67 DateCall &set_date(const char *date, size_t len);
68 DateCall &set_date(const char *date) { return this->set_date(date, strlen(date)); }
69 DateCall &set_date(const std::string &date) { return this->set_date(date.c_str(), date.size()); }
70
71 DateCall &set_year(uint16_t year) {
72 this->year_ = year;
73 return *this;
74 }
76 this->month_ = month;
77 return *this;
78 }
79 DateCall &set_day(uint8_t day) {
80 this->day_ = day;
81 return *this;
82 }
83
84 optional<uint16_t> get_year() const { return this->year_; }
85 optional<uint8_t> get_month() const { return this->month_; }
86 optional<uint8_t> get_day() const { return this->day_; }
87
88 protected:
89 void validate_();
90
92
93 optional<uint16_t> year_;
94 optional<uint8_t> month_;
95 optional<uint8_t> day_;
96};
97
98inline DateCall DateEntity::make_call() { return DateCall(this); }
99
100} // namespace esphome::datetime
101
102#endif // USE_DATETIME_DATE
DateCall & set_date(uint16_t year, uint8_t month, uint8_t day)
optional< uint8_t > day_
Definition date_entity.h:95
optional< uint16_t > year_
Definition date_entity.h:93
optional< uint8_t > month_
Definition date_entity.h:94
DateCall & set_date(const char *date)
Definition date_entity.h:68
DateCall(DateEntity *parent)
Definition date_entity.h:63
optional< uint8_t > get_month() const
Definition date_entity.h:85
DateCall & set_month(uint8_t month)
Definition date_entity.h:75
DateCall & set_date(const std::string &date)
Definition date_entity.h:69
optional< uint8_t > get_day() const
Definition date_entity.h:86
DateCall & set_day(uint8_t day)
Definition date_entity.h:79
optional< uint16_t > get_year() const
Definition date_entity.h:84
DateCall & set_year(uint16_t year)
Definition date_entity.h:71
ESPTime state_as_esptime() const override
Definition date_entity.h:42
virtual void control(const DateCall &call)=0
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__
const void size_t len
Definition hal.h:68
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42