ESPHome 2025.12.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 {
7namespace switch_ {
8
9static const char *const TAG = "switch";
10
11Switch::Switch() : state(false) {}
12
13void Switch::control(bool target_state) {
14 ESP_LOGV(TAG, "'%s' Control: %s", this->get_name().c_str(), ONOFF(target_state));
15 if (target_state) {
16 this->turn_on();
17 } else {
18 this->turn_off();
19 }
20}
22 ESP_LOGD(TAG, "'%s' Turning ON.", this->get_name().c_str());
23 this->write_state(!this->inverted_);
24}
26 ESP_LOGD(TAG, "'%s' Turning OFF.", this->get_name().c_str());
27 this->write_state(this->inverted_);
28}
30 ESP_LOGD(TAG, "'%s' Toggling %s.", this->get_name().c_str(), this->state ? "OFF" : "ON");
31 this->write_state(this->inverted_ == this->state);
32}
35 return {};
36
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_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), ONOFF(this->state));
66 this->state_callback_.call(this->state);
67#if defined(USE_SWITCH) && defined(USE_CONTROLLER_REGISTRY)
69#endif
70}
71bool Switch::assumed_state() { return false; }
72
73void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
74 this->state_callback_.add(std::move(callback));
75}
76void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
77bool Switch::is_inverted() const { return this->inverted_; }
78
79void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) {
80 if (obj != nullptr) {
81 // Prepare restore mode string
82 const LogString *onoff = LOG_STR(""), *inverted = onoff, *restore;
84 restore = LOG_STR("disabled");
85 } else {
86 onoff = obj->restore_mode & RESTORE_MODE_ON_MASK ? LOG_STR("ON") : LOG_STR("OFF");
87 inverted = obj->restore_mode & RESTORE_MODE_INVERTED_MASK ? LOG_STR("inverted ") : LOG_STR("");
88 restore = obj->restore_mode & RESTORE_MODE_PERSISTENT_MASK ? LOG_STR("restore defaults to") : LOG_STR("always");
89 }
90
91 // Build the base message with mandatory fields
92 ESP_LOGCONFIG(tag,
93 "%s%s '%s'\n"
94 "%s Restore Mode: %s%s %s",
95 prefix, type, obj->get_name().c_str(), prefix, LOG_STR_ARG(inverted), LOG_STR_ARG(restore),
96 LOG_STR_ARG(onoff));
97
98 // Add optional fields separately
99 if (!obj->get_icon_ref().empty()) {
100 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
101 }
102 if (obj->assumed_state()) {
103 ESP_LOGCONFIG(tag, "%s Assumed State: YES", prefix);
104 }
105 if (obj->is_inverted()) {
106 ESP_LOGCONFIG(tag, "%s Inverted: YES", prefix);
107 }
108 if (!obj->get_device_class_ref().empty()) {
109 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
110 }
111 }
112}
113
114} // namespace switch_
115} // namespace esphome
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:893
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
StringRef get_device_class_ref() const
Get the device class as StringRef.
const StringRef & get_name() const
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
StringRef get_icon_ref() const
Definition entity_base.h:69
constexpr const char * c_str() const
Definition string_ref.h:69
constexpr bool empty() const
Definition string_ref.h:71
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:29
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:21
void turn_off()
Turn this switch off.
Definition switch.cpp:25
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:73
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
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:76
Deduplicator< bool > publish_dedup_
Definition switch.h:141
bool is_inverted() const
Definition switch.cpp:77
void control(bool target_state)
Control this switch using a boolean state value.
Definition switch.cpp:13
ESPPreferenceObject rtc_
Definition switch.h:135
uint16_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:79
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