7static const char *
const TAG =
"switch";
12 ESP_LOGV(TAG,
"'%s' Control: %s", this->
get_name().c_str(), ONOFF(target_state));
20 ESP_LOGD(TAG,
"'%s' Turning ON.", this->
get_name().c_str());
24 ESP_LOGD(TAG,
"'%s' Turning OFF.", this->
get_name().c_str());
28 ESP_LOGD(TAG,
"'%s' Toggling %s.", this->
get_name().c_str(), this->
state ?
"OFF" :
"ON");
37 if (!this->
rtc_.
load(&initial_state))
63 ESP_LOGD(TAG,
"'%s': Sending state %s", this->
name_.
c_str(), ONOFF(this->state));
77 const LogString *onoff = LOG_STR(
""), *inverted = onoff, *restore;
79 restore = LOG_STR(
"disabled");
89 "%s Restore Mode: %s%s %s",
90 prefix,
type, obj->
get_name().
c_str(), prefix, LOG_STR_ARG(inverted), LOG_STR_ARG(restore),
95 ESP_LOGCONFIG(tag,
"%s Icon: '%s'", prefix, obj->
get_icon().c_str());
98 ESP_LOGCONFIG(tag,
"%s Assumed State: YES", prefix);
101 ESP_LOGCONFIG(tag,
"%s Inverted: YES", prefix);
104 ESP_LOGCONFIG(tag,
"%s Device Class: '%s'", prefix, obj->
get_device_class().c_str());
bool next(T value)
Feeds the next item in the series to the deduplicator and returns false if this is a duplicate.
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
value_type const & value() const
Base class for all switches.
void toggle()
Toggle this switch.
optional< bool > get_initial_state()
Returns the initial state of the switch, as persisted previously, or empty if never persisted.
void turn_on()
Turn this switch on.
void turn_off()
Turn this switch off.
bool state
The current reported state of the binary sensor.
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.
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
void publish_state(bool state)
Publish a state to the front-end from the back-end.
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
CallbackManager< void(bool)> state_callback_
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
Deduplicator< bool > publish_dedup_
void control(bool target_state)
Control this switch using a boolean state value.
const int RESTORE_MODE_INVERTED_MASK
const int RESTORE_MODE_ON_MASK
const int RESTORE_MODE_DISABLED_MASK
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj)
const int RESTORE_MODE_PERSISTENT_MASK
Providing packet encoding functions for exchanging data with a remote host.
ESPPreferences * global_preferences