ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
switch.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace esphome::switch_ {
9
10#define SUB_SWITCH(name) \
11 protected: \
12 switch_::Switch *name##_switch_{nullptr}; \
13\
14 public: \
15 void set_##name##_switch(switch_::Switch *s) { this->name##_switch_ = s; }
16
17// bit0: on/off. bit1: persistent. bit2: inverted. bit3: disabled
18constexpr int RESTORE_MODE_ON_MASK = 0x01;
19constexpr int RESTORE_MODE_PERSISTENT_MASK = 0x02;
20constexpr int RESTORE_MODE_INVERTED_MASK = 0x04;
21constexpr int RESTORE_MODE_DISABLED_MASK = 0x08;
22
32
38class Switch : public EntityBase {
39 public:
40 explicit Switch();
41
49 void publish_state(bool state);
50
53
55 bool state;
56
64 void control(bool target_state);
69 void turn_on();
74 void turn_off();
79 void toggle();
80
90 void set_inverted(bool inverted);
91
96 template<typename F> void add_on_state_callback(F &&callback) {
97 this->state_callback_.add(std::forward<F>(callback));
98 }
99
103 optional<bool> get_initial_state();
104
112
118 virtual bool assumed_state();
119
120 bool is_inverted() const;
121
123
124 protected:
133 virtual void write_state(bool state) = 0;
134
135 // Pointer first (4 bytes)
137
138 // LazyCallbackManager (4 bytes on 32-bit - nullptr when empty)
140
141 // Small types grouped together
142 Deduplicator<bool> publish_dedup_; // 2 bytes (bool has_value_ + bool last_value_)
143 bool inverted_{false}; // 1 byte
144 // Total: 3 bytes, 1 byte padding
145};
146
147#define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj))
148void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj);
149
150} // namespace esphome::switch_
Helper class to deduplicate items in a series of values.
Definition helpers.h:1985
Base class for all switches.
Definition switch.h:38
void toggle()
Toggle this switch.
Definition switch.cpp:28
LazyCallbackManager< void(bool)> state_callback_
Definition switch.h:139
void add_on_state_callback(F &&callback)
Set callback for state changes.
Definition switch.h:96
optional< bool > get_initial_state()
Returns the initial state of the switch, as persisted previously, or empty if never persisted.
Definition switch.cpp:32
void turn_on()
Turn this switch on.
Definition switch.cpp:20
void turn_off()
Turn this switch off.
Definition switch.cpp:24
bool state
The current reported state of the binary sensor.
Definition switch.h:55
virtual void write_state(bool state)=0
Write the given state to hardware.
SwitchRestoreMode restore_mode
Indicates whether or not state is to be retrieved from flash and how.
Definition switch.h:52
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:56
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition switch.cpp:70
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:42
void set_restore_mode(SwitchRestoreMode restore_mode)
Definition switch.h:122
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
Definition switch.cpp:72
Deduplicator< bool > publish_dedup_
Definition switch.h:142
bool is_inverted() const
Definition switch.cpp:73
void control(bool target_state)
Control this switch using a boolean state value.
Definition switch.cpp:12
ESPPreferenceObject rtc_
Definition switch.h:136
uint16_t type
constexpr int RESTORE_MODE_ON_MASK
Definition switch.h:18
constexpr int RESTORE_MODE_INVERTED_MASK
Definition switch.h:20
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj)
Definition switch.cpp:75
@ SWITCH_RESTORE_DISABLED
Definition switch.h:30
@ SWITCH_RESTORE_INVERTED_DEFAULT_ON
Definition switch.h:29
@ SWITCH_RESTORE_INVERTED_DEFAULT_OFF
Definition switch.h:28
@ SWITCH_RESTORE_DEFAULT_OFF
Definition switch.h:26
@ SWITCH_RESTORE_DEFAULT_ON
Definition switch.h:27
constexpr int RESTORE_MODE_PERSISTENT_MASK
Definition switch.h:19
constexpr int RESTORE_MODE_DISABLED_MASK
Definition switch.h:21
const char * tag
Definition log.h:74