ESPHome  2024.10.2
mqtt_backend_esp8266.h
Go to the documentation of this file.
1 #pragma once
2 #include "mqtt_backend.h"
3 
4 #ifdef USE_MQTT
5 #ifdef USE_ESP8266
6 
7 #include <AsyncMqttClient.h>
8 
9 namespace esphome {
10 namespace mqtt {
11 
12 class MQTTBackendESP8266 final : public MQTTBackend {
13  public:
14  void set_keep_alive(uint16_t keep_alive) final { mqtt_client_.setKeepAlive(keep_alive); }
15  void set_client_id(const char *client_id) final { mqtt_client_.setClientId(client_id); }
16  void set_clean_session(bool clean_session) final { mqtt_client_.setCleanSession(clean_session); }
17  void set_credentials(const char *username, const char *password) final {
18  mqtt_client_.setCredentials(username, password);
19  }
20  void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final {
21  mqtt_client_.setWill(topic, qos, retain, payload);
22  }
23  void set_server(network::IPAddress ip, uint16_t port) final { mqtt_client_.setServer(ip, port); }
24  void set_server(const char *host, uint16_t port) final { mqtt_client_.setServer(host, port); }
25 #if ASYNC_TCP_SSL_ENABLED
26  void set_secure(bool secure) { mqtt_client.setSecure(secure); }
27  void add_server_fingerprint(const uint8_t *fingerprint) { mqtt_client.addServerFingerprint(fingerprint); }
28 #endif
29 
30  void set_on_connect(std::function<on_connect_callback_t> &&callback) final {
31  this->mqtt_client_.onConnect(std::move(callback));
32  }
33  void set_on_disconnect(std::function<on_disconnect_callback_t> &&callback) final {
34  auto async_callback = [callback](AsyncMqttClientDisconnectReason reason) {
35  // int based enum so casting isn't a problem
36  callback(static_cast<MQTTClientDisconnectReason>(reason));
37  };
38  this->mqtt_client_.onDisconnect(std::move(async_callback));
39  }
40  void set_on_subscribe(std::function<on_subscribe_callback_t> &&callback) final {
41  this->mqtt_client_.onSubscribe(std::move(callback));
42  }
43  void set_on_unsubscribe(std::function<on_unsubscribe_callback_t> &&callback) final {
44  this->mqtt_client_.onUnsubscribe(std::move(callback));
45  }
46  void set_on_message(std::function<on_message_callback_t> &&callback) final {
47  auto async_callback = [callback](const char *topic, const char *payload,
48  AsyncMqttClientMessageProperties async_properties, size_t len, size_t index,
49  size_t total) { callback(topic, payload, len, index, total); };
50  mqtt_client_.onMessage(std::move(async_callback));
51  }
52  void set_on_publish(std::function<on_publish_user_callback_t> &&callback) final {
53  this->mqtt_client_.onPublish(std::move(callback));
54  }
55 
56  bool connected() const final { return mqtt_client_.connected(); }
57  void connect() final { mqtt_client_.connect(); }
58  void disconnect() final { mqtt_client_.disconnect(true); }
59  bool subscribe(const char *topic, uint8_t qos) final { return mqtt_client_.subscribe(topic, qos) != 0; }
60  bool unsubscribe(const char *topic) final { return mqtt_client_.unsubscribe(topic) != 0; }
61  bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final {
62  return mqtt_client_.publish(topic, qos, retain, payload, length, false, 0) != 0;
63  }
65 
66  protected:
67  AsyncMqttClient mqtt_client_;
68 };
69 
70 } // namespace mqtt
71 } // namespace esphome
72 
73 #endif // defined(USE_ESP8266)
74 #endif
void add_server_fingerprint(const uint8_t *fingerprint)
void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final
void set_on_publish(std::function< on_publish_user_callback_t > &&callback) final
void set_on_message(std::function< on_message_callback_t > &&callback) final
void set_on_connect(std::function< on_connect_callback_t > &&callback) final
void set_on_subscribe(std::function< on_subscribe_callback_t > &&callback) final
bool unsubscribe(const char *topic) final
void set_client_id(const char *client_id) final
void set_on_disconnect(std::function< on_disconnect_callback_t > &&callback) final
void set_keep_alive(uint16_t keep_alive) final
void set_on_unsubscribe(std::function< on_unsubscribe_callback_t > &&callback) final
void set_credentials(const char *username, const char *password) final
std::string size_t len
Definition: helpers.h:292
bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool subscribe(const char *topic, uint8_t qos) final
void set_server(network::IPAddress ip, uint16_t port) final
void set_clean_session(bool clean_session) final
void set_server(const char *host, uint16_t port) final
virtual bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain)=0