ESPHome 2025.12.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 if (!(obj)->get_icon_ref().empty()) { \
19 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
20 } \
21 }
22
23class TimeCall;
24class TimeEntity;
25class OnTimeTrigger;
26
28 uint8_t hour;
29 uint8_t minute;
30 uint8_t second;
31
33 void apply(TimeEntity *time);
34} __attribute__((packed));
35
36class TimeEntity : public DateTimeBase {
37 protected:
38 uint8_t hour_;
39 uint8_t minute_;
40 uint8_t second_;
41
42 public:
43 void publish_state();
45
46 ESPTime state_as_esptime() const override {
47 ESPTime obj;
48 obj.hour = this->hour_;
49 obj.minute = this->minute_;
50 obj.second = this->second_;
51 return obj;
52 }
53
54 const uint8_t &hour = hour_;
55 const uint8_t &minute = minute_;
56 const uint8_t &second = second_;
57
58 protected:
59 friend class TimeCall;
61 friend class OnTimeTrigger;
62
63 virtual void control(const TimeCall &call) = 0;
64};
65
66class TimeCall {
67 public:
68 explicit TimeCall(TimeEntity *parent) : parent_(parent) {}
69 void perform();
70 TimeCall &set_time(uint8_t hour, uint8_t minute, uint8_t second);
72 TimeCall &set_time(const std::string &time);
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
99};
100
101template<typename... Ts> class TimeSetAction : public Action<Ts...>, public Parented<TimeEntity> {
102 public:
104
105 void play(const Ts &...x) override {
106 auto call = this->parent_->make_call();
107
108 if (this->time_.has_value()) {
109 call.set_time(this->time_.value(x...));
110 }
111 call.perform();
112 }
113};
114
115#ifdef USE_TIME
116class OnTimeTrigger : public Trigger<>, public Component, public Parented<TimeEntity> {
117 public:
118 void loop() override;
119
120 protected:
121 bool matches_(const ESPTime &time) const;
122
124};
125#endif
126
127} // namespace esphome::datetime
128
129#endif // USE_DATETIME_TIME
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:932
bool matches_(const ESPTime &time) const
optional< ESPTime > last_check_
optional< uint8_t > second_
Definition time_entity.h:98
TimeCall & set_hour(uint8_t hour)
Definition time_entity.h:74
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:68
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:46
virtual void control(const TimeCall &call)=0
TEMPLATABLE_VALUE(ESPTime, time) void play(const Ts &...x) override
uint8_t second
uint8_t minute
uint8_t hour
esphome::datetime::DateEntity __attribute__
A more user-friendly version of struct tm from time.h.
Definition time.h:15
uint8_t minute
minutes after the hour [0-59]
Definition time.h:21
uint8_t second
seconds after the minute [0-60]
Definition time.h:19
uint8_t hour
hours since midnight [0-23]
Definition time.h:23
uint16_t x
Definition tt21100.cpp:5