mirror of
https://github.com/esphome/esphome.git
synced 2026-10-14 10:12:52 +00:00
43 lines
1.1 KiB
C++
43 lines
1.1 KiB
C++
#include "mqtt_button.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
#include "mqtt_const.h"
|
|
|
|
#ifdef USE_MQTT
|
|
#ifdef USE_BUTTON
|
|
|
|
namespace esphome::mqtt {
|
|
|
|
ESPHOME_LOG_TAG(TAG, "mqtt.button");
|
|
|
|
using namespace esphome::button;
|
|
|
|
MQTTButtonComponent::MQTTButtonComponent(button::Button *button) : button_(button) {}
|
|
|
|
void MQTTButtonComponent::setup() {
|
|
this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
|
|
if (payload == "PRESS") {
|
|
this->button_->press();
|
|
} else {
|
|
ESP_LOGW(TAG, "'%s': Received unknown status payload: %s", this->friendly_name_().c_str(), payload.c_str());
|
|
this->status_momentary_warning(5000);
|
|
}
|
|
});
|
|
}
|
|
void MQTTButtonComponent::dump_config() {
|
|
ESP_LOGCONFIG(TAG, "MQTT Button '%s': ", this->button_->get_name().c_str());
|
|
LOG_MQTT_COMPONENT(false, true);
|
|
}
|
|
|
|
void MQTTButtonComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) {
|
|
config.state_topic = false;
|
|
}
|
|
|
|
MQTT_COMPONENT_TYPE(MQTTButtonComponent, "button")
|
|
const EntityBase *MQTTButtonComponent::get_entity() const { return this->button_; }
|
|
|
|
} // namespace esphome::mqtt
|
|
|
|
#endif
|
|
#endif // USE_MQTT
|