ESPHome 2025.5.0
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 {
10namespace mqtt {
11
12static const char *const TAG = "mqtt.button";
13
14using namespace esphome::button;
15
17
19 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
20 if (payload == "PRESS") {
21 this->button_->press();
22 } else {
23 ESP_LOGW(TAG, "'%s': Received unknown status payload: %s", this->friendly_name().c_str(), payload.c_str());
24 this->status_momentary_warning("state", 5000);
25 }
26 });
27}
29 ESP_LOGCONFIG(TAG, "MQTT Button '%s': ", this->button_->get_name().c_str());
30 LOG_MQTT_COMPONENT(true, true);
31}
32
34 config.state_topic = false;
35 if (!this->button_->get_device_class().empty())
37}
38
39std::string MQTTButtonComponent::component_type() const { return "button"; }
40const EntityBase *MQTTButtonComponent::get_entity() const { return this->button_; }
41
42} // namespace mqtt
43} // namespace esphome
44
45#endif
46#endif // USE_MQTT
void status_momentary_warning(const std::string &name, uint32_t length=5000)
std::string get_device_class()
Get the device class, using the manual override if set.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
Base class for all buttons.
Definition button.h:29
void press()
Press this button.
Definition button.cpp:9
const EntityBase * get_entity() const override
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
std::string component_type() const override
"button" component type.
MQTTButtonComponent(button::Button *button)
virtual 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.
constexpr const char *const MQTT_DEVICE_CLASS
Definition mqtt_const.h:58
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool state_topic
If the state topic should be included. Defaults to true.