ESPHome 2025.10.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
8#ifdef USE_ESP32
9#include <esp_sleep.h>
10#endif
11
12#ifdef USE_TIME
14#include "esphome/core/time.h"
15#endif
16
17#include <cinttypes>
18
19namespace esphome {
20namespace deep_sleep {
21
22#ifdef USE_ESP32
23
36
37#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
38struct Ext1Wakeup {
39 uint64_t mask;
40 esp_sleep_ext1_wakeup_mode_t wakeup_mode;
41};
42#endif
43
45 // Run duration if woken up by timer or any other reason besides those below.
46 uint32_t default_cause;
47 // Run duration if woken up by touch pads.
48 uint32_t touch_cause;
49 // Run duration if woken up by GPIO pins.
50 uint32_t gpio_cause;
51};
52
53#endif // USE_ESP32
54
55template<typename... Ts> class EnterDeepSleepAction;
56
57template<typename... Ts> class PreventDeepSleepAction;
58
66 public:
68 void set_sleep_duration(uint32_t time_ms);
69#if defined(USE_ESP32)
73 void set_wakeup_pin(InternalGPIOPin *pin) { this->wakeup_pin_ = pin; }
74
75 void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode);
76#endif // USE_ESP32
77
78#if defined(USE_ESP32)
79#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
80 void set_ext1_wakeup(Ext1Wakeup ext1_wakeup);
81#endif
82
83#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
84 !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32H2)
85 void set_touch_wakeup(bool touch_wakeup);
86#endif
87
88 // Set the duration in ms for how long the code should run before entering
89 // deep sleep mode, according to the cause the ESP32 has woken.
90 void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration);
91#endif // USE_ESP32
92
94 void set_run_duration(uint32_t time_ms);
95
96 void setup() override;
97 void dump_config() override;
98 void loop() override;
99 float get_loop_priority() const override;
100 float get_setup_priority() const override;
101
103 void begin_sleep(bool manual = false);
104
105 void prevent_deep_sleep();
106 void allow_deep_sleep();
107
108 protected:
109 // Returns nullopt if no run duration is set. Otherwise, returns the run
110 // duration before entering deep sleep.
112
114 bool prepare_to_sleep_();
115 void deep_sleep_();
116
118#ifdef USE_ESP32
121
122#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
124#endif
125
128#endif // USE_ESP32
131 bool prevent_{false};
132};
133
134extern bool global_has_deep_sleep; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
135
136template<typename... Ts> class EnterDeepSleepAction : public Action<Ts...> {
137 public:
139 TEMPLATABLE_VALUE(uint32_t, sleep_duration);
140
141#ifdef USE_TIME
142 void set_until(uint8_t hour, uint8_t minute, uint8_t second) {
143 this->hour_ = hour;
144 this->minute_ = minute;
145 this->second_ = second;
146 }
147
148 void set_time(time::RealTimeClock *time) { this->time_ = time; }
149#endif
150
151 void play(Ts... x) override {
152 if (this->sleep_duration_.has_value()) {
153 this->deep_sleep_->set_sleep_duration(this->sleep_duration_.value(x...));
154 }
155#ifdef USE_TIME
156
157 if (this->hour_.has_value()) {
158 auto time = this->time_->now();
159 const uint32_t timestamp_now = time.timestamp;
160
161 bool after_time = false;
162 if (time.hour > this->hour_) {
163 after_time = true;
164 } else {
165 if (time.hour == this->hour_) {
166 if (time.minute > this->minute_) {
167 after_time = true;
168 } else {
169 if (time.minute == this->minute_) {
170 if (time.second > this->second_) {
171 after_time = true;
172 }
173 }
174 }
175 }
176 }
177
178 time.hour = *this->hour_;
179 time.minute = *this->minute_;
180 time.second = *this->second_;
181 time.recalc_timestamp_utc();
182
183 time_t timestamp = time.timestamp; // timestamp in local time zone
184
185 if (after_time)
186 timestamp += 60 * 60 * 24;
187
188 int32_t offset = ESPTime::timezone_offset();
189 timestamp -= offset; // Change timestamp to utc
190 const uint32_t ms_left = (timestamp - timestamp_now) * 1000;
191 this->deep_sleep_->set_sleep_duration(ms_left);
192 }
193#endif
194 this->deep_sleep_->begin_sleep(true);
195 }
196
197 protected:
199#ifdef USE_TIME
203
205#endif
206};
207
208template<typename... Ts> class PreventDeepSleepAction : public Action<Ts...>, public Parented<DeepSleepComponent> {
209 public:
210 void play(Ts... x) override { this->parent_->prevent_deep_sleep(); }
211};
212
213template<typename... Ts> class AllowDeepSleepAction : public Action<Ts...>, public Parented<DeepSleepComponent> {
214 public:
215 void play(Ts... x) override { this->parent_->allow_deep_sleep(); }
216};
217
218} // namespace deep_sleep
219} // namespace esphome
Helper class to easily give an object a parent of type T.
Definition helpers.h:712
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
void set_wakeup_pin_mode(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)
bool has_value() const
Definition optional.h:92
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.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static int32_t timezone_offset()
Definition time.cpp:205
esp_sleep_ext1_wakeup_mode_t wakeup_mode
uint16_t x
Definition tt21100.cpp:5
uint16_t timestamp
Definition tt21100.cpp:2