ESPHome 2026.11.0b6
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#include "soc/soc_caps.h"
10#endif
11
12#ifdef USE_TIME
14#include "esphome/core/time.h"
15#endif
16
17#include <cinttypes>
18
19namespace esphome::deep_sleep {
20
21#if defined(USE_ESP32) || defined(USE_BK72XX)
22
35#endif
36
37#if defined(USE_BK72XX)
43#endif // USE_BK72XX
44
45#ifdef USE_ESP32
46#if SOC_PM_SUPPORT_EXT1_WAKEUP
47struct Ext1Wakeup {
48 uint64_t mask;
49 esp_sleep_ext1_wakeup_mode_t wakeup_mode;
50};
51#endif
52
54 // Run duration if woken up by timer or any other reason besides those below.
56 // Run duration if woken up by touch pads.
58 // Run duration if woken up by GPIO pins.
60};
61
62#endif // USE_ESP32
63
77
80
83
84#ifdef USE_DEEP_SLEEP_ON_WAKE
85
92inline constexpr float ON_WAKE_TRIGGER_SETUP_PRIORITY = 700.0f;
93
95class WakeTrigger : public Trigger<WakeupCause>, public Component {
96 public:
97 void setup() override {
98 const WakeupCause cause = get_wakeup_cause();
99 if (cause != WAKEUP_CAUSE_NONE) {
100 this->trigger(cause);
101 }
102 }
103 float get_setup_priority() const override { return ON_WAKE_TRIGGER_SETUP_PRIORITY; }
104};
105
106#if defined(USE_ESP32) && SOC_PM_SUPPORT_EXT1_WAKEUP
108class Ext1WakeTrigger : public Trigger<>, public Component {
109 public:
110 explicit Ext1WakeTrigger(uint8_t pin) : pin_(pin) {}
111 void setup() override {
112 if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT1 &&
113 (esp_sleep_get_ext1_wakeup_status() & (1ULL << this->pin_))) {
114 this->trigger();
115 }
116 }
117 float get_setup_priority() const override { return ON_WAKE_TRIGGER_SETUP_PRIORITY; }
118
119 protected:
120 uint8_t pin_;
121};
122#endif
123
124#endif // USE_DEEP_SLEEP_ON_WAKE
125
126template<typename... Ts> class EnterDeepSleepAction;
127
134class DeepSleepComponent final : public Component {
135 public:
137 void set_sleep_duration(uint32_t time_ms) { this->sleep_duration_ = uint64_t(time_ms) * 1000; }
138#if defined(USE_ESP32)
142 void set_wakeup_pin(InternalGPIOPin *pin) { this->wakeup_pin_ = pin; }
143
144 void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode);
145#endif // USE_ESP32
146
147#if defined(USE_BK72XX)
148 void init_wakeup_pins(size_t capacity) { this->wakeup_pins_.init(capacity); }
149 void add_wakeup_pin(InternalGPIOPin *wakeup_pin, WakeupPinMode wakeup_pin_mode) {
150 this->wakeup_pins_.emplace_back(WakeUpPinItem{wakeup_pin, wakeup_pin_mode, !wakeup_pin->is_inverted()});
151 }
152#endif // USE_BK72XX
153
154#if defined(USE_ESP32)
155#if SOC_PM_SUPPORT_EXT1_WAKEUP
156 void set_ext1_wakeup(Ext1Wakeup ext1_wakeup);
157#endif
158
159#if SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
160 void set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
161#endif
162
163 // Set the duration in ms for how long the code should run before entering
164 // deep sleep mode, according to the cause the ESP32 has woken.
165 void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration);
166#endif // USE_ESP32
167
169 void set_run_duration(uint32_t time_ms) { this->run_duration_ = time_ms; }
170
171 void setup() override;
172 void dump_config() override;
173 void loop() override;
174 float get_setup_priority() const override;
175
177 void begin_sleep(bool manual = false);
178
179 void prevent_deep_sleep() { this->prevent_ = true; }
180 void allow_deep_sleep() { this->prevent_ = false; }
181
182 protected:
183 // Returns nullopt if no run duration is set. Otherwise, returns the run
184 // duration before entering deep sleep.
185 optional<uint32_t> get_run_duration_() const;
186
188 bool prepare_to_sleep_();
189 void deep_sleep_();
190 void schedule_sleep_();
191 bool should_teardown_();
192
194 this->next_enter_deep_sleep_ = true;
195 this->enable_loop();
196 }
197
198#ifdef USE_BK72XX
199 bool pin_prevents_sleep_(WakeUpPinItem &pin_item) const;
200 bool get_real_pin_state_(InternalGPIOPin &pin) const { return (pin.digital_read() ^ pin.is_inverted()); }
201#endif // USE_BK72XX
202
203 optional<uint64_t> sleep_duration_;
204
205#ifdef USE_BK72XX
207#endif // USE_BK72XX
208
209#ifdef USE_ESP32
212
213#if SOC_PM_SUPPORT_EXT1_WAKEUP
214 optional<Ext1Wakeup> ext1_wakeup_;
215#endif
216
217 optional<bool> touch_wakeup_;
218
219 optional<WakeupCauseToRunDuration> wakeup_cause_to_run_duration_;
220#endif // USE_ESP32
221
222 optional<uint32_t> run_duration_;
224 bool prevent_{false};
225};
226
227extern bool global_has_deep_sleep; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
228
229template<typename... Ts> class EnterDeepSleepAction final : public Action<Ts...> {
230 public:
232 TEMPLATABLE_VALUE(uint32_t, sleep_duration);
233
234#ifdef USE_TIME
235 void set_until(uint8_t hour, uint8_t minute, uint8_t second) {
236 this->hour_ = hour;
237 this->minute_ = minute;
238 this->second_ = second;
239 }
240
241 void set_time(time::RealTimeClock *time) { this->time_ = time; }
242#endif
243
244 void play(const Ts &...x) override {
245 if (this->sleep_duration_.has_value()) {
246 this->deep_sleep_->set_sleep_duration(this->sleep_duration_.value(x...));
247 }
248#ifdef USE_TIME
249
250 if (this->hour_.has_value()) {
251 auto time = this->time_->now();
252 const uint32_t timestamp_now = time.timestamp;
253
254 bool after_time = false;
255 if (time.hour > this->hour_) {
256 after_time = true;
257 } else {
258 if (time.hour == this->hour_) {
259 if (time.minute > this->minute_) {
260 after_time = true;
261 } else {
262 if (time.minute == this->minute_) {
263 if (time.second > this->second_) {
264 after_time = true;
265 }
266 }
267 }
268 }
269 }
270
271 time.hour = *this->hour_;
272 time.minute = *this->minute_;
273 time.second = *this->second_;
274 time.recalc_timestamp_utc();
275
276 time_t timestamp = time.timestamp; // timestamp in local time zone
277
278 if (after_time)
279 timestamp += 60 * 60 * 24;
280
281 int32_t offset = ESPTime::timezone_offset();
282 timestamp -= offset; // Change timestamp to utc
283 const uint32_t ms_left = (timestamp - timestamp_now) * 1000;
284 this->deep_sleep_->set_sleep_duration(ms_left);
285 }
286#endif
287 this->deep_sleep_->begin_sleep(true);
288 }
289
290 protected:
292#ifdef USE_TIME
293 optional<uint8_t> hour_;
294 optional<uint8_t> minute_;
295 optional<uint8_t> second_;
296
298#endif
299};
300
301} // namespace esphome::deep_sleep
void enable_loop()
Enable this component's loop.
Definition component.h:261
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:669
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:557
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)
void set_run_duration(uint32_t time_ms)
Set a duration in ms for how long the code should run before entering deep sleep mode.
bool pin_prevents_sleep_(WakeUpPinItem &pin_item) const
optional< uint32_t > get_run_duration_() 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)
Fires once on boot when the device was woken from deep sleep by the given ext1 pin.
Fires once on boot when the device woke from deep sleep, with the wakeup cause.
float get_setup_priority() const override
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
bool woke_from_deep_sleep()
Return true when this boot is a wake from deep sleep.
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.
constexpr float ON_WAKE_TRIGGER_SETUP_PRIORITY
Setup priority of on_wake triggers.
WakeupCause get_wakeup_cause()
Return why the device woke from deep sleep. Implemented per platform.
WakeupCause
Why the device woke from deep sleep. Passed to on_wake automations.
@ WAKEUP_CAUSE_TOUCH
The device was woken by a touch pad.
@ WAKEUP_CAUSE_GPIO
The device was woken by a GPIO pin (wakeup_pin or esp32_ext1_wakeup).
@ WAKEUP_CAUSE_TIMER
The device was woken by the sleep timer.
@ WAKEUP_CAUSE_UNKNOWN
The device woke from deep sleep, but the source could not be identified.
@ WAKEUP_CAUSE_NONE
The device did not wake from deep sleep (for example a cold boot, reset or OTA restart).
uint16_t uint16_t & capacity
Definition helpers.cpp:34
static void uint32_t
static int32_t timezone_offset()
Definition time.cpp:351
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