Apply code formatting cleanup across the monitor projects

- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests
- Expand single-statement guards and loops into braced blocks
- Realign wrapped method arguments, chained calls and continuations
- Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty`
- Flatten the boiler config expression and reindent the remaining touched files
This commit is contained in:
2026-12-01 12:33:31 +02:00
parent 77458383d1
commit f919d6b25b
115 changed files with 3115 additions and 1959 deletions
@@ -1,3 +1,5 @@
#region
using FluentAssertions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
@@ -8,6 +10,8 @@ using ShowerMonitor.Models.Enums;
using ShowerMonitorManager;
using ShowerMonitorManager.Models;
#endregion
namespace ShowerMonitor.Manager.Tests;
public class ExhaustFanControlServiceTests
@@ -15,19 +19,19 @@ public class ExhaustFanControlServiceTests
private const string FanEntityId = "switch.exhaust_fan";
private readonly ShowerMonitorOptions _defaultOptions = ShowerMonitorTestData.CreateOptions();
private readonly Mock<IHaContext> _haContextMock = new();
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
private readonly FakeTimeProvider _timeProvider = ShowerMonitorTestData.CreateTimeProvider();
private DateTimeOffset Now => _timeProvider.GetUtcNow();
public static TheoryData<string, ShowerState, string?, bool> ShowerProgressFanStateCases => new()
public static TheoryData<string, ShowerState, string?, bool> ShowerProgressFanStateCases => new TheoryData<string, ShowerState, string?, bool>
{
{ "active while fan is off", ShowerState.Active, "off", true },
{ "active while fan is on", ShowerState.Active, "on", false },
{ "active while fan reports uppercase on", ShowerState.Active, "ON", false },
{ "active while fan is unavailable", ShowerState.Active, "unavailable", true },
{ "active while fan state is unknown", ShowerState.Active, "unknown", true },
{ "active while fan state is empty", ShowerState.Active, "", true },
{ "active while fan state is empty", ShowerState.Active, string.Empty, true },
{ "active while fan state is missing", ShowerState.Active, null, true },
{ "suspected while fan is off", ShowerState.Suspected, "off", true },
{ "suspected while fan is on", ShowerState.Suspected, "on", false },
@@ -36,7 +40,7 @@ public class ExhaustFanControlServiceTests
{ "completed does not turn the fan on", ShowerState.Completed, "off", false }
};
public static TheoryData<string, int, int, bool> FanRunAfterCases => new()
public static TheoryData<string, int, int, bool> FanRunAfterCases => new TheoryData<string, int, int, bool>
{
{ "well before run-after duration", 10, 60, false },
{ "one second before run-after duration", 10, 599, false },
@@ -47,7 +51,7 @@ public class ExhaustFanControlServiceTests
{ "long run-after keeps fan running", 30, 300, false }
};
public static TheoryData<string, bool, string?, bool> ResetCases => new()
public static TheoryData<string, bool, string?, bool> ResetCases => new TheoryData<string, bool, string?, bool>
{
{ "fan on after we turned it on", true, "on", true },
{ "fan reports uppercase on", true, "ON", true },
@@ -83,8 +87,10 @@ public class ExhaustFanControlServiceTests
bool expectTurnOff)
{
SetupFanState("off");
var service = CreateService(ShowerMonitorTestData.CreateOptions(
fanRunAfterShowerMinutes: runAfterMinutes));
fanRunAfterShowerMinutes: runAfterMinutes));
var showerEnd = Now.AddMinutes(5);
service.Evaluate(ShowerState.Active, Now);
@@ -252,8 +258,8 @@ public class ExhaustFanControlServiceTests
private void SetupFanState(string? state)
{
_haContextMock
.Setup(x => x.GetState(_defaultOptions.ExhaustFanEntityId))
.Returns(ShowerMonitorTestData.FanState(state));
.Setup(x => x.GetState(_defaultOptions.ExhaustFanEntityId))
.Returns(ShowerMonitorTestData.FanState(state));
}
private void VerifySwitchCall(
@@ -263,18 +269,15 @@ public class ExhaustFanControlServiceTests
string? because = null)
{
var targets = _haContextMock.Invocations
.Where(invocation => invocation.Method.Name == nameof(IHaContext.CallService)
&& (string?)invocation.Arguments[0] == "switch"
&& (string?)invocation.Arguments[1] == service)
.Select(invocation => (ServiceTarget?)invocation.Arguments[2])
.ToList();
.Where(invocation => invocation.Method.Name == nameof(IHaContext.CallService) && (string?)invocation.Arguments[0] == "switch" && (string?)invocation.Arguments[1] == service)
.Select(invocation => (ServiceTarget?)invocation.Arguments[2])
.ToList();
targets.Should().HaveCount(expectedCount, because);
targets.Should().OnlyContain(
target => target != null
&& target.EntityIds != null
&& target.EntityIds.Count == 1
&& target.EntityIds.Single() == expectedEntityId,
because);
targets.Should()
.OnlyContain(
target => target != null && target.EntityIds != null && target.EntityIds.Count == 1 && target.EntityIds.Single() == expectedEntityId,
because);
}
}
@@ -1,8 +1,12 @@
#region
using FluentAssertions;
using Microsoft.Extensions.Time.Testing;
using ShowerMonitor.Models;
using ShowerMonitorManager;
#endregion
namespace ShowerMonitor.Manager.Tests;
public class HumidityRateCalculatorTests
@@ -13,7 +17,7 @@ public class HumidityRateCalculatorTests
private DateTimeOffset Now => _timeProvider.GetUtcNow();
public static TheoryData<string, int, double[], int[], double> RateCases => new()
public static TheoryData<string, int, double[], int[], double> RateCases => new TheoryData<string, int, double[], int[], double>
{
{ "no readings", 5, [], [], 0.0 },
{ "single reading", 5, [52.9], [0], 0.0 },
@@ -49,7 +53,9 @@ public class HumidityRateCalculatorTests
var calculator = new HumidityRateCalculator(windowSize);
for (var i = 0; i < humidities.Length; i++)
{
calculator.RecordReading(humidities[i], Now.AddSeconds(offsetSeconds[i]));
}
calculator.GetRatePerMinute().Should().BeApproximately(expectedRate, 1e-6, $"scenario: {scenario}");
}
@@ -58,20 +64,24 @@ public class HumidityRateCalculatorTests
public void GetRatePerMinute_WithGeneratedLinearSeries_ReturnsSlopeForEveryWindowSize()
{
const double ratePerMinute = 2.5;
var series = ShowerMonitorTestData.CreateLinearHumiditySeries(
45.0,
ratePerMinute,
12);
45.0,
ratePerMinute,
12);
for (var windowSize = 2; windowSize <= series.Count; windowSize++)
{
var calculator = new HumidityRateCalculator(windowSize);
foreach (var sample in series) calculator.RecordReading(sample.Humidity, Now + sample.Offset);
foreach (var sample in series)
{
calculator.RecordReading(sample.Humidity, Now + sample.Offset);
}
calculator.GetRatePerMinute()
.Should()
.BeApproximately(ratePerMinute, 1e-9, $"window size {windowSize}");
.Should()
.BeApproximately(ratePerMinute, 1e-9, $"window size {windowSize}");
}
}
@@ -1,3 +1,5 @@
#region
using FluentAssertions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
@@ -6,6 +8,8 @@ using ShowerMonitorManager;
using ShowerMonitorManager.Interface;
using ShowerMonitorManager.Models;
#endregion
namespace ShowerMonitor.Manager.Tests;
public class ShowerDetectionServiceTests
@@ -15,7 +19,7 @@ public class ShowerDetectionServiceTests
private DateTimeOffset Now => _timeProvider.GetUtcNow();
public static TheoryData<string, double, double, ShowerState> IdleThresholdCases => new()
public static TheoryData<string, double, double, ShowerState> IdleThresholdCases => new TheoryData<string, double, double, ShowerState>
{
{ "both thresholds met exactly", 52.0, 44.0, ShowerState.Suspected },
{ "rate one step under threshold", 51.98, 44.0, ShowerState.Idle },
@@ -27,7 +31,7 @@ public class ShowerDetectionServiceTests
{ "very large humidity spike", 1000.0, 500.0, ShowerState.Suspected }
};
public static TheoryData<string, double, double, int, ShowerState> ConfirmationCases => new()
public static TheoryData<string, double, double, int, ShowerState> ConfirmationCases => new TheoryData<string, double, double, int, ShowerState>
{
{ "confirmation window reached", 66.0, 49.5, 120, ShowerState.Active },
{ "confirmation window not yet reached", 66.0, 49.5, 119, ShowerState.Suspected },
@@ -39,14 +43,14 @@ public class ShowerDetectionServiceTests
{ "very large humidity at confirmation", 1000.0, 900.0, 120, ShowerState.Active }
};
public static TheoryData<string, double, ShowerState> RecoveryBoundaryCases => new()
public static TheoryData<string, double, ShowerState> RecoveryBoundaryCases => new TheoryData<string, double, ShowerState>
{
{ "differential well under threshold", 52.0, ShowerState.Completed },
{ "differential exactly at threshold", 50.0, ShowerState.Completed },
{ "differential just above threshold", 49.98, ShowerState.Declining }
};
public static TheoryData<string, int, ShowerState> MaxDurationCases => new()
public static TheoryData<string, int, ShowerState> MaxDurationCases => new TheoryData<string, int, ShowerState>
{
{ "one second before max duration", 3_599, ShowerState.Active },
{ "exactly at max duration", 3_600, ShowerState.Declining },
@@ -149,16 +153,19 @@ public class ShowerDetectionServiceTests
EvaluateBaselineAndSpike(service, start);
var state = service.Evaluate(
bathroomHumidity,
mainHomeHumidity,
start.AddSeconds(30 + secondsAfterSpike));
bathroomHumidity,
mainHomeHumidity,
start.AddSeconds(30 + secondsAfterSpike));
state.Should().Be(expectedState, $"scenario: {scenario}");
service.CurrentState.Should().Be(expectedState, $"scenario: {scenario}");
service.TodaySessionCount.Should().Be(0);
service.LastCompletedSession.Should().BeNull();
if (expectedState == ShowerState.Active) service.CurrentSession.Should().NotBeNull();
if (expectedState == ShowerState.Active)
{
service.CurrentSession.Should().NotBeNull();
}
}
[Fact]
@@ -1,8 +1,12 @@
#region
using Microsoft.Extensions.Time.Testing;
using NetDaemon.HassModel.Entities;
using ShowerMonitor.Models;
using ShowerMonitorManager.Models;
#endregion
namespace ShowerMonitor.Manager.Tests;
/// <summary>
@@ -13,7 +17,7 @@ namespace ShowerMonitor.Manager.Tests;
internal static class ShowerMonitorTestData
{
/// <summary>Fixed base time used by all timestamp-sensitive tests.</summary>
private static readonly DateTimeOffset BaseTimestamp = new(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
private static readonly DateTimeOffset BaseTimestamp = new DateTimeOffset(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
/// <summary>Creates a fake clock positioned at <see cref="BaseTimestamp" /> unless overridden.</summary>
public static FakeTimeProvider CreateTimeProvider(DateTimeOffset? start = null)
@@ -39,15 +43,13 @@ internal static class ShowerMonitorTestData
return new ShowerMonitorOptions
{
ExhaustFanEntityId = exhaustFanEntityId,
FanRunAfterShowerMinutes = fanRunAfterShowerMinutes
?? (int)ShowerMonitorConsts.DefaultFanRunAfterShower.TotalMinutes,
FanRunAfterShowerMinutes = fanRunAfterShowerMinutes ?? (int)ShowerMonitorConsts.DefaultFanRunAfterShower.TotalMinutes,
SpikeRateThreshold = spikeRateThreshold,
MinDifferentialHumidity = minDifferentialHumidity,
PeakHumidityThreshold = peakHumidityThreshold,
ConfirmationWindowMinutes = confirmationWindowMinutes,
RecoveryDifferentialThreshold = recoveryDifferentialThreshold,
MaxShowerDurationMinutes = maxShowerDurationMinutes
?? (int)ShowerMonitorConsts.DefaultMaxShowerDuration.TotalMinutes,
MaxShowerDurationMinutes = maxShowerDurationMinutes ?? (int)ShowerMonitorConsts.DefaultMaxShowerDuration.TotalMinutes,
RateWindowSize = rateWindowSize
};
}
@@ -81,9 +83,9 @@ internal static class ShowerMonitorTestData
return
[
.. Enumerable.Range(0, sampleCount)
.Select(i => new HumiditySample(
startHumidity + ratePerMinute * i * intervalSeconds / 60.0,
TimeSpan.FromSeconds(i * intervalSeconds)))
.Select(i => new HumiditySample(
startHumidity + ratePerMinute * i * intervalSeconds / 60.0,
TimeSpan.FromSeconds(i * intervalSeconds)))
];
}