7 static const char *
const TAG =
"switch";
12 ESP_LOGD(TAG,
"'%s' Turning ON.", this->
get_name().c_str());
16 ESP_LOGD(TAG,
"'%s' Turning OFF.", this->
get_name().c_str());
20 ESP_LOGD(TAG,
"'%s' Toggling %s.", this->
get_name().c_str(), this->
state ?
"OFF" :
"ON");
29 if (!this->
rtc_.
load(&initial_state))
55 ESP_LOGD(TAG,
"'%s': Sending state %s", this->
name_.
c_str(), ONOFF(this->state));
68 ESP_LOGCONFIG(tag,
"%s%s '%s'", prefix, type, obj->
get_name().
c_str());
70 ESP_LOGCONFIG(tag,
"%s Icon: '%s'", prefix, obj->
get_icon().c_str());
73 ESP_LOGCONFIG(tag,
"%s Assumed State: YES", prefix);
76 ESP_LOGCONFIG(tag,
"%s Inverted: YES", prefix);
79 ESP_LOGCONFIG(tag,
"%s Device Class: '%s'", prefix, obj->
get_device_class().c_str());
81 const LogString *onoff = LOG_STR(
""), *inverted = onoff, *restore;
83 restore = LOG_STR(
"disabled");
90 ESP_LOGCONFIG(tag,
"%s Restore Mode: %s%s %s", prefix, LOG_STR_ARG(inverted), LOG_STR_ARG(restore),
Base class for all switches.
value_type const & value() const
CallbackManager< void(bool)> state_callback_
bool next(T value)
Feeds the next item in the series to the deduplicator and returns whether this is a duplicate...
std::string get_device_class()
Get the device class, using the manual override if set.
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj)
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
virtual void write_state(bool state)=0
Write the given state to hardware.
Deduplicator< bool > publish_dedup_
std::string get_icon() const
optional< bool > get_initial_state()
Returns the initial state of the switch, as persisted previously, or empty if never persisted...
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
ESPPreferences * global_preferences
const int RESTORE_MODE_ON_MASK
void turn_on()
Turn this switch on.
SwitchRestoreMode restore_mode
Indicates whether or not state is to be retrieved from flash and how.
void toggle()
Toggle this switch.
const int RESTORE_MODE_INVERTED_MASK
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
constexpr const char * c_str() const
const int RESTORE_MODE_PERSISTENT_MASK
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
Implementation of SPI Controller mode.
void publish_state(bool state)
Publish a state to the front-end from the back-end.
const int RESTORE_MODE_DISABLED_MASK
bool state
The current reported state of the binary sensor.
uint32_t get_object_id_hash()
const StringRef & get_name() const
void turn_off()
Turn this switch off.