12 static const char *
const TAG =
"mqtt.switch";
32 ESP_LOGW(TAG,
"'%s': Received unknown status payload: %s", this->
friendly_name().c_str(), payload.c_str());
38 [
this](
bool enabled) { this->
defer(
"send", [
this, enabled]() { this->
publish_state(enabled); }); });
42 LOG_MQTT_COMPONENT(
true,
true);
54 const char *state_s = state ?
"ON" :
"OFF";
bool publish_state(bool state)
Base class for all switches.
MQTTSwitchComponent(switch_::Switch *a_switch)
void status_momentary_warning(const std::string &name, uint32_t length=5000)
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
constexpr const char *const MQTT_OPTIMISTIC
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
bool send_initial_state() override
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Simple Helper struct used for Home Assistant MQTT send_discovery().
void turn_on()
Turn this switch on.
void toggle()
Toggle this switch.
constexpr const char * c_str() const
std::string component_type() const override
"switch" component type.
void dump_config() override
switch_::Switch * switch_
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
Implementation of SPI Controller mode.
const EntityBase * get_entity() const override
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
bool state
The current reported state of the binary sensor.
const StringRef & get_name() const
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
void turn_off()
Turn this switch off.