Skip to content

Commit 45df23c

Browse files
authored
Fixed bug in CombineWithBrightness (#214)
* Fixed naming issues. * Fixed bug in CombineWithBrightness
1 parent afab80e commit 45df23c

6 files changed

Lines changed: 48 additions & 52 deletions

File tree

src/CodeCasa.AutomationPipelines.Lights/Nodes/ManagedNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public TState? Input
2222
public TState? Output => innerNode.Output;
2323
public IObservable<TState?> OnNewOutput => innerNode.OnNewOutput;
2424

25-
public override string? ToString() => $"{innerNode} (scoped)";
25+
public override string ToString() => $"{innerNode} (scoped)";
2626

2727
public async ValueTask DisposeAsync()
2828
{

src/CodeCasa.AutomationPipelines.Lights/Pipeline/LightTransitionPipelineConfigurator.When.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public ILightTransitionPipelineConfigurator<TLight> AddPipelineWhen<TObservable>
130130
}
131131
return When<TObservable>(c =>
132132
c.GetRequiredService<LightPipelineFactory>()
133-
.CreateLightPipeline(c.GetRequiredService<TLight>(), pipelineConfigurator.ApplyHierarchySettings(HierarchyPath, LoggingEnabled ?? false)));
133+
.CreateLightPipeline(c.GetRequiredService<TLight>(), pipelineConfigurator.ApplyHierarchySettings($"{HierarchyPath}->Condition", LoggingEnabled ?? false)));
134134
}
135135

136136
/// <inheritdoc/>
@@ -144,7 +144,7 @@ public ILightTransitionPipelineConfigurator<TLight> AddPipelineWhen(IObservable<
144144
}
145145
return When(observable, c =>
146146
c.GetRequiredService<LightPipelineFactory>()
147-
.CreateLightPipeline(c.GetRequiredService<TLight>(), pipelineConfigurator.ApplyHierarchySettings(HierarchyPath, LoggingEnabled ?? false)));
147+
.CreateLightPipeline(c.GetRequiredService<TLight>(), pipelineConfigurator.ApplyHierarchySettings($"{HierarchyPath}->Condition", LoggingEnabled ?? false)));
148148
}
149149

150150
/// <inheritdoc/>

src/CodeCasa.AutomationPipelines.Lights/Pipeline/ManagedPipeline.cs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,6 @@ internal sealed class ManagedPipeline<TNode>(IServiceScope scope, IPipeline<TNod
99
private readonly IServiceScope _scope = scope ?? throw new ArgumentNullException(nameof(scope));
1010
private readonly IPipeline<TNode> _instance = pipeline ?? throw new ArgumentNullException(nameof(pipeline));
1111

12-
public void Dispose()
13-
{
14-
foreach (var subscription in pipelineSubscriptions)
15-
{
16-
subscription.Dispose();
17-
}
18-
(_instance as IDisposable)?.Dispose();
19-
_scope.Dispose();
20-
}
21-
22-
public async ValueTask DisposeAsync()
23-
{
24-
foreach (var subscription in pipelineSubscriptions)
25-
{
26-
subscription.Dispose();
27-
}
28-
await _instance.DisposeOrDisposeAsync();
29-
await _scope.DisposeOrDisposeAsync();
30-
}
31-
3212
public Guid Id => pipeline.Id;
3313
public string? Name
3414
{
@@ -73,4 +53,29 @@ public IPipeline<TNode> SetOutputHandler(Action<TNode> action, IEqualityComparer
7353
public IReadOnlyCollection<IPipelineNode<TNode>> Nodes => _instance.Nodes;
7454

7555
public IObservable<PipelineTelemetry<TNode>> Telemetry => _instance.Telemetry;
56+
57+
public override string ToString()
58+
{
59+
return $"{pipeline} (scoped)";
60+
}
61+
62+
public void Dispose()
63+
{
64+
foreach (var subscription in pipelineSubscriptions)
65+
{
66+
subscription.Dispose();
67+
}
68+
(_instance as IDisposable)?.Dispose();
69+
_scope.Dispose();
70+
}
71+
72+
public async ValueTask DisposeAsync()
73+
{
74+
foreach (var subscription in pipelineSubscriptions)
75+
{
76+
subscription.Dispose();
77+
}
78+
await _instance.DisposeOrDisposeAsync();
79+
await _scope.DisposeOrDisposeAsync();
80+
}
7681
}

src/CodeCasa.AutomationPipelines/Pipeline.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public class Pipeline<TState> : PipelineNode<TState>, IPipeline<TState>
1717
private Action<TState>? _action;
1818
private IDisposable? _subscription;
1919
private bool _isDisposed;
20-
private string? _name;
2120

2221
/// <summary>
2322
/// Initializes a new, empty pipeline with no nodes.
@@ -81,18 +80,6 @@ public Pipeline(TState defaultState, params IPipelineNode<TState>[] nodes)
8180
/// <inheritdoc />
8281
public IObservable<PipelineTelemetry<TState>> Telemetry => _telemetrySubject.AsObservable();
8382

84-
/// <summary>
85-
/// Sets an optional name for this pipeline, used in ToString() for telemetry and logging.
86-
/// </summary>
87-
public IPipeline<TState> SetName(string name)
88-
{
89-
_name = name;
90-
return this;
91-
}
92-
93-
/// <inheritdoc />
94-
public override string ToString() => _name ?? base.ToString() ?? GetType().Name;
95-
9683
/// <inheritdoc />
9784
public IPipeline<TState> SetDefault(TState state)
9885
{

src/CodeCasa.AutomationPipelines/PipelineNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void SetOutputInternal(TState? output)
124124
}
125125

126126
/// <inheritdoc />
127-
public override string ToString() => GetType().Name;
127+
public override string ToString() => Name ?? GetType().Name;
128128

129129
/// <inheritdoc />
130130
public virtual ValueTask DisposeAsync()

src/CodeCasa.NetDaemon.Sensors.Composite/Extensions/BooleanObservableExtensions.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,32 @@ public static IObservable<bool> PersistTrue(
4343

4444
public static IObservable<bool> CombineWithBrightness(this IObservable<bool> motion, IObservable<bool> brightnessLessThanThreshold)
4545
{
46-
bool? triggered = null;
47-
return motion.CombineLatest(brightnessLessThanThreshold, (motionTriggered, brightnessTriggered) =>
46+
return Observable.Create<bool>(observer =>
4847
{
49-
if (motionTriggered && brightnessTriggered)
48+
bool? triggered = null;
49+
return motion.CombineLatest(brightnessLessThanThreshold, (motionTriggered, brightnessTriggered) =>
5050
{
51-
triggered = true;
52-
}
53-
else {
54-
if (triggered == false)
51+
if (motionTriggered && brightnessTriggered)
5552
{
56-
// Prevent duplicate false emissions.
57-
return null;
53+
triggered = true;
5854
}
59-
60-
if (!motionTriggered)
55+
else
6156
{
62-
triggered = false;
57+
if (triggered == false)
58+
{
59+
// Prevent duplicate false emissions.
60+
return null;
61+
}
62+
63+
if (!motionTriggered)
64+
{
65+
triggered = false;
66+
}
6367
}
64-
}
6568

66-
return triggered;
67-
}).Where(b => b != null).Select(b => b!.Value);
69+
return triggered;
70+
}).Where(b => b != null).Select(b => b!.Value).Subscribe(observer);
71+
});
6872
}
6973
}
7074
}

0 commit comments

Comments
 (0)