DelayUntilStateChange support
The API both fluent and normal API gets the possibility to wait for a state change. This required also to support cancelation of listen events. This means that you now also can cancel any WhenStateChange or ListenEvent calls in your own apps.
code example of normal API
await DelayUntilStateChange("input_boolean.checkitout", to: "on").Task;
The returned type is a object where you use it to wait and use own time out. When that return object is disposed it will cancel the task but you can also cancel yourself.
using var stateChangeDelay = DelayUntilStateChange("input_boolean.checkitout", to: "on").Task;
var resultTask = await Task.WhenAny(stateChangeDelay.Task, new CancellationTokenSource(TimeSpan.FromHours(1)).Token.AsTask());
if (resultTask.IsCanceled)
{
// Ops the state did not change within the hour
}
else
{
// Yay the state changed
}
Fluent API example
await Entity("binary_sensor.pir")
.DelayUntilStateChange((to, _) => to?.State == "on").Task;
Fixes #22