ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
binary_output.h
Go to the documentation of this file.
1#pragma once
2
5
6#ifdef USE_POWER_SUPPLY
8#endif
9
10namespace esphome::output {
11
12#define LOG_BINARY_OUTPUT(this) \
13 if (this->inverted_) { \
14 ESP_LOGCONFIG(TAG, " Inverted: YES"); \
15 }
16
18 public:
20 void set_inverted(bool inverted) { this->inverted_ = inverted; }
21
22#ifdef USE_POWER_SUPPLY
29 void set_power_supply(power_supply::PowerSupply *power_supply) { this->power_.set_parent(power_supply); }
30#endif
31
33 virtual void set_state(bool state) {
34 if (state) {
35 this->turn_on();
36 } else {
37 this->turn_off();
38 }
39 }
40
42 virtual void turn_on() {
43#ifdef USE_POWER_SUPPLY
44 this->power_.request();
45#endif
46 this->write_state(!this->inverted_);
47 }
48
50 virtual void turn_off() {
51#ifdef USE_POWER_SUPPLY
52 this->power_.unrequest();
53#endif
54 this->write_state(this->inverted_);
55 }
56
57 // ========== INTERNAL METHODS ==========
58 // (In most use cases you won't need these)
60 bool is_inverted() const { return this->inverted_; }
61
62 protected:
63 virtual void write_state(bool state) = 0;
64
65 bool inverted_{false};
66#ifdef USE_POWER_SUPPLY
68#endif
69};
70
71} // namespace esphome::output
virtual void write_state(bool state)=0
power_supply::PowerSupplyRequester power_
void set_power_supply(power_supply::PowerSupply *power_supply)
Use this to connect up a power supply to this output.
virtual void set_state(bool state)
Enable or disable this binary output.
virtual void turn_off()
Disable this binary output.
virtual void turn_on()
Enable this binary output.
void set_inverted(bool inverted)
Set the inversion state of this binary output.
bool is_inverted() const
Return whether this binary output is inverted.
bool state
Definition fan.h:2