ESPHome 2026.1.5
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::mqtt {
10
11static const char *const TAG = "mqtt.switch";
12
13using namespace esphome::switch_;
14
16
18 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
19 switch (parse_on_off(payload.c_str())) {
20 case PARSE_ON:
21 this->switch_->turn_on();
22 break;
23 case PARSE_OFF:
24 this->switch_->turn_off();
25 break;
26 case PARSE_TOGGLE:
27 this->switch_->toggle();
28 break;
29 case PARSE_NONE:
30 default:
31 ESP_LOGW(TAG, "'%s': Received unknown status payload: %s", this->friendly_name_().c_str(), payload.c_str());
32 this->status_momentary_warning("state", 5000);
33 break;
34 }
35 });
37 [this](bool enabled) { this->defer("send", [this, enabled]() { this->publish_state(enabled); }); });
38}
40 ESP_LOGCONFIG(TAG, "MQTT Switch '%s': ", this->switch_->get_name().c_str());
41 LOG_MQTT_COMPONENT(true, true);
42}
43
45const EntityBase *MQTTSwitchComponent::get_entity() const { return this->switch_; }
47 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
48 if (this->switch_->assumed_state()) {
49 root[MQTT_OPTIMISTIC] = true;
50 }
51}
53
55 const char *state_s = state ? "ON" : "OFF";
56 return this->publish(this->get_state_topic_(), state_s);
57}
58
59} // namespace esphome::mqtt
60
61#endif
62#endif // USE_MQTT
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:492
void status_momentary_warning(const char *name, uint32_t length=5000)
Set warning status flag and automatically clear it after a timeout.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:73
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string friendly_name_() const
Get the friendly name of this MQTT component.
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
MQTTSwitchComponent(switch_::Switch *a_switch)
Base class for all switches.
Definition switch.h:39
void toggle()
Toggle this switch.
Definition switch.cpp:29
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
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition switch.cpp:73
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition switch.cpp:71
bool state
Definition fan.h:0
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
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:419
@ PARSE_ON
Definition helpers.h:1088
@ PARSE_TOGGLE
Definition helpers.h:1090
@ PARSE_OFF
Definition helpers.h:1089
@ PARSE_NONE
Definition helpers.h:1087
Simple Helper struct used for Home Assistant MQTT send_discovery().