ESPHome  2024.11.1
mqtt_subscribe_sensor.cpp
Go to the documentation of this file.
2 
3 #ifdef USE_MQTT
4 
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace mqtt_subscribe {
9 
10 static const char *const TAG = "mqtt_subscribe.sensor";
11 
14  this->topic_,
15  [this](const std::string &topic, const std::string &payload) {
16  auto val = parse_number<float>(payload);
17  if (!val.has_value()) {
18  ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str());
19  this->publish_state(NAN);
20  return;
21  }
22 
23  this->publish_state(*val);
24  },
25  this->qos_);
26 }
27 
29 void MQTTSubscribeSensor::set_qos(uint8_t qos) { this->qos_ = qos; }
31  LOG_SENSOR("", "MQTT Subscribe", this);
32  ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str());
33 }
34 
35 } // namespace mqtt_subscribe
36 } // namespace esphome
37 
38 #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
mopeka_std_values val[4]
MQTTClientComponent * global_mqtt_client
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
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.