ESPHome 2025.6.3
Loading...
Searching...
No Matches
deep_sleep_component.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace deep_sleep {
7
8static const char *const TAG = "deep_sleep";
9// 5 seconds for deep sleep to ensure clean disconnect from Home Assistant
10static const uint32_t TEARDOWN_TIMEOUT_DEEP_SLEEP_MS = 5000;
11
12bool global_has_deep_sleep = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
13
15 ESP_LOGCONFIG(TAG, "Running setup");
17
18 const optional<uint32_t> run_duration = get_run_duration_();
19 if (run_duration.has_value()) {
20 ESP_LOGI(TAG, "Scheduling in %" PRIu32 " ms", *run_duration);
21 this->set_timeout(*run_duration, [this]() { this->begin_sleep(); });
22 } else {
23 ESP_LOGD(TAG, "Not scheduling; no run duration configured");
24 }
25}
26
28 ESP_LOGCONFIG(TAG, "Deep sleep:");
29 if (this->sleep_duration_.has_value()) {
30 uint32_t duration = *this->sleep_duration_ / 1000;
31 ESP_LOGCONFIG(TAG, " Sleep Duration: %" PRIu32 " ms", duration);
32 }
33 if (this->run_duration_.has_value()) {
34 ESP_LOGCONFIG(TAG, " Run Duration: %" PRIu32 " ms", *this->run_duration_);
35 }
37}
38
40 if (this->next_enter_deep_sleep_)
41 this->begin_sleep();
42}
43
45 return -100.0f; // run after everything else is ready
46}
47
48void DeepSleepComponent::set_sleep_duration(uint32_t time_ms) { this->sleep_duration_ = uint64_t(time_ms) * 1000; }
49
50void DeepSleepComponent::set_run_duration(uint32_t time_ms) { this->run_duration_ = time_ms; }
51
53 if (this->prevent_ && !manual) {
54 this->next_enter_deep_sleep_ = true;
55 return;
56 }
57
58 if (!this->prepare_to_sleep_()) {
59 return;
60 }
61
62 ESP_LOGI(TAG, "Beginning sleep");
63 if (this->sleep_duration_.has_value()) {
64 ESP_LOGI(TAG, "Sleeping for %" PRId64 "us", *this->sleep_duration_);
65 }
67 // It's critical to teardown components cleanly for deep sleep to ensure
68 // Home Assistant sees a clean disconnect instead of marking the device unavailable
69 App.teardown_components(TEARDOWN_TIMEOUT_DEEP_SLEEP_MS);
71
72 this->deep_sleep_();
73}
74
76
78
80
81} // namespace deep_sleep
82} // namespace esphome
void teardown_components(uint32_t timeout_ms)
Teardown all components with a timeout.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.cpp:75
void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration)
optional< uint32_t > get_run_duration_() const
void begin_sleep(bool manual=false)
Helper to enter 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.
bool has_value() const
Definition optional.h:87
uint8_t duration
Definition msa3xx.h:0
const float LATE
For components that should be initialized at the very end of the setup process.
Definition component.cpp:29
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Application App
Global storage of Application pointer - only one Application can exist.