ESPHome 2026.5.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(), this->state ? "OFF" : "ON");
30 this->write_state(this->inverted_ == this->state);
31}
32optional<bool> Switch::get_initial_state() {
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_LOGV(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)
67 ControllerRegistry::notify_switch_update(this);
68#endif
69}
70bool Switch::assumed_state() { return false; }
71
72void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
73bool Switch::is_inverted() const { return this->inverted_; }
74
75void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) {
76 if (obj != nullptr) {
77 // Prepare restore mode string
78 const LogString *onoff = LOG_STR(""), *inverted = onoff, *restore;
80 restore = LOG_STR("disabled");
81 } else {
82 onoff = obj->restore_mode & RESTORE_MODE_ON_MASK ? LOG_STR("ON") : LOG_STR("OFF");
83 inverted = obj->restore_mode & RESTORE_MODE_INVERTED_MASK ? LOG_STR("inverted ") : LOG_STR("");
84 restore = obj->restore_mode & RESTORE_MODE_PERSISTENT_MASK ? LOG_STR("restore defaults to") : LOG_STR("always");
85 }
86
87 // Build the base message with mandatory fields
88 ESP_LOGCONFIG(tag,
89 "%s%s '%s'\n"
90 "%s Restore Mode: %s%s %s",
91 prefix, type, obj->get_name().c_str(), prefix, LOG_STR_ARG(inverted), LOG_STR_ARG(restore),
92 LOG_STR_ARG(onoff));
93
94 // Add optional fields separately
95 LOG_ENTITY_ICON(tag, prefix, *obj);
96 if (obj->assumed_state()) {
97 ESP_LOGCONFIG(tag, "%s Assumed State: YES", prefix);
98 }
99 if (obj->is_inverted()) {
100 ESP_LOGCONFIG(tag, "%s Inverted: YES", prefix);
101 }
102 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
103 }
104}
105
106} // 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:1988
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: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 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:72
Deduplicator< bool > publish_dedup_
Definition switch.h:142
bool is_inverted() const
Definition switch.cpp:73
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:75
constexpr int RESTORE_MODE_PERSISTENT_MASK
Definition switch.h:19
constexpr int RESTORE_MODE_DISABLED_MASK
Definition switch.h:21
const char * tag
Definition log.h:74