ESPHome 2025.6.3
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
4namespace esphome {
5namespace power_supply {
6
7static const char *const TAG = "power_supply";
8
10 ESP_LOGCONFIG(TAG, "Running setup");
11
12 this->pin_->setup();
13 this->pin_->digital_write(false);
14 if (this->enable_on_boot_)
15 this->request_high_power();
16}
18 ESP_LOGCONFIG(TAG, "Power Supply:");
19 LOG_PIN(" Pin: ", this->pin_);
20 ESP_LOGCONFIG(TAG,
21 " Time to enable: %" PRIu32 " ms\n"
22 " Keep on time: %.1f s",
23 this->enable_time_, this->keep_on_time_ / 1000.0f);
24 if (this->enable_on_boot_)
25 ESP_LOGCONFIG(TAG, " Enabled at startup: True");
26}
27
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_LOGD(TAG, "Enabling power supply.");
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_LOGD(TAG, "Disabling power supply.");
51 this->pin_->digital_write(false);
52 });
53 }
54}
56 this->active_requests_ = 0;
57 this->pin_->digital_write(false);
58}
59
60} // namespace power_supply
61} // namespace esphome
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition component.cpp:79
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
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).
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29