ESPHome 2026.3.0-dev
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#if defined(USE_ESP32) || defined(USE_BK72XX)
23
36#endif
37
38#if defined(USE_BK72XX)
44#endif // USE_BK72XX
45
46#ifdef USE_ESP32
47#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
48struct Ext1Wakeup {
49 uint64_t mask;
50 esp_sleep_ext1_wakeup_mode_t wakeup_mode;
51};
52#endif
53
55 // Run duration if woken up by timer or any other reason besides those below.
56 uint32_t default_cause;
57 // Run duration if woken up by touch pads.
58 uint32_t touch_cause;
59 // Run duration if woken up by GPIO pins.
60 uint32_t gpio_cause;
61};
62
63#endif // USE_ESP32
64
65template<typename... Ts> class EnterDeepSleepAction;
66
67template<typename... Ts> class PreventDeepSleepAction;
68
76 public:
78 void set_sleep_duration(uint32_t time_ms);
79#if defined(USE_ESP32)
83 void set_wakeup_pin(InternalGPIOPin *pin) { this->wakeup_pin_ = pin; }
84
85 void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode);
86#endif // USE_ESP32
87
88#if defined(USE_BK72XX)
89 void init_wakeup_pins_(size_t capacity) { this->wakeup_pins_.init(capacity); }
90 void add_wakeup_pin(InternalGPIOPin *wakeup_pin, WakeupPinMode wakeup_pin_mode) {
91 this->wakeup_pins_.emplace_back(WakeUpPinItem{wakeup_pin, wakeup_pin_mode, !wakeup_pin->is_inverted()});
92 }
93#endif // USE_BK72XX
94
95#if defined(USE_ESP32)
96#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
97 void set_ext1_wakeup(Ext1Wakeup ext1_wakeup);
98#endif
99
100#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
101 !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
102 void set_touch_wakeup(bool touch_wakeup);
103#endif
104
105 // Set the duration in ms for how long the code should run before entering
106 // deep sleep mode, according to the cause the ESP32 has woken.
107 void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration);
108#endif // USE_ESP32
109
111 void set_run_duration(uint32_t time_ms);
112
113 void setup() override;
114 void dump_config() override;
115 void loop() override;
116#ifdef USE_LOOP_PRIORITY
117 float get_loop_priority() const override;
118#endif
119 float get_setup_priority() const override;
120
122 void begin_sleep(bool manual = false);
123
124 void prevent_deep_sleep();
125 void allow_deep_sleep();
126
127 protected:
128 // Returns nullopt if no run duration is set. Otherwise, returns the run
129 // duration before entering deep sleep.
131
133 bool prepare_to_sleep_();
134 void deep_sleep_();
135
136#ifdef USE_BK72XX
137 bool pin_prevents_sleep_(WakeUpPinItem &pinItem) const;
138 bool get_real_pin_state_(InternalGPIOPin &pin) const { return (pin.digital_read() ^ pin.is_inverted()); }
139#endif // USE_BK72XX
140
142
143#ifdef USE_BK72XX
145#endif // USE_BK72XX
146
147#ifdef USE_ESP32
150
151#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
153#endif
154
156
158#endif // USE_ESP32
159
162 bool prevent_{false};
163};
164
165extern bool global_has_deep_sleep; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
166
167template<typename... Ts> class EnterDeepSleepAction : public Action<Ts...> {
168 public:
170 TEMPLATABLE_VALUE(uint32_t, sleep_duration);
171
172#ifdef USE_TIME
173 void set_until(uint8_t hour, uint8_t minute, uint8_t second) {
174 this->hour_ = hour;
175 this->minute_ = minute;
176 this->second_ = second;
177 }
178
179 void set_time(time::RealTimeClock *time) { this->time_ = time; }
180#endif
181
182 void play(const Ts &...x) override {
183 if (this->sleep_duration_.has_value()) {
184 this->deep_sleep_->set_sleep_duration(this->sleep_duration_.value(x...));
185 }
186#ifdef USE_TIME
187
188 if (this->hour_.has_value()) {
189 auto time = this->time_->now();
190 const uint32_t timestamp_now = time.timestamp;
191
192 bool after_time = false;
193 if (time.hour > this->hour_) {
194 after_time = true;
195 } else {
196 if (time.hour == this->hour_) {
197 if (time.minute > this->minute_) {
198 after_time = true;
199 } else {
200 if (time.minute == this->minute_) {
201 if (time.second > this->second_) {
202 after_time = true;
203 }
204 }
205 }
206 }
207 }
208
209 time.hour = *this->hour_;
210 time.minute = *this->minute_;
211 time.second = *this->second_;
212 time.recalc_timestamp_utc();
213
214 time_t timestamp = time.timestamp; // timestamp in local time zone
215
216 if (after_time)
217 timestamp += 60 * 60 * 24;
218
219 int32_t offset = ESPTime::timezone_offset();
220 timestamp -= offset; // Change timestamp to utc
221 const uint32_t ms_left = (timestamp - timestamp_now) * 1000;
222 this->deep_sleep_->set_sleep_duration(ms_left);
223 }
224#endif
225 this->deep_sleep_->begin_sleep(true);
226 }
227
228 protected:
230#ifdef USE_TIME
234
236#endif
237};
238
239template<typename... Ts> class PreventDeepSleepAction : public Action<Ts...>, public Parented<DeepSleepComponent> {
240 public:
241 void play(const Ts &...x) override { this->parent_->prevent_deep_sleep(); }
242};
243
244template<typename... Ts> class AllowDeepSleepAction : public Action<Ts...>, public Parented<DeepSleepComponent> {
245 public:
246 void play(const Ts &...x) override { this->parent_->allow_deep_sleep(); }
247};
248
249} // namespace deep_sleep
250} // namespace esphome
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:299
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:1618
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)
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:307
esp_sleep_ext1_wakeup_mode_t wakeup_mode
uint16_t x
Definition tt21100.cpp:5
uint16_t timestamp
Definition tt21100.cpp:2