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 System.Globalization;
using FluentAssertions;
using Microsoft.Extensions.Logging;
@@ -9,6 +11,8 @@ using NetDaemon.HassModel.Entities;
using PowerMonitorManager;
using PowerMonitorManager.Models;
#endregion
namespace PowerMonitor.Manager.Tests;
public class PowerLimitEnforcementServiceTests
@@ -16,53 +20,53 @@ public class PowerLimitEnforcementServiceTests
private const string BoilerSwitch = PowerMonitorTestData.BoilerSwitchEntityId;
private const string AutoManagement = PowerMonitorTestData.AutoManagementEntityId;
private readonly Mock<IHaContext> _haContextMock = new();
private readonly Mock<ILogger<PowerLimitEnforcementService>> _loggerMock = new();
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
private readonly Mock<ILogger<PowerLimitEnforcementService>> _loggerMock = new Mock<ILogger<PowerLimitEnforcementService>>();
private readonly PowerMonitorOptions _options = PowerMonitorTestData.CreateLimitEnforcementOptions();
private readonly FakeTimeProvider _timeProvider = new(PowerMonitorTestData.FixedUtcNow);
private readonly FakeTimeProvider _timeProvider = new FakeTimeProvider(PowerMonitorTestData.FixedUtcNow);
private PowerLimitEnforcementService CreateSut()
{
return new PowerLimitEnforcementService(
_haContextMock.Object,
_loggerMock.Object,
Options.Create(_options),
_timeProvider);
_haContextMock.Object,
_loggerMock.Object,
Options.Create(_options),
_timeProvider);
}
private void VerifyNoServiceCalls()
{
_haContextMock.Verify(
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Never);
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Never);
}
private void VerifyTurnOff(string entityId, int expectedCalls)
{
_haContextMock.Verify(
x => x.CallService("switch", "turn_off", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(entityId)), null),
Times.Exactly(expectedCalls));
x => x.CallService("switch", "turn_off", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(entityId)), null),
Times.Exactly(expectedCalls));
}
private void VerifyTurnOn(string entityId, int expectedCalls)
{
_haContextMock.Verify(
x => x.CallService("switch", "turn_on", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(entityId)), null),
Times.Exactly(expectedCalls));
x => x.CallService("switch", "turn_on", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(entityId)), null),
Times.Exactly(expectedCalls));
}
private void VerifyNotificationCreated(int expectedCalls)
{
_haContextMock.Verify(
x => x.CallService("persistent_notification", "create", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Exactly(expectedCalls));
x => x.CallService("persistent_notification", "create", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Exactly(expectedCalls));
}
private void VerifyNotificationDismissed(int expectedCalls)
{
_haContextMock.Verify(
x => x.CallService("persistent_notification", "dismiss", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Exactly(expectedCalls));
x => x.CallService("persistent_notification", "dismiss", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Exactly(expectedCalls));
}
#region EnforceLimit
@@ -131,11 +135,13 @@ public class PowerLimitEnforcementServiceTests
{
// Arrange
var calls = new List<(string domain, string service, string? entityId)>();
_haContextMock
.Setup(x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget>(),
It.IsAny<object?>()))
.Callback<string, string, ServiceTarget?, object?>((d, s, t, _) =>
calls.Add((d, s, t?.EntityIds?.FirstOrDefault())));
.Setup(x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget>(),
It.IsAny<object?>()))
.Callback<string, string, ServiceTarget?, object?>((d, s, t, _) =>
calls.Add((d, s, t?.EntityIds?.FirstOrDefault())));
var sut = CreateSut();
// Act
@@ -144,8 +150,12 @@ public class PowerLimitEnforcementServiceTests
// Assert
var turnOffCalls = calls.Where(c => c.service == "turn_off").ToList();
turnOffCalls.Should().HaveCount(2);
turnOffCalls[0].entityId.Should().Be(BoilerSwitch,
"boiler switch must be turned off before auto_management (race-safe order)");
turnOffCalls[0]
.entityId.Should()
.Be(BoilerSwitch,
"boiler switch must be turned off before auto_management (race-safe order)");
turnOffCalls[1].entityId.Should().Be(AutoManagement);
}
@@ -156,10 +166,12 @@ public class PowerLimitEnforcementServiceTests
_options.EnforceSwitchOffEntityIds = ["switch.high_load_a", "switch.high_load_b"];
_options.PauseAndRestoreEntityIds = ["switch.monitor_c"];
var calls = new List<string?>();
_haContextMock
.Setup(x => x.CallService(It.IsAny<string>(), "turn_off", It.IsAny<ServiceTarget>(), It.IsAny<object?>()))
.Callback<string, string, ServiceTarget?, object?>((_, _, target, _) =>
calls.Add(target?.EntityIds?.FirstOrDefault()));
.Setup(x => x.CallService(It.IsAny<string>(), "turn_off", It.IsAny<ServiceTarget>(), It.IsAny<object?>()))
.Callback<string, string, ServiceTarget?, object?>((_, _, target, _) =>
calls.Add(target?.EntityIds?.FirstOrDefault()));
var sut = CreateSut();
// Act
@@ -184,16 +196,17 @@ public class PowerLimitEnforcementServiceTests
// Assert
sut.IsLimitActive.Should().BeTrue();
VerifyNotificationCreated(1);
_haContextMock.Verify(
x => x.CallService("switch", It.IsAny<string>(), It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Never);
x => x.CallService("switch", It.IsAny<string>(), It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Never);
}
#endregion
#region Recovery
public static TheoryData<string, int, double, bool, int> RecoveryCases => new()
public static TheoryData<string, int, double, bool, int> RecoveryCases => new TheoryData<string, int, double, bool, int>
{
{ "not yet due", 300, 3000.0, true, 0 },
{ "one second before due", 599, 3000.0, true, 0 },
@@ -268,7 +281,7 @@ public class PowerLimitEnforcementServiceTests
[InlineData(31, true)]
[InlineData(600, true)]
public void EnforceLimit_AfterRecovery_ShouldReEnforceOnlyOnceCooldownElapsed(int secondsAfterRecovery,
bool expectedLimitActive)
bool expectedLimitActive)
{
// Arrange
var sut = CreateSut();
@@ -379,9 +392,11 @@ public class PowerLimitEnforcementServiceTests
// Assert
sut.IsLimitActive.Should().BeTrue();
_haContextMock.Verify(
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Exactly(3));
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
Times.Exactly(3));
VerifyTurnOff(BoilerSwitch, 1);
VerifyTurnOff(AutoManagement, 1);
VerifyNotificationCreated(1);