ESPHome 2026.11.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.
18// bit4: restore from flash. bit5: restore from RTC memory (neither: platform default).
19// Code generation ors the storage bit into the restore mode passed to set_restore_mode().
20constexpr int RESTORE_MODE_ON_MASK = 0x01;
21constexpr int RESTORE_MODE_PERSISTENT_MASK = 0x02;
22constexpr int RESTORE_MODE_INVERTED_MASK = 0x04;
23constexpr int RESTORE_MODE_DISABLED_MASK = 0x08;
25constexpr int RESTORE_MODE_STORAGE_RTC_MASK = 0x20;
27
37
43class Switch : public EntityBase {
44 public:
45 explicit Switch();
46
54 void publish_state(bool state);
55
58
60 bool state;
61
69 void control(bool target_state);
74 void turn_on();
79 void turn_off();
84 void toggle();
85
95 void set_inverted(bool inverted) { this->inverted_ = inverted; }
96
101 template<typename F> void add_on_state_callback(F &&callback) {
102 this->state_callback_.add(std::forward<F>(callback));
103 }
104
108 optional<bool> get_initial_state();
109
117
120 void restore();
121 void restore(bool when_disabled);
122
128 virtual bool assumed_state();
129
130 bool is_inverted() const { return this->inverted_; }
131
133
134 protected:
143 virtual void write_state(bool state) = 0;
144
145 // Pointer first (4 bytes)
147
148 // LazyCallbackManager (4 bytes on 32-bit - nullptr when empty)
150
151 // Small types grouped together
152 Deduplicator<bool> publish_dedup_; // 3 bytes (bool has_value_ + bool value_unknown_ + bool last_value_)
153 bool inverted_{false}; // 1 byte
154 // Total: 4 bytes, no padding
155};
156
157#define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj))
158void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj);
159
161template<auto Fn> class ApplySwitch final : public Switch {
162 public:
163 // Not "= default": placement new would zero-fill .bss that is already zero.
165
166 protected:
167 void write_state(bool state) override { Fn(this, state); }
168};
169
170} // namespace esphome::switch_
Helper class to deduplicate items in a series of values.
Definition helpers.h:2045
Calls the codegen-generated Fn(this, state) on a state write; the hub and any publish are baked into ...
Definition switch.h:161
void write_state(bool state) override
Definition switch.h:167
Base class for all switches.
Definition switch.h:43
void toggle()
Toggle this switch.
Definition switch.cpp:27
LazyCallbackManager< void(bool)> state_callback_
Definition switch.h:149
void add_on_state_callback(F &&callback)
Set callback for state changes.
Definition switch.h:101
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:19
void turn_off()
Turn this switch off.
Definition switch.cpp:23
bool state
The current reported state of the binary sensor.
Definition switch.h:60
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:57
void restore()
Drive the switch to the state its restore mode yields, through control(), so inversion and write_stat...
Definition switch.cpp:63
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:70
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition switch.cpp:85
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:49
void set_restore_mode(SwitchRestoreMode restore_mode)
Definition switch.h:132
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
Definition switch.h:95
Deduplicator< bool > publish_dedup_
Definition switch.h:152
bool is_inverted() const
Definition switch.h:130
void control(bool target_state)
Control this switch using a boolean state value.
Definition switch.cpp:12
ESPPreferenceObject rtc_
Definition switch.h:146
uint8_t type
constexpr int RESTORE_MODE_STORAGE_RTC_MASK
Definition switch.h:25
constexpr int RESTORE_MODE_ON_MASK
Definition switch.h:20
constexpr int RESTORE_MODE_STORAGE_MASK
Definition switch.h:26
constexpr int RESTORE_MODE_INVERTED_MASK
Definition switch.h:22
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj)
Definition switch.cpp:87
@ SWITCH_RESTORE_DISABLED
Definition switch.h:35
@ SWITCH_RESTORE_INVERTED_DEFAULT_ON
Definition switch.h:34
@ SWITCH_RESTORE_INVERTED_DEFAULT_OFF
Definition switch.h:33
@ SWITCH_RESTORE_DEFAULT_OFF
Definition switch.h:31
@ SWITCH_RESTORE_DEFAULT_ON
Definition switch.h:32
constexpr int RESTORE_MODE_STORAGE_FLASH_MASK
Definition switch.h:24
constexpr int RESTORE_MODE_PERSISTENT_MASK
Definition switch.h:21
constexpr int RESTORE_MODE_DISABLED_MASK
Definition switch.h:23