-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightTransitionPipelineConfiguratorExtensions.Scene.cs
More file actions
240 lines (224 loc) · 14.2 KB
/
Copy pathLightTransitionPipelineConfiguratorExtensions.Scene.cs
File metadata and controls
240 lines (224 loc) · 14.2 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using CodeCasa.AutomationPipelines.Lights.Cycle;
using CodeCasa.AutomationPipelines.Lights.Pipeline;
using CodeCasa.AutomationPipelines.Lights.Toggle;
using CodeCasa.Lights.NetDaemon;
using CodeCasa.Lights.NetDaemon.Scenes;
using NetDaemon.HassModel.Entities;
namespace CodeCasa.AutomationPipelines.Lights.NetDaemon.Extensions;
public static partial class LightTransitionPipelineConfiguratorExtensions
{
// -------------------------------------------------------------------------
// When
// -------------------------------------------------------------------------
/// <summary>
/// Registers a node that applies light parameters from the given Home Assistant <paramref name="sceneEntity"/>
/// when the observable of type <typeparamref name="TObservable"/> emits <see langword="true"/>.
/// When the observable emits <see langword="false"/>, inputs are passed through unchanged.
/// The scene is fetched and cached via <see cref="LightSceneCacheService"/> on first use.
/// If the current light is not part of the scene, no state is applied.
/// The observable is resolved from the service provider.
/// </summary>
/// <typeparam name="TObservable">The type of the observable to resolve from the service provider.</typeparam>
/// <param name="configurator">The pipeline configurator.</param>
/// <param name="sceneEntity">The scene entity whose light parameters will be applied.</param>
/// <returns>The configurator instance for method chaining.</returns>
public static ILightTransitionPipelineConfigurator<NetDaemonLight> When<TObservable>(
this ILightTransitionPipelineConfigurator<NetDaemonLight> configurator,
IEntityCore sceneEntity)
where TObservable : IObservable<bool>
{
return configurator.When<TObservable>(sp => SceneExtensionHelpers.GetSceneLightParameters(sp, sceneEntity));
}
/// <summary>
/// Registers a node that applies light parameters from the given Home Assistant <paramref name="sceneEntity"/>
/// when the <paramref name="observable"/> emits <see langword="true"/>.
/// When the observable emits <see langword="false"/>, inputs are passed through unchanged.
/// The scene is fetched and cached via <see cref="LightSceneCacheService"/> on first use.
/// If the current light is not part of the scene, no state is applied.
/// </summary>
/// <param name="configurator">The pipeline configurator.</param>
/// <param name="observable">The observable that determines when to apply the scene.</param>
/// <param name="sceneEntity">The scene entity whose light parameters will be applied.</param>
/// <returns>The configurator instance for method chaining.</returns>
public static ILightTransitionPipelineConfigurator<NetDaemonLight> When(
this ILightTransitionPipelineConfigurator<NetDaemonLight> configurator,
IObservable<bool> observable,
IEntityCore sceneEntity)
{
return configurator.When(observable, sp => SceneExtensionHelpers.GetSceneLightParameters(sp, sceneEntity));
}
// -------------------------------------------------------------------------
// Switch
// -------------------------------------------------------------------------
/// <summary>
/// Registers a node that switches between two Home Assistant scenes based on a boolean observable.
/// When the observable of type <typeparamref name="TObservable"/> emits <see langword="true"/>, the
/// <paramref name="trueSceneEntity"/> is applied; when it emits <see langword="false"/>, the
/// <paramref name="falseSceneEntity"/> is applied.
/// Scenes are fetched and cached via <see cref="LightSceneCacheService"/> on first use.
/// The observable is resolved from the service provider.
/// </summary>
/// <typeparam name="TObservable">The type of the observable to resolve from the service provider.</typeparam>
/// <param name="configurator">The pipeline configurator.</param>
/// <param name="trueSceneEntity">The scene entity to apply when the observable emits true.</param>
/// <param name="falseSceneEntity">The scene entity to apply when the observable emits false.</param>
/// <returns>The configurator instance for method chaining.</returns>
public static ILightTransitionPipelineConfigurator<NetDaemonLight> Switch<TObservable>(
this ILightTransitionPipelineConfigurator<NetDaemonLight> configurator,
IEntityCore trueSceneEntity,
IEntityCore falseSceneEntity)
where TObservable : IObservable<bool>
{
return configurator.Switch<TObservable>(
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, trueSceneEntity),
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, falseSceneEntity));
}
/// <summary>
/// Registers a node that switches between two Home Assistant scenes based on the <paramref name="observable"/>.
/// When the observable emits <see langword="true"/>, the <paramref name="trueSceneEntity"/> is applied;
/// when it emits <see langword="false"/>, the <paramref name="falseSceneEntity"/> is applied.
/// Scenes are fetched and cached via <see cref="LightSceneCacheService"/> on first use.
/// </summary>
/// <param name="configurator">The pipeline configurator.</param>
/// <param name="observable">The observable that determines which scene to apply.</param>
/// <param name="trueSceneEntity">The scene entity to apply when the observable emits true.</param>
/// <param name="falseSceneEntity">The scene entity to apply when the observable emits false.</param>
/// <returns>The configurator instance for method chaining.</returns>
public static ILightTransitionPipelineConfigurator<NetDaemonLight> Switch(
this ILightTransitionPipelineConfigurator<NetDaemonLight> configurator,
IObservable<bool> observable,
IEntityCore trueSceneEntity,
IEntityCore falseSceneEntity)
{
return configurator.Switch(
observable,
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, trueSceneEntity),
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, falseSceneEntity));
}
// -------------------------------------------------------------------------
// SwitchWhen
// -------------------------------------------------------------------------
/// <summary>
/// Registers a node that applies a scene switch between two Home Assistant scenes when the
/// <paramref name="whenObservable"/> emits <see langword="true"/>, and passes inputs through unchanged when it emits <see langword="false"/>.
/// The <paramref name="switchObservable"/> determines which scene to apply.
/// Scenes are fetched and cached via <see cref="LightSceneCacheService"/> on first use.
/// </summary>
/// <param name="configurator">The pipeline configurator.</param>
/// <param name="whenObservable">The observable that gates the switch; when false, inputs pass through unchanged.</param>
/// <param name="switchObservable">The observable that selects which scene to apply.</param>
/// <param name="trueSceneEntity">The scene entity to apply when the switch observable emits true.</param>
/// <param name="falseSceneEntity">The scene entity to apply when the switch observable emits false.</param>
/// <returns>The configurator instance for method chaining.</returns>
public static ILightTransitionPipelineConfigurator<NetDaemonLight> SwitchWhen(
this ILightTransitionPipelineConfigurator<NetDaemonLight> configurator,
IObservable<bool> whenObservable,
IObservable<bool> switchObservable,
IEntityCore trueSceneEntity,
IEntityCore falseSceneEntity)
{
return configurator.SwitchWhen(
whenObservable,
switchObservable,
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, trueSceneEntity),
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, falseSceneEntity));
}
/// <summary>
/// Registers a node that applies a scene switch between two Home Assistant scenes when the observable of type
/// <typeparamref name="TWhenObservable"/> emits <see langword="true"/>, and passes inputs through unchanged when it emits <see langword="false"/>.
/// Both observables are resolved from the service provider.
/// Scenes are fetched and cached via <see cref="LightSceneCacheService"/> on first use.
/// </summary>
/// <typeparam name="TWhenObservable">The type of the gating observable to resolve from the service provider.</typeparam>
/// <typeparam name="TSwitchObservable">The type of the branch-selecting observable to resolve from the service provider.</typeparam>
/// <param name="configurator">The pipeline configurator.</param>
/// <param name="trueSceneEntity">The scene entity to apply when the switch observable emits true.</param>
/// <param name="falseSceneEntity">The scene entity to apply when the switch observable emits false.</param>
/// <returns>The configurator instance for method chaining.</returns>
public static ILightTransitionPipelineConfigurator<NetDaemonLight> SwitchWhen<TWhenObservable, TSwitchObservable>(
this ILightTransitionPipelineConfigurator<NetDaemonLight> configurator,
IEntityCore trueSceneEntity,
IEntityCore falseSceneEntity)
where TWhenObservable : IObservable<bool>
where TSwitchObservable : IObservable<bool>
{
return configurator.SwitchWhen<TWhenObservable, TSwitchObservable>(
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, trueSceneEntity),
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, falseSceneEntity));
}
/// <summary>
/// Registers a node that applies a scene switch between two Home Assistant scenes when the observable of type
/// <typeparamref name="TWhenObservable"/> emits <see langword="true"/>, and passes inputs through unchanged when it emits <see langword="false"/>.
/// The <paramref name="switchObservable"/> determines which scene to apply.
/// The when observable is resolved from the service provider.
/// Scenes are fetched and cached via <see cref="LightSceneCacheService"/> on first use.
/// </summary>
/// <typeparam name="TWhenObservable">The type of the gating observable to resolve from the service provider.</typeparam>
/// <param name="configurator">The pipeline configurator.</param>
/// <param name="switchObservable">The observable that selects which scene to apply.</param>
/// <param name="trueSceneEntity">The scene entity to apply when the switch observable emits true.</param>
/// <param name="falseSceneEntity">The scene entity to apply when the switch observable emits false.</param>
/// <returns>The configurator instance for method chaining.</returns>
public static ILightTransitionPipelineConfigurator<NetDaemonLight> SwitchWhen<TWhenObservable>(
this ILightTransitionPipelineConfigurator<NetDaemonLight> configurator,
IObservable<bool> switchObservable,
IEntityCore trueSceneEntity,
IEntityCore falseSceneEntity)
where TWhenObservable : IObservable<bool>
{
return configurator.SwitchWhen<TWhenObservable>(
switchObservable,
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, trueSceneEntity),
sp => SceneExtensionHelpers.GetSceneLightParameters(sp, falseSceneEntity));
}
// -------------------------------------------------------------------------
// AddToggle (with scene entries)
// -------------------------------------------------------------------------
/// <summary>
/// Adds a toggle node that cycles through one or more Home Assistant scenes when triggered by <paramref name="triggerObservable"/>.
/// Each trigger toggles to the next scene in order, wrapping back to the first after the last.
/// Scenes are fetched and cached via <see cref="LightSceneCacheService"/> on first use.
/// If the current light is not part of a scene, no state is applied for that step.
/// </summary>
/// <typeparam name="T">The type of values emitted by the trigger observable.</typeparam>
/// <param name="configurator">The pipeline configurator.</param>
/// <param name="triggerObservable">The observable that triggers toggling to the next scene.</param>
/// <param name="sceneEntities">The scene entities to toggle through.</param>
/// <returns>The configurator instance for method chaining.</returns>
public static ILightTransitionPipelineConfigurator<NetDaemonLight> AddToggle<T>(
this ILightTransitionPipelineConfigurator<NetDaemonLight> configurator,
IObservable<T> triggerObservable,
params IEntityCore[] sceneEntities)
{
return configurator.AddToggle(triggerObservable, c =>
{
foreach (var scene in sceneEntities)
c.AddScene(scene);
});
}
// -------------------------------------------------------------------------
// AddCycle (with scene entries)
// -------------------------------------------------------------------------
/// <summary>
/// Adds a cycle node that rotates through one or more Home Assistant scenes when triggered by <paramref name="triggerObservable"/>.
/// Each trigger cycles to the next scene in order.
/// Scenes are fetched and cached via <see cref="LightSceneCacheService"/> on first use.
/// If the current light is not part of a scene, no state is applied for that step.
/// </summary>
/// <typeparam name="T">The type of values emitted by the trigger observable.</typeparam>
/// <param name="configurator">The pipeline configurator.</param>
/// <param name="triggerObservable">The observable that triggers cycling to the next scene.</param>
/// <param name="sceneEntities">The scene entities to cycle through.</param>
/// <returns>The configurator instance for method chaining.</returns>
public static ILightTransitionPipelineConfigurator<NetDaemonLight> AddCycle<T>(
this ILightTransitionPipelineConfigurator<NetDaemonLight> configurator,
IObservable<T> triggerObservable,
params IEntityCore[] sceneEntities)
{
return configurator.AddCycle(triggerObservable, c =>
{
foreach (var scene in sceneEntities)
c.AddScene(scene);
});
}
}