ESPHome  2024.10.2
custom_mqtt_device.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 #ifdef USE_MQTT
5 
7 #include "mqtt_client.h"
8 
9 namespace esphome {
10 namespace mqtt {
11 
22  public:
54  template<typename T>
55  void subscribe(const std::string &topic, void (T::*callback)(const std::string &, const std::string &),
56  uint8_t qos = 0);
57 
58  template<typename T>
59  void subscribe(const std::string &topic, void (T::*callback)(const std::string &), uint8_t qos = 0);
60 
61  template<typename T> void subscribe(const std::string &topic, void (T::*callback)(), uint8_t qos = 0);
62 
95  template<typename T>
96  void subscribe_json(const std::string &topic, void (T::*callback)(const std::string &, JsonObject), uint8_t qos = 0);
97 
98  template<typename T> void subscribe_json(const std::string &topic, void (T::*callback)(JsonObject), uint8_t qos = 0);
99 
115  bool publish(const std::string &topic, const std::string &payload, uint8_t qos = 0, bool retain = false);
116 
133  bool publish(const std::string &topic, float value, int8_t number_decimals = 3);
134 
148  bool publish(const std::string &topic, int value);
149 
167  bool publish_json(const std::string &topic, const json::json_build_t &f, uint8_t qos, bool retain);
168 
184  bool publish_json(const std::string &topic, const json::json_build_t &f);
185 
187  bool is_connected();
188 };
189 
190 template<typename T>
191 void CustomMQTTDevice::subscribe(const std::string &topic,
192  void (T::*callback)(const std::string &, const std::string &), uint8_t qos) {
193  auto f = std::bind(callback, (T *) this, std::placeholders::_1, std::placeholders::_2);
194  global_mqtt_client->subscribe(topic, f, qos);
195 }
196 template<typename T>
197 void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(const std::string &), uint8_t qos) {
198  auto f = std::bind(callback, (T *) this, std::placeholders::_2);
199  global_mqtt_client->subscribe(topic, f, qos);
200 }
201 template<typename T> void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(), uint8_t qos) {
202  auto f = std::bind(callback, (T *) this);
203  global_mqtt_client->subscribe(topic, f, qos);
204 }
205 template<typename T>
206 void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(const std::string &, JsonObject),
207  uint8_t qos) {
208  auto f = std::bind(callback, (T *) this, std::placeholders::_1, std::placeholders::_2);
209  global_mqtt_client->subscribe_json(topic, f, qos);
210 }
211 template<typename T>
212 void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(JsonObject), uint8_t qos) {
213  auto f = std::bind(callback, (T *) this, std::placeholders::_2);
214  global_mqtt_client->subscribe_json(topic, f, qos);
215 }
216 
217 } // namespace mqtt
218 } // namespace esphome
219 
220 #endif // USE_MQTT
bool is_connected()
Check whether the MQTT client is currently connected and messages can be published.
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.
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.
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
Definition: json_util.h:20
This class is a helper class for custom components that communicate using MQTT.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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.
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...