ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
switch.cpp
Go to the documentation of this file.
1#include "switch.h"
4#include "esphome/core/log.h"
5
6namespace esphome::switch_ {
7
8static const char *const TAG = "switch";
9
10Switch::Switch() : state(false) {}
11
12void Switch::control(bool target_state) {
13 ESP_LOGV(TAG, "'%s' Control: %s", this->get_name().c_str(), ONOFF(target_state));
14 if (target_state) {
15 this->turn_on();
16 } else {
17 this->turn_off();
18 }
19}
21 ESP_LOGD(TAG, "'%s' Turning ON.", this->get_name().c_str());
22 this->write_state(!this->inverted_);
23}
25 ESP_LOGD(TAG, "'%s' Turning OFF.", this->get_name().c_str());
26 this->write_state(this->inverted_);
27}
29 ESP_LOGD(TAG, "'%s' Toggling %s.", this->get_name().c_str(), this->state ? "OFF" : "ON");
30 this->write_state(this->inverted_ == this->state);
31}
34 return {};
35
36 this->rtc_ = this->make_entity_preference<bool>();
37 bool initial_state;
38 if (!this->rtc_.load(&initial_state))
39 return {};
40 return initial_state;
41}
44 return {};
45 }
46 bool initial_state = restore_mode & RESTORE_MODE_ON_MASK; // default value *_OFF or *_ON
47 if (restore_mode & RESTORE_MODE_PERSISTENT_MASK) { // For RESTORE_*
48 optional<bool> restored_state = this->get_initial_state();
49 if (restored_state.has_value()) {
50 // Invert value if any of the *_INVERTED_* modes
51 initial_state = restore_mode & RESTORE_MODE_INVERTED_MASK ? !restored_state.value() : restored_state.value();
52 }
53 }
54 return initial_state;
55}
57 if (!this->publish_dedup_.next(state))
58 return;
59 this->state = state != this->inverted_;
60
62 this->rtc_.save(&this->state);
63
64 ESP_LOGD(TAG, "'%s' >> %s", this->name_.c_str(), ONOFF(this->state));
65 this->state_callback_.call(this->state);
66#if defined(USE_SWITCH) && defined(USE_CONTROLLER_REGISTRY)
68#endif
69}
70bool Switch::assumed_state() { return false; }
71
72void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
73 this->state_callback_.add(std::move(callback));
74}
75void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
76bool Switch::is_inverted() const { return this->inverted_; }
77
78void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) {
79 if (obj != nullptr) {
80 // Prepare restore mode string
81 const LogString *onoff = LOG_STR(""), *inverted = onoff, *restore;
83 restore = LOG_STR("disabled");
84 } else {
85 onoff = obj->restore_mode & RESTORE_MODE_ON_MASK ? LOG_STR("ON") : LOG_STR("OFF");
86 inverted = obj->restore_mode & RESTORE_MODE_INVERTED_MASK ? LOG_STR("inverted ") : LOG_STR("");
87 restore = obj->restore_mode & RESTORE_MODE_PERSISTENT_MASK ? LOG_STR("restore defaults to") : LOG_STR("always");
88 }
89
90 // Build the base message with mandatory fields
91 ESP_LOGCONFIG(tag,
92 "%s%s '%s'\n"
93 "%s Restore Mode: %s%s %s",
94 prefix, type, obj->get_name().c_str(), prefix, LOG_STR_ARG(inverted), LOG_STR_ARG(restore),
95 LOG_STR_ARG(onoff));
96
97 // Add optional fields separately
98 LOG_ENTITY_ICON(tag, prefix, *obj);
99 if (obj->assumed_state()) {
100 ESP_LOGCONFIG(tag, "%s Assumed State: YES", prefix);
101 }
102 if (obj->is_inverted()) {
103 ESP_LOGCONFIG(tag, "%s Inverted: YES", prefix);
104 }
105 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
106 }
107}
108
109} // namespace esphome::switch_
static void notify_switch_update(switch_::Switch *obj)
bool next(T value)
Feeds the next item in the series to the deduplicator and returns false if this is a duplicate.
Definition helpers.h:1593
bool save(const T *src)
Definition preferences.h:21
const StringRef & get_name() const
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
constexpr const char * c_str() const
Definition string_ref.h:73
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
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_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
bool state
Definition fan.h:2
const char *const TAG
Definition spi.cpp:7
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
constexpr int RESTORE_MODE_PERSISTENT_MASK
Definition switch.h:19
constexpr int RESTORE_MODE_DISABLED_MASK
Definition switch.h:21