ESPHome  2024.11.2
custom_mqtt_device.cpp
Go to the documentation of this file.
1 #include "custom_mqtt_device.h"
2 
3 #ifdef USE_MQTT
4 
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace mqtt {
9 
10 static const char *const TAG = "mqtt.custom";
11 
12 bool CustomMQTTDevice::publish(const std::string &topic, const std::string &payload, uint8_t qos, bool retain) {
13  return global_mqtt_client->publish(topic, payload, qos, retain);
14 }
15 bool CustomMQTTDevice::publish(const std::string &topic, float value, int8_t number_decimals) {
16  auto str = value_accuracy_to_string(value, number_decimals);
17  return this->publish(topic, str);
18 }
19 bool CustomMQTTDevice::publish(const std::string &topic, int value) {
20  char buffer[24];
21  sprintf(buffer, "%d", value);
22  return this->publish(topic, buffer);
23 }
24 bool CustomMQTTDevice::publish_json(const std::string &topic, const json::json_build_t &f, uint8_t qos, bool retain) {
25  return global_mqtt_client->publish_json(topic, f, qos, retain);
26 }
27 bool CustomMQTTDevice::publish_json(const std::string &topic, const json::json_build_t &f) {
28  return this->publish_json(topic, f, 0, false);
29 }
31 
32 } // namespace mqtt
33 } // namespace esphome
34 
35 #endif // USE_MQTT
bool is_connected()
Check whether the MQTT client is currently connected and messages can be published.
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
Create a string from a value and an accuracy in decimals.
Definition: helpers.cpp:415
bool publish(const std::string &topic, const std::string &payload, uint8_t qos=0, bool retain=false)
Publish an MQTT message with the given payload and QoS and retain settings.
MQTTClientComponent * global_mqtt_client
bool publish_json(const std::string &topic, const json::json_build_t &f, uint8_t qos, bool retain)
Publish a JSON-encoded MQTT message with the given Quality of Service and retain settings.
bool publish(const MQTTMessage &message)
Publish a MQTTMessage.
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
Definition: json_util.h:20
bool publish_json(const std::string &topic, const json::json_build_t &f, uint8_t qos=0, bool retain=false)
Construct and send a JSON MQTT message.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7