ESPHome 2026.10.2
Loading...
Searching...
No Matches
time_entity.h
Go to the documentation of this file.
1#pragma once
5#ifdef USE_DATETIME_TIME
6
9#include "esphome/core/time.h"
10
11#include "datetime_base.h"
12
13namespace esphome::datetime {
14
15#define LOG_DATETIME_TIME(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 TimeCall;
22class TimeEntity;
23class OnTimeTrigger;
24
26 uint8_t hour;
27 uint8_t minute;
28 uint8_t second;
29
31 void apply(TimeEntity *time);
32} __attribute__((packed));
33
34class TimeEntity : public DateTimeBase {
35 protected:
36 uint8_t hour_;
37 uint8_t minute_;
38 uint8_t second_;
39
40 public:
41 void publish_state();
43
44 ESPTime state_as_esptime() const override {
45 ESPTime obj;
46 obj.hour = this->hour_;
47 obj.minute = this->minute_;
48 obj.second = this->second_;
49 return obj;
50 }
51
52 const uint8_t &hour = hour_;
53 const uint8_t &minute = minute_;
54 const uint8_t &second = second_;
55
56 protected:
57 friend class TimeCall;
59 friend class OnTimeTrigger;
60
61 virtual void control(const TimeCall &call) = 0;
62};
63
64class TimeCall {
65 public:
66 explicit TimeCall(TimeEntity *parent) : parent_(parent) {}
67 void perform();
68 TimeCall &set_time(uint8_t hour, uint8_t minute, uint8_t second);
70 TimeCall &set_time(const char *time, size_t len);
71 TimeCall &set_time(const char *time) { return this->set_time(time, strlen(time)); }
72 TimeCall &set_time(const std::string &time) { return this->set_time(time.c_str(), time.size()); }
73
75 this->hour_ = hour;
76 return *this;
77 }
79 this->minute_ = minute;
80 return *this;
81 }
83 this->second_ = second;
84 return *this;
85 }
86
87 optional<uint8_t> get_hour() const { return this->hour_; }
88 optional<uint8_t> get_minute() const { return this->minute_; }
89 optional<uint8_t> get_second() const { return this->second_; }
90
91 protected:
92 void validate_();
93
95
96 optional<uint8_t> hour_;
97 optional<uint8_t> minute_;
98 optional<uint8_t> second_;
99};
100
101inline TimeCall TimeEntity::make_call() { return TimeCall(this); }
102
103#ifdef USE_TIME
104class OnTimeTrigger final : public Trigger<>, public Component, public Parented<TimeEntity> {
105 public:
106 void loop() override;
107
108 protected:
109 bool matches_(const ESPTime &time) const;
110
111 optional<ESPTime> last_check_;
112};
113#endif
114
115} // namespace esphome::datetime
116
117#endif // USE_DATETIME_TIME
Helper class to easily give an object a parent of type T.
Definition helpers.h:2004
bool matches_(const ESPTime &time) const
optional< ESPTime > last_check_
optional< uint8_t > second_
Definition time_entity.h:98
TimeCall & set_time(const std::string &time)
Definition time_entity.h:72
TimeCall & set_hour(uint8_t hour)
Definition time_entity.h:74
TimeCall & set_time(const char *time)
Definition time_entity.h:71
optional< uint8_t > hour_
Definition time_entity.h:96
TimeCall & set_minute(uint8_t minute)
Definition time_entity.h:78
TimeCall(TimeEntity *parent)
Definition time_entity.h:66
optional< uint8_t > get_hour() const
Definition time_entity.h:87
optional< uint8_t > minute_
Definition time_entity.h:97
optional< uint8_t > get_second() const
Definition time_entity.h:89
TimeCall & set_second(uint8_t second)
Definition time_entity.h:82
optional< uint8_t > get_minute() const
Definition time_entity.h:88
TimeCall & set_time(uint8_t hour, uint8_t minute, uint8_t second)
ESPTime state_as_esptime() const override
Definition time_entity.h:44
virtual void control(const TimeCall &call)=0
uint8_t second
uint8_t minute
uint8_t hour
esphome::datetime::DateEntity __attribute__
const void size_t len
Definition hal.h:64
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
uint8_t hour
hours since midnight [0-23]
Definition time.h:34