ESPHome 2025.8.0b1
Loading...
Searching...
No Matches
switch.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace esphome {
9namespace switch_ {
10
11#define SUB_SWITCH(name) \
12 protected: \
13 switch_::Switch *name##_switch_{nullptr}; \
14\
15 public: \
16 void set_##name##_switch(switch_::Switch *s) { this->name##_switch_ = s; }
17
18// bit0: on/off. bit1: persistent. bit2: inverted. bit3: disabled
19const int RESTORE_MODE_ON_MASK = 0x01;
23
33
40 public:
41 explicit Switch();
42
50 void publish_state(bool state);
51
54
56 bool state;
57
65 void control(bool target_state);
70 void turn_on();
75 void turn_off();
80 void toggle();
81
91 void set_inverted(bool inverted);
92
97 void add_on_state_callback(std::function<void(bool)> &&callback);
98
103
111
117 virtual bool assumed_state();
118
119 bool is_inverted() const;
120
122
123 protected:
132 virtual void write_state(bool state) = 0;
133
134 // Pointer first (4 bytes)
136
137 // CallbackManager (12 bytes on 32-bit - contains vector)
139
140 // Small types grouped together
141 Deduplicator<bool> publish_dedup_; // 2 bytes (bool has_value_ + bool last_value_)
142 bool inverted_{false}; // 1 byte
143 // Total: 3 bytes, 1 byte padding
144};
145
146#define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj))
147void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj);
148
149} // namespace switch_
150} // namespace esphome
Helper class to deduplicate items in a series of values.
Definition helpers.h:628
Base class for all switches.
Definition switch.h:39
void toggle()
Toggle this switch.
Definition switch.cpp:27
optional< bool > get_initial_state()
Returns the initial state of the switch, as persisted previously, or empty if never persisted.
Definition switch.cpp:31
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:56
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:53
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition switch.cpp:68
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:55
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition switch.cpp:66
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:41
void set_restore_mode(SwitchRestoreMode restore_mode)
Definition switch.h:121
CallbackManager< void(bool)> state_callback_
Definition switch.h:138
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
Definition switch.cpp:71
Deduplicator< bool > publish_dedup_
Definition switch.h:141
bool is_inverted() const
Definition switch.cpp:72
void control(bool target_state)
Control this switch using a boolean state value.
Definition switch.cpp:11
ESPPreferenceObject rtc_
Definition switch.h:135
uint8_t type
const int RESTORE_MODE_INVERTED_MASK
Definition switch.h:21
const int RESTORE_MODE_ON_MASK
Definition switch.h:19
const int RESTORE_MODE_DISABLED_MASK
Definition switch.h:22
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj)
Definition switch.cpp:74
@ SWITCH_RESTORE_DISABLED
Definition switch.h:31
@ SWITCH_RESTORE_INVERTED_DEFAULT_ON
Definition switch.h:30
@ SWITCH_RESTORE_INVERTED_DEFAULT_OFF
Definition switch.h:29
@ SWITCH_RESTORE_DEFAULT_OFF
Definition switch.h:27
@ SWITCH_RESTORE_DEFAULT_ON
Definition switch.h:28
const int RESTORE_MODE_PERSISTENT_MASK
Definition switch.h:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7