ESPHome 2026.11.0-dev
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
11ESPHOME_LOG_TAG(TAG, "mqtt.switch");
12
13using namespace esphome::switch_;
14
16
18 this->subscribe(this->command_topic_source_(), [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", LOG_STR_ARG(this->log_name_()), payload.c_str());
32 this->status_momentary_warning(5000);
33 break;
34 }
35 });
37 [this](bool enabled) { this->defer(SEND_DEFER_ID, [this, enabled]() { this->publish_state(enabled); }); });
38}
39void MQTTSwitchComponent::dump_config() { LOG_MQTT_COMPONENT(true, true); }
40
42const EntityBase *MQTTSwitchComponent::get_entity() const { return this->switch_; }
44 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
45 if (this->switch_->assumed_state()) {
46 root[MQTT_OPTIMISTIC] = true;
47 }
48}
50
52 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
53 const char *state_s = state ? "ON" : "OFF";
54 return this->publish(this->get_state_topic_to_(topic_buf), state_s);
55}
56
57} // namespace esphome::mqtt
58
59#endif
60#endif // USE_MQTT
void status_momentary_warning(uint32_t length=5000)
Set warning status flag and clear it after length ms.
void defer(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call with a const char* name.
static constexpr uint32_t SEND_DEFER_ID
const LogString * log_name_() const
Entity name for a "%s" log argument.
void subscribe(T &&topic, F &&callback, uint8_t qos=0)
Subscribe to a MQTT topic.
MQTTTopicSource command_topic_source_() const
The command topic as a subscription source. A lambda topic is read once and copied,...
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
MQTTSwitchComponent(switch_::Switch *a_switch)
Base class for all switches.
Definition switch.h:43
void toggle()
Toggle this switch.
Definition switch.cpp:27
void add_on_state_callback(F &&callback)
Set callback for state changes.
Definition switch.h:101
void turn_on()
Turn this switch on.
Definition switch.cpp:19
void turn_off()
Turn this switch off.
Definition switch.cpp:23
bool state
The current reported state of the binary sensor.
Definition switch.h:60
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition switch.cpp:85
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
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:554
@ PARSE_ON
Definition helpers.h:1741
@ PARSE_TOGGLE
Definition helpers.h:1743
@ PARSE_OFF
Definition helpers.h:1742
@ PARSE_NONE
Definition helpers.h:1740
Simple Helper struct used for Home Assistant MQTT send_discovery().