ESPHome 2025.5.0
Loading...
Searching...
No Matches
mqtt_switch.cpp
Go to the documentation of this file.
1#include "mqtt_switch.h"
2#include "esphome/core/log.h"
3
4#include "mqtt_const.h"
5
6#ifdef USE_MQTT
7#ifdef USE_SWITCH
8
9namespace esphome {
10namespace mqtt {
11
12static const char *const TAG = "mqtt.switch";
13
14using namespace esphome::switch_;
15
17
19 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
20 switch (parse_on_off(payload.c_str())) {
21 case PARSE_ON:
22 this->switch_->turn_on();
23 break;
24 case PARSE_OFF:
25 this->switch_->turn_off();
26 break;
27 case PARSE_TOGGLE:
28 this->switch_->toggle();
29 break;
30 case PARSE_NONE:
31 default:
32 ESP_LOGW(TAG, "'%s': Received unknown status payload: %s", this->friendly_name().c_str(), payload.c_str());
33 this->status_momentary_warning("state", 5000);
34 break;
35 }
36 });
38 [this](bool enabled) { this->defer("send", [this, enabled]() { this->publish_state(enabled); }); });
39}
41 ESP_LOGCONFIG(TAG, "MQTT Switch '%s': ", this->switch_->get_name().c_str());
42 LOG_MQTT_COMPONENT(true, true);
43}
44
45std::string MQTTSwitchComponent::component_type() const { return "switch"; }
46const EntityBase *MQTTSwitchComponent::get_entity() const { return this->switch_; }
48 if (this->switch_->assumed_state())
49 root[MQTT_OPTIMISTIC] = true;
50}
52
54 const char *state_s = state ? "ON" : "OFF";
55 return this->publish(this->get_state_topic_(), state_s);
56}
57
58} // namespace mqtt
59} // namespace esphome
60
61#endif
62#endif // USE_MQTT
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.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
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.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
std::string component_type() const override
"switch" component type.
const EntityBase * get_entity() const override
MQTTSwitchComponent(switch_::Switch *a_switch)
Base class for all switches.
Definition switch.h:39
void toggle()
Toggle this switch.
Definition switch.cpp:19
void turn_on()
Turn this switch on.
Definition switch.cpp:11
void turn_off()
Turn this switch off.
Definition switch.cpp:15
bool state
The current reported state of the binary sensor.
Definition switch.h:53
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition switch.cpp:60
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition switch.cpp:58
bool state
Definition fan.h:0
constexpr const char *const MQTT_OPTIMISTIC
Definition mqtt_const.h:126
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
Definition helpers.cpp:420
@ PARSE_ON
Definition helpers.h:442
@ PARSE_TOGGLE
Definition helpers.h:444
@ PARSE_OFF
Definition helpers.h:443
@ PARSE_NONE
Definition helpers.h:441
Simple Helper struct used for Home Assistant MQTT send_discovery().