Table of Contents

Class YeelightLight

Namespace
Yeelight
Assembly
YeelightClient.dll

A light of a Yeelight device: what it is doing, and the commands that change it.

public abstract class YeelightLight : INotifyPropertyChanged
Inheritance
YeelightLight
Implements
Derived
Inherited Members

Remarks

A device has one or two lights. Its main light is the YeelightDevice itself (set_power, set_bright, ...), and a device that has a background light (some ceiling lights) offers it as Background (bg_set_power, bg_set_bright, ...). The two have the same commands and the same properties.

The properties are what the light told. A light reports every change to the connections it has, and the client reads all its properties when a connection is made, so the properties follow the light, whoever changes it (the app of Yeelight, a remote, the switch on the wall). A command does not change them: its effect arrives as a report a moment later. A property is 0 (or empty) until the light has reported it. PropertyChanged is raised for the changes, by the dispatcher of the client (see YeelightClient): it says which property changed, and a change that is still waiting when the same property changes again is raised once, so read the property when it is raised.

Every command returns when the light has answered, and throws a YeelightException when it did not succeed: the light refused it, there is no connection, the answer did not come in time, or the command was held back by the limit of 60 commands a minute that a light puts on a connection. An argument that a light would refuse (a brightness of 101) is refused first, with an ArgumentOutOfRangeException, and nothing is sent. A command that the light does not list among the methods that it supports throws a NotSupportedException. Every command takes a CancellationToken: cancelling it ends the wait with an OperationCanceledException and leaves the connection usable (a command that was already sent is not undone). Most commands are only accepted by a light that is on: see the notes of the individual commands.

A transition makes a change gradual: null (or Zero) is a sudden change, anything else is smooth, over that time, which is at least MinTransition.

Properties

Brightness

Gets the brightness, a percentage from 1 to 100 (0 until the light has reported it).

Color

Gets Rgb as a colour.

ColorMode

Gets the mode that decides which of Rgb, ColorTemperature and Hue with Saturation is what the light shows. It is Rgb until the light has reported it.

ColorTemperature

Gets the colour temperature, in kelvin. It is only what the light shows when ColorMode is ColorTemperature. The range depends on the model.

FlowParameters

Gets the parameters of the flow that is running (empty when none is), as the light reports them: read them with TryParse(string?, out ColorFlow?).

Hue

Gets the hue, from 0 to 359. It is only what the light shows when ColorMode is Hsv.

IsFlowing

Gets whether a colour flow is running.

IsOn

Gets whether the light is on (false means it is switched off by software: it is not without power).

Rgb

Gets the colour as a number: red times 65536 plus green times 256 plus blue. It is only what the light shows when ColorMode is Rgb.

Saturation

Gets the saturation, from 0 to 100. It is only what the light shows when ColorMode is Hsv.

Methods

AdjustAsync(AdjustAction, AdjustProperty, CancellationToken)

Changes the brightness, the colour temperature or the colour without knowing what it is now: a step up, a step down, or a step up that starts again at the bottom when it reaches the top.

AdjustBrightnessAsync(int, TimeSpan?, CancellationToken)

Changes the brightness by a percentage of what it is now.

AdjustColorAsync(TimeSpan?, CancellationToken)

Moves the colour on. How far is the light's own affair: the protocol has no say in it.

AdjustColorTemperatureAsync(int, TimeSpan?, CancellationToken)

Changes the colour temperature by a percentage of what it is now.

SendCommandAsync(string, IReadOnlyList<object?>?, CancellationToken)

Sends a command by its method and parameters, as the protocol has them ("set_power" with "on", "smooth", 500), for what this class has no method for. Nothing is added to the method and nothing is checked.

SetBrightnessAsync(int, TimeSpan?, CancellationToken)

Sets the brightness. Only a light that is on accepts it.

SetColorTemperatureAsync(int, TimeSpan?, CancellationToken)

Sets the colour temperature, which switches the light to ColorTemperature. Only a light that is on accepts it.

SetDefaultAsync(CancellationToken)

Saves the current state of the light in its memory, so that it shows that state when it is switched on at the wall again (after a loss of power). Only a light that is on accepts it.

SetHsvAsync(int, int, TimeSpan?, CancellationToken)

Sets the colour as a hue and a saturation, which switches the light to Hsv. Only a light that is on accepts it.

SetPowerAsync(bool, TimeSpan?, PowerOnMode, CancellationToken)

Switches the light on or off.

SetRgbAsync(Color, TimeSpan?, CancellationToken)

Sets the colour, which switches the light to Rgb. Only a light that is on accepts it.

SetRgbAsync(int, int, int, TimeSpan?, CancellationToken)

Sets the colour, which switches the light to Rgb. Only a light that is on accepts it.

SetSceneAsync(YeelightScene, CancellationToken)

Sets the light directly to a scene, switching it on first when it is off.

StartColorFlowAsync(ColorFlow, CancellationToken)

Starts a colour flow. Only a light that is on accepts it.

StopColorFlowAsync(CancellationToken)

Stops the colour flow that is running.

ToggleAsync(CancellationToken)

Switches the light off when it is on, and on when it is off, without having to know which it is.

TurnOffAsync(TimeSpan?, CancellationToken)

Switches the light off.

TurnOnAsync(TimeSpan?, PowerOnMode, CancellationToken)

Switches the light on.

Events

PropertyChanged

Raised when a property of the light changed, by the dispatcher of the client: one at a time, in the order in which they happened, on a thread that is not the one that reads from the light.