ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
switch.cpp
Go to the documentation of this file.
1#include "switch.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace switch_ {
6
7static const char *const TAG = "switch";
8
9Switch::Switch() : state(false) {}
10
11void Switch::control(bool target_state) {
12 ESP_LOGV(TAG, "'%s' Control: %s", this->get_name().c_str(), ONOFF(target_state));
13 if (target_state) {
14 this->turn_on();
15 } else {
16 this->turn_off();
17 }
18}
20 ESP_LOGD(TAG, "'%s' Turning ON.", this->get_name().c_str());
21 this->write_state(!this->inverted_);
22}
24 ESP_LOGD(TAG, "'%s' Turning OFF.", this->get_name().c_str());
25 this->write_state(this->inverted_);
26}
28 ESP_LOGD(TAG, "'%s' Toggling %s.", this->get_name().c_str(), this->state ? "OFF" : "ON");
29 this->write_state(this->inverted_ == this->state);
30}
33 return {};
34
36 bool initial_state;
37 if (!this->rtc_.load(&initial_state))
38 return {};
39 return initial_state;
40}
43 return {};
44 }
45 bool initial_state = restore_mode & RESTORE_MODE_ON_MASK; // default value *_OFF or *_ON
46 if (restore_mode & RESTORE_MODE_PERSISTENT_MASK) { // For RESTORE_*
47 optional<bool> restored_state = this->get_initial_state();
48 if (restored_state.has_value()) {
49 // Invert value if any of the *_INVERTED_* modes
50 initial_state = restore_mode & RESTORE_MODE_INVERTED_MASK ? !restored_state.value() : restored_state.value();
51 }
52 }
53 return initial_state;
54}
56 if (!this->publish_dedup_.next(state))
57 return;
58 this->state = state != this->inverted_;
59
61 this->rtc_.save(&this->state);
62
63 ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), ONOFF(this->state));
64 this->state_callback_.call(this->state);
65}
66bool Switch::assumed_state() { return false; }
67
68void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
69 this->state_callback_.add(std::move(callback));
70}
71void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
72bool Switch::is_inverted() const { return this->inverted_; }
73
74void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) {
75 if (obj != nullptr) {
76 // Prepare restore mode string
77 const LogString *onoff = LOG_STR(""), *inverted = onoff, *restore;
79 restore = LOG_STR("disabled");
80 } else {
81 onoff = obj->restore_mode & RESTORE_MODE_ON_MASK ? LOG_STR("ON") : LOG_STR("OFF");
82 inverted = obj->restore_mode & RESTORE_MODE_INVERTED_MASK ? LOG_STR("inverted ") : LOG_STR("");
83 restore = obj->restore_mode & RESTORE_MODE_PERSISTENT_MASK ? LOG_STR("restore defaults to") : LOG_STR("always");
84 }
85
86 // Build the base message with mandatory fields
87 ESP_LOGCONFIG(tag,
88 "%s%s '%s'\n"
89 "%s Restore Mode: %s%s %s",
90 prefix, type, obj->get_name().c_str(), prefix, LOG_STR_ARG(inverted), LOG_STR_ARG(restore),
91 LOG_STR_ARG(onoff));
92
93 // Add optional fields separately
94 if (!obj->get_icon().empty()) {
95 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon().c_str());
96 }
97 if (obj->assumed_state()) {
98 ESP_LOGCONFIG(tag, "%s Assumed State: YES", prefix);
99 }
100 if (obj->is_inverted()) {
101 ESP_LOGCONFIG(tag, "%s Inverted: YES", prefix);
102 }
103 if (!obj->get_device_class().empty()) {
104 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class().c_str());
105 }
106 }
107}
108
109} // namespace switch_
110} // namespace esphome
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:631
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
std::string get_device_class()
Get the device class, using the manual override if set.
uint32_t get_object_id_hash()
const StringRef & get_name() const
std::string get_icon() const
constexpr const char * c_str() const
Definition string_ref.h:69
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: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
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
bool state
Definition fan.h:0
const char *const TAG
Definition spi.cpp:8
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
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
ESPPreferences * global_preferences