ESPHome 2026.1.4
Loading...
Searching...
No Matches
custom_mqtt_device.cpp
Go to the documentation of this file.
2
3#ifdef USE_MQTT
4
5#include "esphome/core/log.h"
6
7namespace esphome::mqtt {
8
9static const char *const TAG = "mqtt.custom";
10
11bool CustomMQTTDevice::publish(const std::string &topic, const std::string &payload, uint8_t qos, bool retain) {
12 return global_mqtt_client->publish(topic, payload, qos, retain);
13}
14bool CustomMQTTDevice::publish(const std::string &topic, float value, int8_t number_decimals) {
15 char buf[VALUE_ACCURACY_MAX_LEN];
16 size_t len = value_accuracy_to_buf(buf, value, number_decimals);
17 return global_mqtt_client->publish(topic, buf, len);
18}
19bool CustomMQTTDevice::publish(const std::string &topic, int value) {
20 char buffer[24];
21 int len = snprintf(buffer, sizeof(buffer), "%d", value);
22 return global_mqtt_client->publish(topic, buffer, len);
23}
24bool 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}
27bool 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 esphome::mqtt
33
34#endif // USE_MQTT
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.
bool is_connected()
Check whether the MQTT client is currently connected and messages can be published.
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.
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.
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
Definition json_util.h:46
MQTTClientComponent * global_mqtt_client
size_t value_accuracy_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals)
Format value with accuracy to buffer, returns chars written (excluding null)
Definition helpers.cpp:448
std::string size_t len
Definition helpers.h:595