ESPHome 2026.8.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 if (this->pin_->is_internal() && this->enable_on_boot_)
27 return setup_priority::IO;
28}
29
30bool PowerSupply::is_enabled() const { return this->active_requests_ != 0; }
31
33 if (this->active_requests_ == 0) {
34 this->cancel_timeout("power-supply-off");
35 ESP_LOGV(TAG, "Enabling");
36 this->pin_->digital_write(true);
37 delay(this->enable_time_);
38 }
39 this->active_requests_++;
40}
41
43 if (this->active_requests_ == 0) {
44 ESP_LOGW(TAG, "Invalid call to unrequest_high_power");
45 return;
46 }
47 this->active_requests_--;
48 if (this->active_requests_ == 0) {
49 this->set_timeout("power-supply-off", this->keep_on_time_, [this]() {
50 ESP_LOGV(TAG, "Disabling");
51 this->pin_->digital_write(false);
52 });
53 }
54}
56 this->active_requests_ = 0;
57 this->pin_->digital_write(false);
58}
59
60} // namespace esphome::power_supply
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual bool is_internal()
Definition gpio.h:81
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 POWER
For power supply components that must be on before buses like i2c can work.
Definition component.h:37
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:41
void HOT delay(uint32_t ms)
Definition hal.cpp:85