ESPHome 2025.5.0
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 {
8namespace mqtt {
9
10static const char *const TAG = "mqtt.custom";
11
12bool 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}
15bool 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}
19bool CustomMQTTDevice::publish(const std::string &topic, int value) {
20 char buffer[24];
21 sprintf(buffer, "%d", value);
22 return this->publish(topic, buffer);
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 mqtt
33} // namespace esphome
34
35#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:20
MQTTClientComponent * global_mqtt_client
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:435