Table of Contents

Class Light

Namespace
HaMqttDiscoverable.Entities
Assembly
HaMqttDiscoverable.dll

A light that can be turned on/off and, optionally, dimmed.

public sealed class Light : HaEntity<LightConfig>, IAsyncDisposable
Inheritance
Light
Implements
Inherited Members

Examples

var light = new Light(connection, new LightConfig
{
    Name = "Lamp",
    UniqueId = "living-room-lamp",
    Device = device,
    SupportsBrightness = true,
});
light.CommandReceived += async state =>
{
    ApplyToHardware(state.State == "ON", state.Brightness);
    await light.PublishStateAsync(state);
};
await light.PublishDiscoveryAsync();

Constructors

Light(HaMqttConnection, LightConfig, Func<LightState, Task>?)

Creates a light from the given config, optionally subscribing onCommand to CommandReceived.

public Light(HaMqttConnection connection, LightConfig config, Func<LightState, Task>? onCommand = null)

Parameters

connection HaMqttConnection
config LightConfig
onCommand Func<LightState, Task>

Properties

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

PublishStateAsync(LightState, bool, CancellationToken)

Publishes the light's current state.

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

Parameters

state LightState
retain bool
cancellationToken CancellationToken

Returns

Task

PublishStateAsync(bool, int?, bool, CancellationToken)

Publishes the light's current on/off state and, optionally, brightness.

public Task PublishStateAsync(bool isOn, int? brightness = null, bool retain = true, CancellationToken cancellationToken = default)

Parameters

isOn bool
brightness int?
retain bool
cancellationToken CancellationToken

Returns

Task

Events

CommandReceived

Raised when Home Assistant sends a new desired state.

public event Func<LightState, Task>? CommandReceived

Event Type

Func<LightState, Task>