ESPHome 2025.5.0
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 {
14namespace datetime {
15
16#define LOG_DATETIME_TIME(prefix, type, obj) \
17 if ((obj) != nullptr) { \
18 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
19 if (!(obj)->get_icon().empty()) { \
20 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
21 } \
22 }
23
24class TimeCall;
25class TimeEntity;
26class OnTimeTrigger;
27
29 uint8_t hour;
30 uint8_t minute;
31 uint8_t second;
32
34 void apply(TimeEntity *time);
35} __attribute__((packed));
36
37class TimeEntity : public DateTimeBase {
38 protected:
39 uint8_t hour_;
40 uint8_t minute_;
41 uint8_t second_;
42
43 public:
44 void publish_state();
46
47 ESPTime state_as_esptime() const override {
48 ESPTime obj;
49 obj.hour = this->hour_;
50 obj.minute = this->minute_;
51 obj.second = this->second_;
52 return obj;
53 }
54
55 const uint8_t &hour = hour_;
56 const uint8_t &minute = minute_;
57 const uint8_t &second = second_;
58
59 protected:
60 friend class TimeCall;
62 friend class OnTimeTrigger;
63
64 virtual void control(const TimeCall &call) = 0;
65};
66
67class TimeCall {
68 public:
69 explicit TimeCall(TimeEntity *parent) : parent_(parent) {}
70 void perform();
71 TimeCall &set_time(uint8_t hour, uint8_t minute, uint8_t second);
73 TimeCall &set_time(const std::string &time);
74
76 this->hour_ = hour;
77 return *this;
78 }
80 this->minute_ = minute;
81 return *this;
82 }
84 this->second_ = second;
85 return *this;
86 }
87
88 optional<uint8_t> get_hour() const { return this->hour_; }
89 optional<uint8_t> get_minute() const { return this->minute_; }
90 optional<uint8_t> get_second() const { return this->second_; }
91
92 protected:
93 void validate_();
94
96
100};
101
102template<typename... Ts> class TimeSetAction : public Action<Ts...>, public Parented<TimeEntity> {
103 public:
105
106 void play(Ts... x) override {
107 auto call = this->parent_->make_call();
108
109 if (this->time_.has_value()) {
110 call.set_time(this->time_.value(x...));
111 }
112 call.perform();
113 }
114};
115
116#ifdef USE_TIME
117class OnTimeTrigger : public Trigger<>, public Component, public Parented<TimeEntity> {
118 public:
119 void loop() override;
120
121 protected:
122 bool matches_(const ESPTime &time) const;
123
125};
126#endif
127
128} // namespace datetime
129} // namespace esphome
130
131#endif // USE_DATETIME_TIME
virtual void play(Ts... x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
bool matches_(const ESPTime &time) const
optional< ESPTime > last_check_
optional< uint8_t > second_
Definition time_entity.h:99
TimeCall & set_hour(uint8_t hour)
Definition time_entity.h:75
optional< uint8_t > hour_
Definition time_entity.h:97
TimeCall & set_minute(uint8_t minute)
Definition time_entity.h:79
TimeCall(TimeEntity *parent)
Definition time_entity.h:69
optional< uint8_t > get_hour() const
Definition time_entity.h:88
optional< uint8_t > minute_
Definition time_entity.h:98
optional< uint8_t > get_second() const
Definition time_entity.h:90
TimeCall & set_second(uint8_t second)
Definition time_entity.h:83
optional< uint8_t > get_minute() const
Definition time_entity.h:89
TimeCall & set_time(uint8_t hour, uint8_t minute, uint8_t second)
ESPTime state_as_esptime() const override
Definition time_entity.h:47
virtual void control(const TimeCall &call)=0
TEMPLATABLE_VALUE(ESPTime, time) void play(Ts... x) override
uint8_t second
uint8_t minute
uint8_t hour
esphome::datetime::DateEntity __attribute__
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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