ESPHome 2026.7.4
Loading...
Searching...
No Matches
deep_sleep_component.h
Go to the documentation of this file.
1#pragma once
2
5#include "esphome/core/hal.h"
7#ifdef USE_ESP32
8#include <esp_sleep.h>
9#endif
10
11#ifdef USE_TIME
13#include "esphome/core/time.h"
14#endif
15
16#include <cinttypes>
17
18namespace esphome::deep_sleep {
19
20#if defined(USE_ESP32) || defined(USE_BK72XX)
21
34#endif
35
36#if defined(USE_BK72XX)
42#endif // USE_BK72XX
43
44#ifdef USE_ESP32
45#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
46struct Ext1Wakeup {
47 uint64_t mask;
48 esp_sleep_ext1_wakeup_mode_t wakeup_mode;
49};
50#endif
51
53 // Run duration if woken up by timer or any other reason besides those below.
55 // Run duration if woken up by touch pads.
57 // Run duration if woken up by GPIO pins.
59};
60
61#endif // USE_ESP32
62
63template<typename... Ts> class EnterDeepSleepAction;
64
65template<typename... Ts> class PreventDeepSleepAction;
66
73class DeepSleepComponent final : public Component {
74 public:
76 void set_sleep_duration(uint32_t time_ms);
77#if defined(USE_ESP32)
81 void set_wakeup_pin(InternalGPIOPin *pin) { this->wakeup_pin_ = pin; }
82
83 void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode);
84#endif // USE_ESP32
85
86#if defined(USE_BK72XX)
87 void init_wakeup_pins_(size_t capacity) { this->wakeup_pins_.init(capacity); }
88 void add_wakeup_pin(InternalGPIOPin *wakeup_pin, WakeupPinMode wakeup_pin_mode) {
89 this->wakeup_pins_.emplace_back(WakeUpPinItem{wakeup_pin, wakeup_pin_mode, !wakeup_pin->is_inverted()});
90 }
91#endif // USE_BK72XX
92
93#if defined(USE_ESP32)
94#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
95 void set_ext1_wakeup(Ext1Wakeup ext1_wakeup);
96#endif
97
98#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
99 !defined(USE_ESP32_VARIANT_ESP32C5) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
100 !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
101 void set_touch_wakeup(bool touch_wakeup);
102#endif
103
104 // Set the duration in ms for how long the code should run before entering
105 // deep sleep mode, according to the cause the ESP32 has woken.
106 void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration);
107#endif // USE_ESP32
108
110 void set_run_duration(uint32_t time_ms);
111
112 void setup() override;
113 void dump_config() override;
114 void loop() override;
115 float get_setup_priority() const override;
116
118 void begin_sleep(bool manual = false);
119
120 void prevent_deep_sleep();
121 void allow_deep_sleep();
122
123 protected:
124 // Returns nullopt if no run duration is set. Otherwise, returns the run
125 // duration before entering deep sleep.
126 optional<uint32_t> get_run_duration_() const;
127
129 bool prepare_to_sleep_();
130 void deep_sleep_();
131 void schedule_sleep_();
132 bool should_teardown_();
133
134#ifdef USE_BK72XX
135 bool pin_prevents_sleep_(WakeUpPinItem &pinItem) const;
136 bool get_real_pin_state_(InternalGPIOPin &pin) const { return (pin.digital_read() ^ pin.is_inverted()); }
137#endif // USE_BK72XX
138
139 optional<uint64_t> sleep_duration_;
140
141#ifdef USE_BK72XX
143#endif // USE_BK72XX
144
145#ifdef USE_ESP32
148
149#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
150 optional<Ext1Wakeup> ext1_wakeup_;
151#endif
152
153 optional<bool> touch_wakeup_;
154
155 optional<WakeupCauseToRunDuration> wakeup_cause_to_run_duration_;
156#endif // USE_ESP32
157
158 optional<uint32_t> run_duration_;
160 bool prevent_{false};
161};
162
163extern bool global_has_deep_sleep; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
164
165template<typename... Ts> class EnterDeepSleepAction final : public Action<Ts...> {
166 public:
168 TEMPLATABLE_VALUE(uint32_t, sleep_duration);
169
170#ifdef USE_TIME
171 void set_until(uint8_t hour, uint8_t minute, uint8_t second) {
172 this->hour_ = hour;
173 this->minute_ = minute;
174 this->second_ = second;
175 }
176
177 void set_time(time::RealTimeClock *time) { this->time_ = time; }
178#endif
179
180 void play(const Ts &...x) override {
181 if (this->sleep_duration_.has_value()) {
182 this->deep_sleep_->set_sleep_duration(this->sleep_duration_.value(x...));
183 }
184#ifdef USE_TIME
185
186 if (this->hour_.has_value()) {
187 auto time = this->time_->now();
188 const uint32_t timestamp_now = time.timestamp;
189
190 bool after_time = false;
191 if (time.hour > this->hour_) {
192 after_time = true;
193 } else {
194 if (time.hour == this->hour_) {
195 if (time.minute > this->minute_) {
196 after_time = true;
197 } else {
198 if (time.minute == this->minute_) {
199 if (time.second > this->second_) {
200 after_time = true;
201 }
202 }
203 }
204 }
205 }
206
207 time.hour = *this->hour_;
208 time.minute = *this->minute_;
209 time.second = *this->second_;
210 time.recalc_timestamp_utc();
211
212 time_t timestamp = time.timestamp; // timestamp in local time zone
213
214 if (after_time)
215 timestamp += 60 * 60 * 24;
216
217 int32_t offset = ESPTime::timezone_offset();
218 timestamp -= offset; // Change timestamp to utc
219 const uint32_t ms_left = (timestamp - timestamp_now) * 1000;
220 this->deep_sleep_->set_sleep_duration(ms_left);
221 }
222#endif
223 this->deep_sleep_->begin_sleep(true);
224 }
225
226 protected:
228#ifdef USE_TIME
229 optional<uint8_t> hour_;
230 optional<uint8_t> minute_;
231 optional<uint8_t> second_;
232
234#endif
235};
236
237template<typename... Ts>
238class PreventDeepSleepAction final : public Action<Ts...>, public Parented<DeepSleepComponent> {
239 public:
240 void play(const Ts &...x) override { this->parent_->prevent_deep_sleep(); }
241};
242
243template<typename... Ts> class AllowDeepSleepAction final : public Action<Ts...>, public Parented<DeepSleepComponent> {
244 public:
245 void play(const Ts &...x) override { this->parent_->allow_deep_sleep(); }
246};
247
248} // namespace esphome::deep_sleep
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:534
virtual bool digital_read()=0
virtual bool is_inverted() const =0
Helper class to easily give an object a parent of type T.
Definition helpers.h:1875
This component allows setting up the node to go into deep sleep mode to conserve battery.
void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration)
optional< uint32_t > get_run_duration_() const
bool pin_prevents_sleep_(WakeUpPinItem &pinItem) const
void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode)
bool get_real_pin_state_(InternalGPIOPin &pin) const
void add_wakeup_pin(InternalGPIOPin *wakeup_pin, WakeupPinMode wakeup_pin_mode)
void begin_sleep(bool manual=false)
Helper to enter deep sleep mode.
void set_wakeup_pin(InternalGPIOPin *pin)
Set the pin to wake up to on the ESP32 once it's in deep sleep mode.
void set_sleep_duration(uint32_t time_ms)
Set the duration in ms the component should sleep once it's in deep sleep mode.
void set_ext1_wakeup(Ext1Wakeup ext1_wakeup)
optional< WakeupCauseToRunDuration > wakeup_cause_to_run_duration_
EnterDeepSleepAction(DeepSleepComponent *deep_sleep)
TEMPLATABLE_VALUE(uint32_t, sleep_duration)
void set_until(uint8_t hour, uint8_t minute, uint8_t second)
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
ESPTime now()
Get the time in the currently defined timezone.
uint8_t second
uint8_t minute
uint8_t hour
WakeupPinMode
The values of this enum define what should be done if deep sleep is set up with a wakeup pin on the E...
@ WAKEUP_PIN_MODE_KEEP_AWAKE
As long as the wakeup pin is still in the wakeup state, keep awake.
@ WAKEUP_PIN_MODE_INVERT_WAKEUP
Automatically invert the wakeup level.
@ WAKEUP_PIN_MODE_IGNORE
Ignore the fact that we will wake up when going into deep sleep.
uint16_t uint16_t & capacity
Definition helpers.cpp:25
static void uint32_t
static int32_t timezone_offset()
Definition time.cpp:353
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:48
esp_sleep_ext1_wakeup_mode_t wakeup_mode
uint16_t x
Definition tt21100.cpp:5
uint16_t timestamp
Definition tt21100.cpp:2