ESPHome 2025.5.0
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
53 bool state;
54
57
62 void turn_on();
67 void turn_off();
72 void toggle();
73
83 void set_inverted(bool inverted);
84
89 void add_on_state_callback(std::function<void(bool)> &&callback);
90
95
103
109 virtual bool assumed_state();
110
111 bool is_inverted() const;
112
114
115 protected:
124 virtual void write_state(bool state) = 0;
125
127 bool inverted_{false};
130};
131
132#define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj))
133void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj);
134
135} // namespace switch_
136} // namespace esphome
Helper class to deduplicate items in a series of values.
Definition helpers.h:517
Base class for all switches.
Definition switch.h:39
void toggle()
Toggle this switch.
Definition switch.cpp:19
optional< bool > get_initial_state()
Returns the initial state of the switch, as persisted previously, or empty if never persisted.
Definition switch.cpp:23
void turn_on()
Turn this switch on.
Definition switch.cpp:11
void turn_off()
Turn this switch off.
Definition switch.cpp:15
bool state
The current reported state of the binary sensor.
Definition switch.h:53
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:56
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition switch.cpp:60
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:47
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition switch.cpp:58
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:33
void set_restore_mode(SwitchRestoreMode restore_mode)
Definition switch.h:113
CallbackManager< void(bool)> state_callback_
Definition switch.h:126
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
Definition switch.cpp:63
Deduplicator< bool > publish_dedup_
Definition switch.h:128
bool is_inverted() const
Definition switch.cpp:64
ESPPreferenceObject rtc_
Definition switch.h:129
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:66
const int RESTORE_MODE_PERSISTENT_MASK
Definition switch.h:20
@ 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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7