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
connectionHaMqttConnectionconfigLightConfigonCommandFunc<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
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
payloadJsonObject
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
payloadstring
Returns
PublishStateAsync(LightState, bool, CancellationToken)
Publishes the light's current state.
public Task PublishStateAsync(LightState state, bool retain = true, CancellationToken cancellationToken = default)
Parameters
stateLightStateretainboolcancellationTokenCancellationToken
Returns
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
isOnboolbrightnessint?retainboolcancellationTokenCancellationToken
Returns
Events
CommandReceived
Raised when Home Assistant sends a new desired state.
public event Func<LightState, Task>? CommandReceived