Apply code formatting cleanup across monitor projects

- Reindent appsettings.json and the MSBuild project files
- Collapse single-statement guard blocks and tidy using and member order
- Normalize XML doc indentation and file endings
- Reorder BatteryMonitor test-data helpers and move the exclusion tests into
  their own region
- Carry the same formatting through the boiler config, control, entity and
  test files
This commit is contained in:
2026-11-30 11:52:06 +02:00
parent cf8c9434c8
commit 24729ee267
165 changed files with 2513 additions and 2470 deletions
@@ -1,10 +1,10 @@
using LightMonitor.Models;
using LightMonitorManager;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using LightMonitorManager;
namespace LightMonitor.Manager.Tests;
@@ -79,7 +79,7 @@ public class LightControlServiceTests
public void TurnOnLight_ShouldTargetTheConfiguredLightEntity()
{
const string customLightEntityId = "light.kitchen_ceiling";
var options = LightMonitorTestData.CreateOptions(lightEntityId: customLightEntityId);
var options = LightMonitorTestData.CreateOptions(customLightEntityId);
SetupState(SunEntityId, LightMonitorConsts.SunBelowHorizon);
SetupState(customLightEntityId, "off");
@@ -137,4 +137,4 @@ public class LightControlServiceTests
_haContextMock.Verify(x => x.GetState(LightEntityId), Times.Once);
VerifyTurnOn(LightEntityId, false);
}
}
}
@@ -1,34 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="10.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="8.11.0" />
<PackageReference Include="Moq" Version="4.21.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="10.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0"/>
<PackageReference Include="xunit" Version="2.9.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="8.11.0"/>
<PackageReference Include="Moq" Version="4.21.0"/>
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LightMonitor.Manager\LightMonitor.Manager.csproj" />
<ProjectReference Include="..\LightMonitor.Models\LightMonitor.Models.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LightMonitor.Manager\LightMonitor.Manager.csproj"/>
<ProjectReference Include="..\LightMonitor.Models\LightMonitor.Models.csproj"/>
</ItemGroup>
</Project>
@@ -1,13 +1,13 @@
using System.Reactive.Subjects;
using FluentAssertions;
using LightMonitor.Models;
using LightMonitorManager;
using LightMonitorManager.Interface;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using LightMonitorManager;
using LightMonitorManager.Interface;
namespace LightMonitor.Manager.Tests;
@@ -16,25 +16,21 @@ public class LightMonitorInitTests
private const string SunEntityId = LightMonitorTestData.SunEntityId;
private const string PersonEntityId = LightMonitorTestData.PrimaryPersonEntityId;
private readonly Mock<IHaContext> _haContextMock = new();
private readonly Mock<IPresenceService> _presenceServiceMock = new();
private readonly Mock<ILightControlService> _lightControlServiceMock = new();
private readonly Subject<StateChange> _stateChangesSubject = new();
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 LightMonitorInit CreateSut(LightMonitorOptions? options = null, Exception? subscriptionException = null)
{
if (subscriptionException is null)
{
_haContextMock.Setup(x => x.StateAllChanges())
.Returns(_stateChangesSubject);
}
else
{
_haContextMock.Setup(x => x.StateAllChanges())
.Throws(subscriptionException);
}
return new LightMonitorInit(
_haContextMock.Object,
@@ -57,13 +53,9 @@ public class LightMonitorInitTests
private void VerifyTurnOn(bool shouldTurnOn)
{
if (shouldTurnOn)
{
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Once);
}
else
{
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
}
}
[Theory]
@@ -237,7 +229,8 @@ public class LightMonitorInitTests
{
CreateSut();
EmitStateChange("sensor.sun_next_dawn", LightMonitorConsts.UnavailableState, LightMonitorConsts.SunBelowHorizon);
EmitStateChange("sensor.sun_next_dawn", LightMonitorConsts.UnavailableState,
LightMonitorConsts.SunBelowHorizon);
VerifyTurnOn(false);
}
@@ -262,4 +255,4 @@ public class LightMonitorInitTests
VerifyTurnOn(false);
}
}
}
@@ -4,9 +4,9 @@ using NetDaemon.HassModel.Entities;
namespace LightMonitor.Manager.Tests;
/// <summary>
/// Deterministic, reusable fixtures for the LightMonitor tests.
/// Keeping the option builder, state builders and generated state tables here
/// avoids copy-pasted setup across the test classes.
/// Deterministic, reusable fixtures for the LightMonitor tests.
/// Keeping the option builder, state builders and generated state tables here
/// avoids copy-pasted setup across the test classes.
/// </summary>
internal static class LightMonitorTestData
{
@@ -33,36 +33,9 @@ internal static class LightMonitorTestData
];
/// <summary>
/// Builds light monitor options with production-compatible defaults.
/// </summary>
public static LightMonitorOptions CreateOptions(
string lightEntityId = LightEntityId,
string sunEntityId = SunEntityId,
IEnumerable<string>? personEntityIds = null,
bool enabled = true) => new()
{
Enabled = enabled,
LightEntityId = lightEntityId,
SunEntityId = sunEntityId,
PersonEntityIds = personEntityIds?.ToArray() ?? [.. PersonEntityIds]
};
/// <summary>
/// Builds an <see cref="EntityState"/> carrying <paramref name="state"/>.
/// </summary>
public static EntityState CreateState(string? state) => new() { State = state };
/// <summary>
/// Builds an <see cref="EntityState"/>, or <c>null</c> when the entity has no state at all
/// (as returned by <c>IHaContext.GetState</c> for a missing entity).
/// </summary>
public static EntityState? CreateOptionalState(string? state) =>
state is null ? null : CreateState(state);
/// <summary>
/// Sun states that should (<c>true</c>) or should not (<c>false</c>) cause a light turn on.
/// "below_horizon" is compared case-insensitively; unavailable, unknown, blank and
/// missing states must never turn the light on.
/// Sun states that should (<c>true</c>) or should not (<c>false</c>) cause a light turn on.
/// "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()
{
@@ -79,8 +52,8 @@ internal static class LightMonitorTestData
};
/// <summary>
/// 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.
/// 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()
{
@@ -97,8 +70,8 @@ internal static class LightMonitorTestData
};
/// <summary>
/// Person states checked against the "arrived home" transition.
/// Only "home" in any casing counts.
/// Person states checked against the "arrived home" transition.
/// Only "home" in any casing counts.
/// </summary>
public static TheoryData<string?, bool> PersonNewStateCases { get; } = new()
{
@@ -116,8 +89,8 @@ internal static class LightMonitorTestData
};
/// <summary>
/// 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.
/// 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()
{
@@ -130,8 +103,8 @@ internal static class LightMonitorTestData
};
/// <summary>
/// Old sun states for a transition to "below_horizon". Recovery from unavailable, unknown
/// or missing state must be ignored.
/// 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()
{
@@ -143,13 +116,13 @@ internal static class LightMonitorTestData
};
/// <summary>
/// Every 4^3 = 64 combination of home / not_home / unavailable / missing state across the
/// three configured person entities.
/// Every 4^3 = 64 combination of home / not_home / unavailable / missing state across the
/// three configured person entities.
/// </summary>
public static TheoryData<string?[]> PresenceStateMatrix { get; } = CreatePresenceStateMatrix();
/// <summary>
/// Person id configurations that resolve to "nobody tracked".
/// Person id configurations that resolve to "nobody tracked".
/// </summary>
public static TheoryData<string[]> NoPersonEntityIdSets { get; } =
[
@@ -158,21 +131,50 @@ internal static class LightMonitorTestData
[" "]
];
/// <summary>
/// Builds light monitor options with production-compatible defaults.
/// </summary>
public static LightMonitorOptions CreateOptions(
string lightEntityId = LightEntityId,
string sunEntityId = SunEntityId,
IEnumerable<string>? personEntityIds = null,
bool enabled = true)
{
return new LightMonitorOptions
{
Enabled = enabled,
LightEntityId = lightEntityId,
SunEntityId = sunEntityId,
PersonEntityIds = personEntityIds?.ToArray() ?? [.. PersonEntityIds]
};
}
/// <summary>
/// Builds an <see cref="EntityState" /> carrying <paramref name="state" />.
/// </summary>
public static EntityState CreateState(string? state)
{
return new EntityState { State = state };
}
/// <summary>
/// Builds an <see cref="EntityState" />, or <c>null</c> when the entity has no state at all
/// (as returned by <c>IHaContext.GetState</c> for a missing entity).
/// </summary>
public static EntityState? CreateOptionalState(string? state)
{
return state is null ? null : CreateState(state);
}
private static TheoryData<string?[]> CreatePresenceStateMatrix()
{
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,18 +1,17 @@
using FluentAssertions;
using LightMonitor.Models;
using LightMonitorManager;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
using LightMonitorManager;
namespace LightMonitor.Manager.Tests;
public class PresenceServiceTests
{
private readonly Mock<IHaContext> _haContextMock = new();
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private readonly Mock<IHaContext> _haContextMock = new();
private PresenceService CreateService(LightMonitorOptions? options = null)
{
@@ -96,9 +95,7 @@ public class PresenceServiceTests
CreateService().IsAnyoneHome().Should().BeFalse();
foreach (var personEntityId in LightMonitorTestData.PersonEntityIds)
{
_haContextMock.Verify(x => x.GetState(personEntityId), Times.Once);
}
}
[Fact]
@@ -123,8 +120,6 @@ public class PresenceServiceTests
service.IsAnyoneHome().Should().BeFalse();
foreach (var personEntityId in LightMonitorConsts.DefaultPersonEntityIds)
{
_haContextMock.Verify(x => x.GetState(personEntityId), Times.Once);
}
}
}
}