-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityExtensions.ToBooleanObservable.cs
More file actions
152 lines (137 loc) · 6.87 KB
/
Copy pathEntityExtensions.ToBooleanObservable.cs
File metadata and controls
152 lines (137 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Reactive.Linq;
using NetDaemon.HassModel.Entities;
namespace CodeCasa.NetDaemon.Extensions.Observables;
public static partial class EntityExtensions
{
/// <summary>
/// <para>
/// Returns a boolean observable that emits <see langword="true"/> when the entity opens
/// and emits <see langword="false"/> when the entity closes or changes to any other state.
/// The observable is distinct until changed.
/// </para>
/// <para>
/// Any state other than "on" (such as off, unknown, unavailable, or a null state when
/// the entity is removed) is treated as closed and will emit <see langword="false"/>.
/// </para>
/// </summary>
public static IObservable<bool> ToOpenClosedObservable(this Entity entity) => entity.ToBooleanObservable();
/// <summary>
/// <para>
/// Returns a boolean observable that emits <see langword="true"/> when the entity turns on
/// and emits <see langword="false"/> when the entity turns off or changes to any other state.
/// The observable is distinct until changed.
/// </para>
/// <para>
/// Any state other than "on" (such as off, unknown, unavailable, or a null state when
/// the entity is removed) is treated as off and will emit <see langword="false"/>.
/// </para>
/// </summary>
public static IObservable<bool> ToOnOffObservable(this Entity entity) => entity.ToBooleanObservable();
/// <summary>
/// <para>
/// Returns a boolean observable that emits true when the entity turns on and emits false when the entity changes to any other state (including off, unknown, unavailable or null).
/// The observable is distinct until changed.
/// </para>
/// <para>
/// A null entity state, which typically indicates the entity has been removed, will emit false.
/// </para>
/// </summary>
public static IObservable<bool> ToBooleanObservable(this Entity entity)
{
ArgumentNullException.ThrowIfNull(entity);
return entity.Stateful()
.Select(s => s.New?.IsOn() ?? false)
.DistinctUntilChanged();
}
/// <summary>
/// Returns a boolean observable that reflects the result of the provided predicate on the new state of the provided entity.
/// Predicate will be evaluated on all state changes, including attribute changes.
/// The observable is distinct until changed. A null entity state (typically received when an entity is removed) is filtered out so the predicate doesn't have to handle null values.
/// </summary>
public static IObservable<bool> ToBooleanObservable(this Entity entity, Func<EntityState, bool> predicate)
{
ArgumentNullException.ThrowIfNull(entity);
ArgumentNullException.ThrowIfNull(predicate);
return entity
.StatefulAll()
.Where(s => s.New != null)
.Select(s => predicate(s.New!))
.DistinctUntilChanged();
}
/// <summary>
/// Returns a boolean observable that reflects the result of the provided predicate on the new state of the provided entity.
/// Predicate will be evaluated on all state changes, including attribute changes.
/// The observable is distinct until changed. A null entity state (typically received when an entity is removed) is filtered out so the predicate doesn't have to handle null values.
/// </summary>
public static IObservable<bool>
ToBooleanObservable<TEntity, TEntityState, TAttributes>(
this Entity<TEntity, TEntityState, TAttributes> entity,
Func<TEntityState, bool> predicate)
where TEntity : Entity<TEntity, TEntityState, TAttributes>
where TEntityState : EntityState<TAttributes>
where TAttributes : class
{
ArgumentNullException.ThrowIfNull(entity);
ArgumentNullException.ThrowIfNull(predicate);
return entity
.StatefulAll()
.Where(s => s.New != null)
.Select(s => predicate(s.New!))
.DistinctUntilChanged();
}
/// <summary>
/// <para>
/// Returns a boolean observable that emits true when the entity turns on and emits false when the entity changes to any other state (including off, unknown, unavailable or null).
/// Predicate will be evaluated on all state changes, including attribute changes.
/// </para>
/// <para>
/// The observable only emits subsequent changes and will not emit the initial state.
/// A null entity state, which typically indicates the entity has been removed, will emit false.
/// </para>
/// </summary>
public static IObservable<bool> ToChangesOnlyBooleanObservable(this Entity entity)
{
ArgumentNullException.ThrowIfNull(entity);
return entity.StateAllChanges()
.Select(s => s.New?.IsOn() ?? false)
.DistinctUntilChanged();
}
/// <summary>
/// Returns a boolean observable that reflects the result of the provided predicate on the new state of the provided entity.
/// Predicate will be evaluated on all state changes, including attribute changes.
/// The observable only emits changes and will not emit the initial state.
/// The observable is distinct until changed. A null entity state (typically received when an entity is removed) is filtered out so the predicate doesn't have to handle null values.
/// </summary>
public static IObservable<bool> ToChangesOnlyBooleanObservable(this Entity entity, Func<EntityState, bool> predicate)
{
ArgumentNullException.ThrowIfNull(entity);
ArgumentNullException.ThrowIfNull(predicate);
return entity
.StateAllChanges()
.Where(s => s.New != null)
.Select(s => predicate(s.New!))
.DistinctUntilChanged();
}
/// <summary>
/// Returns a boolean observable that reflects the result of the provided predicate on the new state of the provided entity.
/// Predicate will be evaluated on all state changes, including attribute changes.
/// The observable only emits changes and will not emit the initial state.
/// The observable is distinct until changed. A null entity state (typically received when an entity is removed) is filtered out so the predicate doesn't have to handle null values.
/// </summary>
public static IObservable<bool>
ToChangesOnlyBooleanObservable<TEntity, TEntityState, TAttributes>(
this Entity<TEntity, TEntityState, TAttributes> entity,
Func<TEntityState, bool> predicate)
where TEntity : Entity<TEntity, TEntityState, TAttributes>
where TEntityState : EntityState<TAttributes>
where TAttributes : class
{
ArgumentNullException.ThrowIfNull(entity);
ArgumentNullException.ThrowIfNull(predicate);
return entity
.StateAllChanges()
.Where(s => s.New != null)
.Select(s => predicate(s.New!))
.DistinctUntilChanged();
}
}