Table of Contents

Class Fan

Namespace
HaMqttDiscoverable.Entities
Assembly
HaMqttDiscoverable.dll

A fan that can be turned on/off, and optionally has a speed percentage and/or named preset modes. Oscillation and direction (forward/reverse) control aren't exposed by this type yet - they'd follow the same auxiliary-topic pattern as SupportsPercentage support; see HaEntity.RegisterAuxiliaryCommandTopic if you need to add them.

public sealed class Fan : HaEntity<FanConfig>, IAsyncDisposable
Inheritance
Fan
Implements
Inherited Members

Constructors

Fan(HaMqttConnection, FanConfig)

Creates a fan from the given config.

public Fan(HaMqttConnection connection, FanConfig config)

Parameters

connection HaMqttConnection
config FanConfig

Properties

PercentageCommandTopic

The topic this fan accepts a speed percentage on, or null if SupportsPercentage is false.

public string? PercentageCommandTopic { get; }

Property Value

string

PercentageStateTopic

The topic this fan publishes its speed percentage to, or null if SupportsPercentage is false.

public string? PercentageStateTopic { get; }

Property Value

string

PresetModeCommandTopic

The topic this fan accepts a preset mode on, or null if PresetModes is empty.

public string? PresetModeCommandTopic { get; }

Property Value

string

PresetModeStateTopic

The topic this fan publishes its preset mode to, or null if PresetModes is empty.

public string? PresetModeStateTopic { get; }

Property Value

string

SupportsCommands

Whether this entity type accepts commands from Home Assistant (e.g. switches, numbers). Sensors are read-only and return false.

protected override bool SupportsCommands { get; }

Property Value

bool

Methods

EnrichDiscoveryPayload(JsonObject)

Allows subclasses to add fields to the discovery payload that aren't part of EntityConfig directly.

protected override void EnrichDiscoveryPayload(JsonObject payload)

Parameters

payload JsonObject

OnCommandReceivedAsync(string)

Called with the raw payload every time a message arrives on CommandTopic. Overridden by controllable entity types.

protected override Task OnCommandReceivedAsync(string payload)

Parameters

payload string

Returns

Task

PublishPercentageAsync(int, bool, CancellationToken)

Publishes the fan's current speed (0-100). Requires SupportsPercentage.

public Task PublishPercentageAsync(int percentage, bool retain = true, CancellationToken cancellationToken = default)

Parameters

percentage int
retain bool
cancellationToken CancellationToken

Returns

Task

PublishPresetModeAsync(string, bool, CancellationToken)

Publishes the fan's current preset mode. Requires PresetModes to be non-empty.

public Task PublishPresetModeAsync(string presetMode, bool retain = true, CancellationToken cancellationToken = default)

Parameters

presetMode string
retain bool
cancellationToken CancellationToken

Returns

Task

PublishStateAsync(bool, bool, CancellationToken)

Publishes the fan's current on/off state.

public Task PublishStateAsync(bool isOn, bool retain = true, CancellationToken cancellationToken = default)

Parameters

isOn bool
retain bool
cancellationToken CancellationToken

Returns

Task

Events

CommandReceived

Raised when Home Assistant sends a command to turn the fan on or off.

public event Func<bool, Task>? CommandReceived

Event Type

Func<bool, Task>

PercentageCommandReceived

Raised when Home Assistant sends a new speed percentage (0-100). Only raised when SupportsPercentage is true.

public event Func<int, Task>? PercentageCommandReceived

Event Type

Func<int, Task>

PresetModeCommandReceived

Raised when Home Assistant sends a new preset mode. Only raised when PresetModes is non-empty.

public event Func<string, Task>? PresetModeCommandReceived

Event Type

Func<string, Task>