ESPHome 2026.3.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
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 void add_on_state_callback(std::function<void(bool)> &&callback);
97
102
110
116 virtual bool assumed_state();
117
118 bool is_inverted() const;
119
121
122 protected:
131 virtual void write_state(bool state) = 0;
132
133 // Pointer first (4 bytes)
135
136 // LazyCallbackManager (4 bytes on 32-bit - nullptr when empty)
138
139 // Small types grouped together
140 Deduplicator<bool> publish_dedup_; // 2 bytes (bool has_value_ + bool last_value_)
141 bool inverted_{false}; // 1 byte
142 // Total: 3 bytes, 1 byte padding
143};
144
145#define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj))
146void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj);
147
148} // namespace esphome::switch_
Helper class to deduplicate items in a series of values.
Definition helpers.h:1590
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:137
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 add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition switch.cpp:72
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:120
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
Definition switch.cpp:75
Deduplicator< bool > publish_dedup_
Definition switch.h:140
bool is_inverted() const
Definition switch.cpp:76
void control(bool target_state)
Control this switch using a boolean state value.
Definition switch.cpp:12
ESPPreferenceObject rtc_
Definition switch.h:134
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:78
@ 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