JBHaMqttDiscovery
Loading...
Searching...
No Matches
AsyncMqttClientTransport Class Reference

MQTT transport adapter for AsyncMqttClient. More...

#include <AsyncMqttClientTransport.h>

Inheritance diagram for AsyncMqttClientTransport:
MqttTransport

Public Member Functions

 AsyncMqttClientTransport (AsyncMqttClient &client)
 Construct an AsyncMqttClient transport adapter.
void setServer (const char *host, uint16_t port, const char *user=nullptr, const char *pass=nullptr) override
 Set MQTT server and credentials.
void setServer (const std::string &host, uint16_t port, const std::string &user="", const std::string &pass="") override
 Set MQTT server and credentials (std::string version).
bool connected () const override
bool publish (const char *topic, const uint8_t *payload, size_t len, bool retained, uint8_t qos) override
void setOnConnect (void(*cb_)(void *), void *ctx_) override
Public Member Functions inherited from MqttTransport
virtual void tick ()
 Periodic processing hook (optional).
void setLogger (JBLogger *logger)
 Set the logger for this transport.

Additional Inherited Members

Protected Attributes inherited from MqttTransport
JBLogger * log = nullptr

Detailed Description

MQTT transport adapter for AsyncMqttClient.

AsyncMqttClient is a fully asynchronous, event-driven MQTT client.

Advantages:

  • No polling required
  • Proper QoS support
  • Native connection callbacks

This transport provides the most reliable behavior for Home Assistant discovery and availability handling.

Constructor & Destructor Documentation

◆ AsyncMqttClientTransport()

AsyncMqttClientTransport::AsyncMqttClientTransport ( AsyncMqttClient & client)
inlineexplicit

Construct an AsyncMqttClient transport adapter.

Parameters
clientReference to an already configured AsyncMqttClient instance

Member Function Documentation

◆ connected()

bool AsyncMqttClientTransport::connected ( ) const
inlineoverridevirtual

Check whether the MQTT client is currently connected.

Returns
true if connected, false otherwise

Implements MqttTransport.

◆ publish()

bool AsyncMqttClientTransport::publish ( const char * topic,
const uint8_t * payload,
size_t len,
bool retained,
uint8_t qos )
inlineoverridevirtual

Publish an MQTT message.

Parameters
topicMQTT topic (null-terminated string)
payloadPointer to payload bytes (may be nullptr)
lenPayload length in bytes
retainedWhether the message should be retained
qosRequested QoS level (best-effort for some clients)
Returns
true if the publish request was accepted, false otherwise
Note
Passing payload == nullptr publishes an empty payload, which is required for Home Assistant entity removal.

Implements MqttTransport.

◆ setOnConnect()

void AsyncMqttClientTransport::setOnConnect ( void(* cb_ )(void *),
void * ctx_ )
inlineoverridevirtual

Register a callback invoked when the MQTT connection is established.For asynchronous transports, this is wired directly to the MQTT client's onConnect handler.For synchronous transports, this callback is invoked when a rising-edge connection transition is detected via tick().

Parameters
cbCallback function
ctxUser context pointer passed back to the callback

Implements MqttTransport.

◆ setServer() [1/2]

void AsyncMqttClientTransport::setServer ( const char * host,
uint16_t port,
const char * user = nullptr,
const char * pass = nullptr )
inlineoverridevirtual

Set MQTT server and credentials.

Parameters
hostMQTT host/IP
portMQTT port
userMQTT username
passMQTT password

Implements MqttTransport.

◆ setServer() [2/2]

void AsyncMqttClientTransport::setServer ( const std::string & host,
uint16_t port,
const std::string & user = "",
const std::string & pass = "" )
inlineoverridevirtual

Set MQTT server and credentials (std::string version).

Parameters
hostMQTT host/IP
portMQTT port
userMQTT username
passMQTT password

Implements MqttTransport.


The documentation for this class was generated from the following file: