Table of Contents

Class Button

Namespace
HaMqttDiscoverable.Entities
Assembly
HaMqttDiscoverable.dll

A stateless, momentary action exposed to Home Assistant, e.g. "Restart" or "Identify".

public sealed class Button : HaEntity<ButtonConfig>, IAsyncDisposable
Inheritance
Button
Implements
Inherited Members

Examples

var restart = new Button(connection, new ButtonConfig
{
    Name = "Restart",
    UniqueId = "device-restart",
    Device = device,
});
restart.Pressed += async () => await RestartAsync();
await restart.PublishDiscoveryAsync();

Constructors

Button(HaMqttConnection, ButtonConfig, Func<Task>?)

Creates a button from the given config, optionally subscribing onPressed to Pressed.

public Button(HaMqttConnection connection, ButtonConfig config, Func<Task>? onPressed = null)

Parameters

connection HaMqttConnection
config ButtonConfig
onPressed Func<Task>

Properties

HasStateTopic

Whether this entity type reports a state at all. Stateless entities like Button override this to false.

protected override bool HasStateTopic { get; }

Property Value

bool

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

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

Events

Pressed

Raised whenever Home Assistant reports the button was pressed.

public event Func<Task>? Pressed

Event Type

Func<Task>