Table of Contents

Class HaMqttConnection

Namespace
HaMqttDiscoverable
Assembly
HaMqttDiscoverable.dll

Wraps an MQTT connection used to publish Home Assistant discovery configs, entity state, and to receive commands. Create one connection per application (or per broker) and pass it to every HaEntity<TConfig> you create.

public sealed class HaMqttConnection : IAsyncDisposable
Inheritance
HaMqttConnection
Implements
Inherited Members

Constructors

HaMqttConnection(MqttSettings)

Creates a connection from the given settings. Call ConnectAsync(CancellationToken) to actually connect.

public HaMqttConnection(MqttSettings settings)

Parameters

settings MqttSettings

Properties

AvailabilityTopic

The shared availability topic published by this connection (its MQTT last-will topic), used by entities that don't define their own Availability override.

public string AvailabilityTopic { get; }

Property Value

string

DiscoveryPrefix

The discovery prefix configured on the Home Assistant side (default "homeassistant").

public string DiscoveryPrefix { get; }

Property Value

string

IsConnected

Whether the underlying MQTT client is currently connected.

public bool IsConnected { get; }

Property Value

bool

MqttClient

The underlying MQTTnet client, exposed for advanced scenarios not covered by this library.

public IMqttClient MqttClient { get; }

Property Value

IMqttClient

Methods

ConnectAsync(CancellationToken)

Connects to the MQTT broker, registering a last-will message so Home Assistant can detect an unexpected disconnect.

public Task ConnectAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

DisconnectAsync(CancellationToken)

Publishes "offline" to the shared availability topic (if enabled) and disconnects gracefully.

public Task DisconnectAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

DisposeAsync()

Disconnects (best-effort) and releases the underlying MQTT client.

public ValueTask DisposeAsync()

Returns

ValueTask

PublishAsync(string, string, bool, int, CancellationToken)

Publishes a raw payload to an arbitrary MQTT topic.

public Task PublishAsync(string topic, string payload, bool retain = false, int qos = 0, CancellationToken cancellationToken = default)

Parameters

topic string
payload string
retain bool
qos int
cancellationToken CancellationToken

Returns

Task

SubscribeAsync(string, Func<string, Task>, int, CancellationToken)

Subscribes to an MQTT topic, invoking handler with the payload of every message received on it.

public Task SubscribeAsync(string topic, Func<string, Task> handler, int qos = 0, CancellationToken cancellationToken = default)

Parameters

topic string
handler Func<string, Task>
qos int
cancellationToken CancellationToken

Returns

Task

UnsubscribeAsync(string, Func<string, Task>, CancellationToken)

Removes a previously registered handler for a topic, unsubscribing entirely once no handlers remain.

public Task UnsubscribeAsync(string topic, Func<string, Task> handler, CancellationToken cancellationToken = default)

Parameters

topic string
handler Func<string, Task>
cancellationToken CancellationToken

Returns

Task