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 LightMonitor.Models;
using LightMonitorManager;
using Microsoft.Extensions.Logging;
@@ -6,6 +8,8 @@ using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
#endregion
namespace LightMonitor.Manager.Tests;
public class LightControlServiceTests
@@ -15,20 +19,20 @@ public class LightControlServiceTests
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private readonly Mock<IHaContext> _haContextMock = new();
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
private LightControlService CreateService(LightMonitorOptions? options = null)
{
return new LightControlService(
_haContextMock.Object,
Mock.Of<ILogger<LightControlService>>(),
Options.Create(options ?? _defaultOptions));
_haContextMock.Object,
Mock.Of<ILogger<LightControlService>>(),
Options.Create(options ?? _defaultOptions));
}
private void SetupState(string entityId, string? state)
{
_haContextMock.Setup(x => x.GetState(entityId))
.Returns(LightMonitorTestData.CreateOptionalState(state));
.Returns(LightMonitorTestData.CreateOptionalState(state));
}
private void VerifyTurnOn(string entityId, bool shouldTurnOn)
@@ -36,17 +40,17 @@ public class LightControlServiceTests
if (!shouldTurnOn)
{
_haContextMock.Verify(x => x.CallService(
It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
Times.Never);
It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
Times.Never);
return;
}
_haContextMock.Verify(x => x.CallService("light", "turn_on",
It.Is<ServiceTarget>(target => target.EntityIds != null && target.EntityIds.Contains(entityId)),
It.IsAny<object?>()),
Times.Once);
It.Is<ServiceTarget>(target => target.EntityIds != null && target.EntityIds.Contains(entityId)),
It.IsAny<object?>()),
Times.Once);
}
[Theory]
@@ -1,3 +1,5 @@
#region
using System.Reactive.Subjects;
using FluentAssertions;
using LightMonitor.Models;
@@ -9,6 +11,8 @@ using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
#endregion
namespace LightMonitor.Manager.Tests;
public class LightMonitorInitTests
@@ -18,30 +22,30 @@ public class LightMonitorInitTests
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private readonly Mock<IHaContext> _haContextMock = new();
private readonly Mock<ILightControlService> _lightControlServiceMock = new();
private readonly Mock<IPresenceService> _presenceServiceMock = new();
private readonly Subject<StateChange> _stateChangesSubject = new();
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
private readonly Mock<ILightControlService> _lightControlServiceMock = new Mock<ILightControlService>();
private readonly Mock<IPresenceService> _presenceServiceMock = new Mock<IPresenceService>();
private readonly Subject<StateChange> _stateChangesSubject = new Subject<StateChange>();
private LightMonitorInit CreateSut(LightMonitorOptions? options = null, Exception? subscriptionException = null)
{
if (subscriptionException is null)
{
_haContextMock.Setup(x => x.StateAllChanges())
.Returns(_stateChangesSubject);
.Returns(_stateChangesSubject);
}
else
{
_haContextMock.Setup(x => x.StateAllChanges())
.Throws(subscriptionException);
.Throws(subscriptionException);
}
return new LightMonitorInit(
_haContextMock.Object,
Mock.Of<ILogger<LightMonitorInit>>(),
_presenceServiceMock.Object,
_lightControlServiceMock.Object,
Options.Create(options ?? _defaultOptions));
_haContextMock.Object,
Mock.Of<ILogger<LightMonitorInit>>(),
_presenceServiceMock.Object,
_lightControlServiceMock.Object,
Options.Create(options ?? _defaultOptions));
}
private void EmitStateChange(string entityId, string? oldState, string? newState)
@@ -49,9 +53,9 @@ public class LightMonitorInitTests
var entity = new Entity(_haContextMock.Object, entityId);
_stateChangesSubject.OnNext(new StateChange(
entity,
LightMonitorTestData.CreateOptionalState(oldState),
LightMonitorTestData.CreateOptionalState(newState)));
entity,
LightMonitorTestData.CreateOptionalState(oldState),
LightMonitorTestData.CreateOptionalState(newState)));
}
private void VerifyTurnOn(bool shouldTurnOn)
@@ -107,9 +111,9 @@ public class LightMonitorInitTests
CreateSut(LightMonitorTestData.CreateOptions(personEntityIds: [PersonEntityId]));
EmitStateChange(
LightMonitorTestData.SecondaryPersonEntityId,
"not_home",
LightMonitorConsts.PersonHomeState);
LightMonitorTestData.SecondaryPersonEntityId,
"not_home",
LightMonitorConsts.PersonHomeState);
VerifyTurnOn(false);
}
@@ -180,14 +184,15 @@ public class LightMonitorInitTests
public void SunStateChange_WhenPresenceCheckThrows_ShouldKeepListening()
{
_presenceServiceMock.SetupSequence(x => x.IsAnyoneHome())
.Throws(new InvalidOperationException("presence failed"))
.Returns(true);
.Throws(new InvalidOperationException("presence failed"))
.Returns(true);
CreateSut();
var act = () => EmitStateChange(
SunEntityId,
LightMonitorConsts.SunAboveHorizon,
LightMonitorConsts.SunBelowHorizon);
SunEntityId,
LightMonitorConsts.SunAboveHorizon,
LightMonitorConsts.SunBelowHorizon);
act.Should().NotThrow();
VerifyTurnOn(false);
@@ -200,8 +205,9 @@ public class LightMonitorInitTests
public void PersonStateChange_WhenTurnOnLightThrows_ShouldKeepListening()
{
_lightControlServiceMock.SetupSequence(x => x.TurnOnLight())
.Throws(new InvalidOperationException("light failed"))
.Pass();
.Throws(new InvalidOperationException("light failed"))
.Pass();
CreateSut();
var act = () => EmitStateChange(PersonEntityId, "not_home", LightMonitorConsts.PersonHomeState);
@@ -238,7 +244,7 @@ public class LightMonitorInitTests
CreateSut();
EmitStateChange("sensor.sun_next_dawn", LightMonitorConsts.UnavailableState,
LightMonitorConsts.SunBelowHorizon);
LightMonitorConsts.SunBelowHorizon);
VerifyTurnOn(false);
}
@@ -1,6 +1,10 @@
#region
using LightMonitor.Models;
using NetDaemon.HassModel.Entities;
#endregion
namespace LightMonitor.Manager.Tests;
/// <summary>
@@ -37,7 +41,7 @@ internal static class LightMonitorTestData
/// "below_horizon" is compared case-insensitively; unavailable, unknown, blank and
/// missing states must never turn the light on.
/// </summary>
public static TheoryData<string?, bool> SunStateTurnOnCases { get; } = new()
public static TheoryData<string?, bool> SunStateTurnOnCases { get; } = new TheoryData<string?, bool>
{
{ LightMonitorConsts.SunBelowHorizon, true },
{ LightMonitorConsts.SunBelowHorizon.ToUpperInvariant(), true },
@@ -55,7 +59,7 @@ internal static class LightMonitorTestData
/// Light states evaluated while the sun is below the horizon. Only "on" (any casing)
/// suppresses the turn on call; everything else, including missing state, turns it on.
/// </summary>
public static TheoryData<string?, bool> LightStateTurnOnCases { get; } = new()
public static TheoryData<string?, bool> LightStateTurnOnCases { get; } = new TheoryData<string?, bool>
{
{ LightMonitorConsts.LightOnState, false },
{ LightMonitorConsts.LightOnState.ToUpperInvariant(), false },
@@ -73,7 +77,7 @@ internal static class LightMonitorTestData
/// Person states checked against the "arrived home" transition.
/// Only "home" in any casing counts.
/// </summary>
public static TheoryData<string?, bool> PersonNewStateCases { get; } = new()
public static TheoryData<string?, bool> PersonNewStateCases { get; } = new TheoryData<string?, bool>
{
{ LightMonitorConsts.PersonHomeState, true },
{ "HOME", true },
@@ -92,7 +96,7 @@ internal static class LightMonitorTestData
/// Old person states for a transition to "home". Recovery from unavailable, unknown or
/// missing state must be ignored; every other old state is a real arrival.
/// </summary>
public static TheoryData<string?, bool> PersonArrivalTransitions { get; } = new()
public static TheoryData<string?, bool> PersonArrivalTransitions { get; } = new TheoryData<string?, bool>
{
{ "not_home", true },
{ "work", true },
@@ -106,7 +110,7 @@ internal static class LightMonitorTestData
/// Old sun states for a transition to "below_horizon". Recovery from unavailable, unknown
/// or missing state must be ignored.
/// </summary>
public static TheoryData<string?, bool> SunBelowHorizonTransitions { get; } = new()
public static TheoryData<string?, bool> SunBelowHorizonTransitions { get; } = new TheoryData<string?, bool>
{
{ LightMonitorConsts.SunAboveHorizon, true },
{ string.Empty, true },
@@ -171,9 +175,15 @@ internal static class LightMonitorTestData
var matrix = new TheoryData<string?[]>();
foreach (var first in PresenceStates)
foreach (var second in PresenceStates)
foreach (var third in PresenceStates)
matrix.Add([first, second, third]);
{
foreach (var second in PresenceStates)
{
foreach (var third in PresenceStates)
{
matrix.Add([first, second, third]);
}
}
}
return matrix;
}
@@ -1,3 +1,5 @@
#region
using FluentAssertions;
using LightMonitor.Models;
using LightMonitorManager;
@@ -6,19 +8,21 @@ using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
#endregion
namespace LightMonitor.Manager.Tests;
public class PresenceServiceTests
{
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private readonly Mock<IHaContext> _haContextMock = new();
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
private PresenceService CreateService(LightMonitorOptions? options = null)
{
return new PresenceService(
_haContextMock.Object,
Mock.Of<ILogger<PresenceService>>(),
Options.Create(options ?? _defaultOptions));
_haContextMock.Object,
Mock.Of<ILogger<PresenceService>>(),
Options.Create(options ?? _defaultOptions));
}
private void SetupStates(string?[] personStates)
@@ -29,7 +33,7 @@ public class PresenceServiceTests
var state = LightMonitorTestData.CreateOptionalState(personStates[index]);
_haContextMock.Setup(x => x.GetState(entityId))
.Returns(state);
.Returns(state);
}
}
@@ -40,7 +44,7 @@ public class PresenceServiceTests
SetupStates(personStates);
var expected = personStates.Any(state =>
string.Equals(state, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase));
string.Equals(state, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase));
CreateService().IsAnyoneHome().Should().Be(expected);
}
@@ -50,10 +54,10 @@ public class PresenceServiceTests
public void IsAnyoneHome_ShouldOnlyTreatHomeStateAsPresent(string? state, bool expected)
{
_haContextMock.Setup(x => x.GetState(LightMonitorTestData.PrimaryPersonEntityId))
.Returns(LightMonitorTestData.CreateOptionalState(state));
.Returns(LightMonitorTestData.CreateOptionalState(state));
var options = LightMonitorTestData.CreateOptions(
personEntityIds: [LightMonitorTestData.PrimaryPersonEntityId]);
personEntityIds: [LightMonitorTestData.PrimaryPersonEntityId]);
CreateService(options).IsAnyoneHome().Should().Be(expected);
}
@@ -81,7 +85,7 @@ public class PresenceServiceTests
public void IsAnyoneHome_ShouldStopCheckingAfterTheFirstHomePerson()
{
_haContextMock.Setup(x => x.GetState(LightMonitorTestData.PrimaryPersonEntityId))
.Returns(LightMonitorTestData.CreateState(LightMonitorConsts.PersonHomeState));
.Returns(LightMonitorTestData.CreateState(LightMonitorConsts.PersonHomeState));
CreateService().IsAnyoneHome().Should().BeTrue();
@@ -95,17 +99,19 @@ public class PresenceServiceTests
CreateService().IsAnyoneHome().Should().BeFalse();
foreach (var personEntityId in LightMonitorTestData.PersonEntityIds)
{
_haContextMock.Verify(x => x.GetState(personEntityId), Times.Once);
}
}
[Fact]
public void IsAnyoneHome_ShouldIgnorePersonsThatAreNotConfigured()
{
_haContextMock.Setup(x => x.GetState(LightMonitorTestData.SecondaryPersonEntityId))
.Returns(LightMonitorTestData.CreateState(LightMonitorConsts.PersonHomeState));
.Returns(LightMonitorTestData.CreateState(LightMonitorConsts.PersonHomeState));
var options = LightMonitorTestData.CreateOptions(
personEntityIds: [LightMonitorTestData.PrimaryPersonEntityId]);
personEntityIds: [LightMonitorTestData.PrimaryPersonEntityId]);
CreateService(options).IsAnyoneHome().Should().BeFalse();
@@ -120,6 +126,8 @@ public class PresenceServiceTests
service.IsAnyoneHome().Should().BeFalse();
foreach (var personEntityId in LightMonitorConsts.DefaultPersonEntityIds)
{
_haContextMock.Verify(x => x.GetState(personEntityId), Times.Once);
}
}
}