ESPHome 2026.10.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_LOGV(TAG, "'%s' Turning ON.", this->get_name().c_str());
22 this->write_state(!this->inverted_);
23}
25 ESP_LOGV(TAG, "'%s' Turning OFF.", this->get_name().c_str());
26 this->write_state(this->inverted_);
27}
29 ESP_LOGV(TAG, "'%s' Toggling %s.", this->get_name().c_str(),
30 this->state ? LOG_STR_LITERAL("OFF") : LOG_STR_LITERAL("ON"));
31 this->write_state(this->inverted_ == this->state);
32}
33optional<bool> Switch::get_initial_state() {
35 return {};
36
37 this->rtc_ = this->make_entity_preference<bool>();
38 bool initial_state;
39 if (!this->rtc_.load(&initial_state))
40 return {};
41 return initial_state;
42}
45 return {};
46 }
47 bool initial_state = restore_mode & RESTORE_MODE_ON_MASK; // default value *_OFF or *_ON
48 if (restore_mode & RESTORE_MODE_PERSISTENT_MASK) { // For RESTORE_*
49 optional<bool> restored_state = this->get_initial_state();
50 if (restored_state.has_value()) {
51 // Invert value if any of the *_INVERTED_* modes
52 initial_state = restore_mode & RESTORE_MODE_INVERTED_MASK ? !restored_state.value() : restored_state.value();
53 }
54 }
55 return initial_state;
56}
58 if (!this->publish_dedup_.next(state))
59 return;
60 this->state = state != this->inverted_;
61
63 this->rtc_.save(&this->state);
64
65 ESP_LOGV(TAG, "'%s' >> %s", this->name_.c_str(), ONOFF(this->state));
66 this->state_callback_.call(this->state);
67#if defined(USE_SWITCH) && defined(USE_CONTROLLER_REGISTRY)
68 ControllerRegistry::notify_switch_update(this);
69#endif
70}
71bool Switch::assumed_state() { return false; }
72
73void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) {
74 if (obj != nullptr) {
75 // Prepare restore mode string
76 const LogString *onoff = LOG_STR(""), *inverted = onoff, *restore;
78 restore = LOG_STR("disabled");
79 } else {
80 onoff = obj->restore_mode & RESTORE_MODE_ON_MASK ? LOG_STR("ON") : LOG_STR("OFF");
81 inverted = obj->restore_mode & RESTORE_MODE_INVERTED_MASK ? LOG_STR("inverted ") : LOG_STR("");
82 restore = obj->restore_mode & RESTORE_MODE_PERSISTENT_MASK ? LOG_STR("restore defaults to") : LOG_STR("always");
83 }
84
85 // Build the base message with mandatory fields
86 ESP_LOGCONFIG(tag,
87 "%s%s '%s'\n"
88 "%s Restore Mode: %s%s %s",
89 prefix, type, obj->get_name().c_str(), prefix, LOG_STR_ARG(inverted), LOG_STR_ARG(restore),
90 LOG_STR_ARG(onoff));
91
92 // Add optional fields separately
93 LOG_ENTITY_ICON(tag, prefix, *obj);
94 if (obj->assumed_state()) {
95 ESP_LOGCONFIG(tag, "%s Assumed State: YES", prefix);
96 }
97 if (obj->is_inverted()) {
98 ESP_LOGCONFIG(tag, "%s Inverted: YES", prefix);
99 }
100 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
101 }
102}
103
104} // namespace esphome::switch_
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:1885
const StringRef & get_name() const
Definition entity_base.h:71
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
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:139
optional< bool > get_initial_state()
Returns the initial state of the switch, as persisted previously, or empty if never persisted.
Definition switch.cpp:33
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 publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:57
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition switch.cpp:71
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:43
Deduplicator< bool > publish_dedup_
Definition switch.h:142
bool is_inverted() const
Definition switch.h:120
void control(bool target_state)
Control this switch using a boolean state value.
Definition switch.cpp:12
ESPPreferenceObject rtc_
Definition switch.h:136
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:73
constexpr int RESTORE_MODE_PERSISTENT_MASK
Definition switch.h:19
constexpr int RESTORE_MODE_DISABLED_MASK
Definition switch.h:21