ESPHome  2024.9.2
mqtt_subscribe_text_sensor.cpp
Go to the documentation of this file.
2 
3 #ifdef USE_MQTT
4 
5 #include "esphome/core/log.h"
6 #include <utility>
7 
8 namespace esphome {
9 namespace mqtt_subscribe {
10 
11 static const char *const TAG = "mqtt_subscribe.text_sensor";
12 
14  this->parent_->subscribe(
15  this->topic_, [this](const std::string &topic, const std::string &payload) { this->publish_state(payload); },
16  this->qos_);
17 }
19 void MQTTSubscribeTextSensor::set_qos(uint8_t qos) { this->qos_ = qos; }
21  LOG_TEXT_SENSOR("", "MQTT Subscribe Text Sensor", this);
22  ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str());
23 }
24 
25 } // namespace mqtt_subscribe
26 } // namespace esphome
27 
28 #endif // USE_MQTT
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition: component.cpp:27
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to an MQTT topic and call callback when a message is received.