ESPHome 2026.11.0-dev
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 (log_entity_header(TAG, prefix, LOG_STR_LITERAL(type), (obj))) { \
17 LOG_ENTITY_ICON(TAG, prefix, *(obj)); \
18 }
19
20class TimeCall;
21class TimeEntity;
22class OnTimeTrigger;
23
25 uint8_t hour;
26 uint8_t minute;
27 uint8_t second;
28
30 void apply(TimeEntity *time);
31} __attribute__((packed));
32
33class TimeEntity : public DateTimeBase {
34 protected:
35 uint8_t hour_;
36 uint8_t minute_;
37 uint8_t second_;
38
39 public:
40 void publish_state();
42
43 ESPTime state_as_esptime() const override {
44 ESPTime obj;
45 obj.hour = this->hour_;
46 obj.minute = this->minute_;
47 obj.second = this->second_;
48 return obj;
49 }
50
51 const uint8_t &hour = hour_;
52 const uint8_t &minute = minute_;
53 const uint8_t &second = second_;
54
55 protected:
56 friend class TimeCall;
58 friend class OnTimeTrigger;
59
60 virtual void control(const TimeCall &call) = 0;
61};
62
63class TimeCall {
64 public:
65 explicit TimeCall(TimeEntity *parent) : parent_(parent) {}
66 void perform();
67 TimeCall &set_time(uint8_t hour, uint8_t minute, uint8_t second);
69 TimeCall &set_time(const char *time, size_t len);
70 TimeCall &set_time(const char *time) { return this->set_time(time, strlen(time)); }
71 TimeCall &set_time(const std::string &time) { return this->set_time(time.c_str(), time.size()); }
72
74 this->hour_ = hour;
75 return *this;
76 }
78 this->minute_ = minute;
79 return *this;
80 }
82 this->second_ = second;
83 return *this;
84 }
85
86 optional<uint8_t> get_hour() const { return this->hour_; }
87 optional<uint8_t> get_minute() const { return this->minute_; }
88 optional<uint8_t> get_second() const { return this->second_; }
89
90 protected:
91 void validate_();
92
94
95 optional<uint8_t> hour_;
96 optional<uint8_t> minute_;
97 optional<uint8_t> second_;
98};
99
100inline TimeCall TimeEntity::make_call() { return TimeCall(this); }
101
102#ifdef USE_TIME
103class OnTimeTrigger final : public Trigger<>, public Component, public Parented<TimeEntity> {
104 public:
105 void loop() override;
106
107 protected:
108 bool matches_(const ESPTime &time) const;
109
110 optional<ESPTime> last_check_;
111};
112#endif
113
114} // namespace esphome::datetime
115
116#endif // USE_DATETIME_TIME
Helper class to easily give an object a parent of type T.
Definition helpers.h:2073
bool matches_(const ESPTime &time) const
optional< ESPTime > last_check_
optional< uint8_t > second_
Definition time_entity.h:97
TimeCall & set_time(const std::string &time)
Definition time_entity.h:71
TimeCall & set_hour(uint8_t hour)
Definition time_entity.h:73
TimeCall & set_time(const char *time)
Definition time_entity.h:70
optional< uint8_t > hour_
Definition time_entity.h:95
TimeCall & set_minute(uint8_t minute)
Definition time_entity.h:77
TimeCall(TimeEntity *parent)
Definition time_entity.h:65
optional< uint8_t > get_hour() const
Definition time_entity.h:86
optional< uint8_t > minute_
Definition time_entity.h:96
optional< uint8_t > get_second() const
Definition time_entity.h:88
TimeCall & set_second(uint8_t second)
Definition time_entity.h:81
optional< uint8_t > get_minute() const
Definition time_entity.h:87
TimeCall & set_time(uint8_t hour, uint8_t minute, uint8_t second)
ESPTime state_as_esptime() const override
Definition time_entity.h:43
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:68
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