ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mqtt_button.cpp
Go to the documentation of this file.
1#include "mqtt_button.h"
2#include "esphome/core/log.h"
3
4#include "mqtt_const.h"
5
6#ifdef USE_MQTT
7#ifdef USE_BUTTON
8
9namespace esphome::mqtt {
10
11ESPHOME_LOG_TAG(TAG, "mqtt.button");
12
13using namespace esphome::button;
14
16
18 this->subscribe(this->command_topic_source_(), [this](const std::string &topic, const std::string &payload) {
19 if (payload == "PRESS") {
20 this->button_->press();
21 } else {
22 ESP_LOGW(TAG, "'%s': Received unknown status payload: %s", LOG_STR_ARG(this->log_name_()), payload.c_str());
23 this->status_momentary_warning(5000);
24 }
25 });
26}
27void MQTTButtonComponent::dump_config() { LOG_MQTT_COMPONENT(false, true); }
28
30 config.state_topic = false;
31}
32
34const EntityBase *MQTTButtonComponent::get_entity() const { return this->button_; }
35
36} // namespace esphome::mqtt
37
38#endif
39#endif // USE_MQTT
void status_momentary_warning(uint32_t length=5000)
Set warning status flag and clear it after length ms.
Base class for all buttons.
Definition button.h:25
void press()
Press this button.
Definition button.cpp:16
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
MQTTButtonComponent(button::Button *button)
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,...
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool state_topic
If the state topic should be included. Defaults to true.