ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
power_supply.cpp
Go to the documentation of this file.
1#include "power_supply.h"
2#include "esphome/core/log.h"
3
5
6static const char *const TAG = "power_supply";
7
9 this->pin_->setup();
10 this->pin_->digital_write(false);
11 if (this->enable_on_boot_)
12 this->request_high_power();
13}
15 ESP_LOGCONFIG(TAG,
16 "Power Supply:\n"
17 " Time to enable: %" PRIu32 " ms\n"
18 " Keep on time: %" PRIu32 " s\n"
19 " Enable at startup: %s",
20 this->enable_time_, this->keep_on_time_ / 1000u, YESNO(this->enable_on_boot_));
21 LOG_PIN(" Pin: ", this->pin_);
22}
23
25
26bool PowerSupply::is_enabled() const { return this->active_requests_ != 0; }
27
29 if (this->active_requests_ == 0) {
30 this->cancel_timeout("power-supply-off");
31 ESP_LOGV(TAG, "Enabling");
32 this->pin_->digital_write(true);
33 delay(this->enable_time_);
34 }
35 this->active_requests_++;
36}
37
39 if (this->active_requests_ == 0) {
40 ESP_LOGW(TAG, "Invalid call to unrequest_high_power");
41 return;
42 }
43 this->active_requests_--;
44 if (this->active_requests_ == 0) {
45 this->set_timeout("power-supply-off", this->keep_on_time_, [this]() {
46 ESP_LOGV(TAG, "Disabling");
47 this->pin_->digital_write(false);
48 });
49 }
50}
52 this->active_requests_ = 0;
53 this->pin_->digital_write(false);
54}
55
56} // namespace esphome::power_supply
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_timeout(const std boo cancel_timeout)(const char *name)
Cancel a timeout function.
Definition component.h:515
virtual void setup()=0
virtual void digital_write(bool value)=0
void request_high_power()
Request high power mode. Use unrequest_high_power() to remove this request.
bool is_enabled() const
Is this power supply currently on?
void unrequest_high_power()
Un-request high power mode.
void setup() override
Register callbacks.
float get_setup_priority() const override
Hardware setup priority (+1).
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:39
void HOT delay(uint32_t ms)
Definition hal.cpp:85