ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
custom_mqtt_device.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_MQTT
5
7#include "mqtt_client.h"
8
9namespace esphome::mqtt {
10
21 public:
53 template<typename T>
54 void subscribe(const std::string &topic, void (T::*callback)(const std::string &, const std::string &),
55 uint8_t qos = 0);
56
57 template<typename T>
58 void subscribe(const std::string &topic, void (T::*callback)(const std::string &), uint8_t qos = 0);
59
60 template<typename T> void subscribe(const std::string &topic, void (T::*callback)(), uint8_t qos = 0);
61
94 template<typename T>
95 void subscribe_json(const std::string &topic, void (T::*callback)(const std::string &, JsonObject), uint8_t qos = 0);
96
97 template<typename T> void subscribe_json(const std::string &topic, void (T::*callback)(JsonObject), uint8_t qos = 0);
98
114 bool publish(const std::string &topic, const std::string &payload, uint8_t qos = 0, bool retain = false);
115
132 bool publish(const std::string &topic, float value, int8_t number_decimals = 3);
133
147 bool publish(const std::string &topic, int value);
148
166 bool publish_json(const std::string &topic, const json::json_build_t &f, uint8_t qos, bool retain);
167
183 bool publish_json(const std::string &topic, const json::json_build_t &f);
184
186 bool is_connected();
187};
188
189template<typename T>
190void CustomMQTTDevice::subscribe(const std::string &topic,
191 void (T::*callback)(const std::string &, const std::string &), uint8_t qos) {
192 auto *obj = static_cast<T *>(this);
194 topic, [obj, callback](const std::string &t, const std::string &payload) { (obj->*callback)(t, payload); }, qos);
195}
196template<typename T>
197void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(const std::string &), uint8_t qos) {
198 auto *obj = static_cast<T *>(this);
200 topic, [obj, callback](const std::string &, const std::string &payload) { (obj->*callback)(payload); }, qos);
201}
202template<typename T> void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(), uint8_t qos) {
203 auto *obj = static_cast<T *>(this);
205 topic, [obj, callback](const std::string &, const std::string &) { (obj->*callback)(); }, qos);
206}
207template<typename T>
208void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(const std::string &, JsonObject),
209 uint8_t qos) {
210 auto *obj = static_cast<T *>(this);
212 topic, [obj, callback](const std::string &t, JsonObject root) { (obj->*callback)(t, root); }, qos);
213}
214template<typename T>
215void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(JsonObject), uint8_t qos) {
216 auto *obj = static_cast<T *>(this);
218 topic, [obj, callback](const std::string &, JsonObject root) { (obj->*callback)(root); }, qos);
219}
220
221} // namespace esphome::mqtt
222
223#endif // USE_MQTT
This class is a helper class for custom components that communicate using 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.
void subscribe(const std::string &topic, void(T::*callback)(const std::string &, const std::string &), uint8_t qos=0)
Subscribe to an MQTT topic with the given Quality of Service.
void subscribe_json(const std::string &topic, void(T::*callback)(const std::string &, JsonObject), uint8_t qos=0)
Subscribe to an MQTT topic and call the callback if the payload can be decoded as JSON with the given...
void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos=0)
Subscribe to a MQTT topic and automatically parse JSON payload.
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.
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
Definition json_util.h:151
static float float float t
MQTTClientComponent * global_mqtt_client