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 HomeAssistant.Entities.Interfaces;
using Microsoft.Extensions.Logging;
@@ -5,6 +7,8 @@ using Moq;
using PowerMonitor.Models;
using PowerMonitorManager;
#endregion
namespace PowerMonitor.Manager.Tests;
public class PowerMonitorEntityServiceTests
@@ -27,18 +31,22 @@ public class PowerMonitorEntityServiceTests
"button.powermonitor_trigger_limit_breach"
];
private readonly Mock<IEntityManager> _entityManagerMock = new();
private readonly Mock<IEntityManager> _entityManagerMock = new Mock<IEntityManager>();
private readonly PowerMonitorEntityService _sut;
public PowerMonitorEntityServiceTests()
{
var loggerMock = new Mock<ILogger<PowerMonitorEntityService>>();
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<PowerMonitorEntity>()))
.Returns(Task.CompletedTask);
.Returns(Task.CompletedTask);
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
.Returns(Task.CompletedTask);
.Returns(Task.CompletedTask);
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
.Returns(Task.CompletedTask);
.Returns(Task.CompletedTask);
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>())).Returns(Task.CompletedTask);
_sut = new PowerMonitorEntityService(_entityManagerMock.Object, loggerMock.Object);
}
@@ -56,6 +64,7 @@ public class PowerMonitorEntityServiceTests
// Assert
result.Should().HaveCount(4);
string[] expectedEntityIds =
[
$"sensor.powermonitor_{deviceName}_cost_15min",
@@ -63,6 +72,7 @@ public class PowerMonitorEntityServiceTests
$"sensor.powermonitor_{deviceName}_cost_daily",
$"sensor.powermonitor_{deviceName}_cost_monthly"
];
result.Should().BeEquivalentTo(expectedEntityIds);
}
@@ -79,15 +89,15 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
var sensors = Enumerable.Range(0, sensorCount)
.Select(index => PowerMonitorTestData.CreateSensor($"sensor.device{index}_power", $"Device {index}"))
.ToList();
.Select(index => PowerMonitorTestData.CreateSensor($"sensor.device{index}_power", $"Device {index}"))
.ToList();
// Act
await _sut.CreateDeviceEntitiesAsync(sensors);
// Assert
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<PowerMonitorEntity>()),
Times.Exactly(expectedCreates));
Times.Exactly(expectedCreates));
}
#endregion
@@ -152,7 +162,7 @@ public class PowerMonitorEntityServiceTests
#region GetPowerDeviceEntitiesNames
public static TheoryData<string, string, string, string> DeviceEntityCases => new()
public static TheoryData<string, string, string, string> DeviceEntityCases => new TheoryData<string, string, string, string>
{
{ "sonoff device", "sensor.sonoff_1002230e32_power", "Sonoff Power", "sonoff_1002230e32" },
{ "simple device", "sensor.tv_power", "TV Power", "tv" },
@@ -176,6 +186,7 @@ public class PowerMonitorEntityServiceTests
// Assert
result.Should().HaveCount(4, $"scenario: {scenario}");
string[] expectedEntityIds =
[
$"sensor.powermonitor_{expectedDeviceName}_cost_15min",
@@ -183,8 +194,10 @@ public class PowerMonitorEntityServiceTests
$"sensor.powermonitor_{expectedDeviceName}_cost_daily",
$"sensor.powermonitor_{expectedDeviceName}_cost_monthly"
];
result.Select(entity => entity.EntityId).Should().BeEquivalentTo(expectedEntityIds, $"scenario: {scenario}");
result.Should().OnlyContain(entity => entity.Domain == "sensor", $"scenario: {scenario}");
string[] expectedNames =
[
$"{friendlyName} cost (15 min)",
@@ -192,6 +205,7 @@ public class PowerMonitorEntityServiceTests
$"{friendlyName} cost (daily)",
$"{friendlyName} cost (monthly)"
];
result.Select(entity => entity.Name).Should().BeEquivalentTo(expectedNames, $"scenario: {scenario}");
}
@@ -228,9 +242,9 @@ public class PowerMonitorEntityServiceTests
{
// Arrange - both 1h cost entities fail, the remaining twelve must still be created.
_entityManagerMock
.Setup(em =>
em.CreateEntityAsync(It.Is<PowerMonitorEntity>(entity => entity.Identifier.EndsWith("_cost_1h"))))
.ThrowsAsync(new InvalidOperationException("Entity creation failed"));
.Setup(em =>
em.CreateEntityAsync(It.Is<PowerMonitorEntity>(entity => entity.Identifier.EndsWith("_cost_1h"))))
.ThrowsAsync(new InvalidOperationException("Entity creation failed"));
// Act
await _sut.CreateSystemEntitiesAsync();
@@ -282,8 +296,8 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
_entityManagerMock
.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
.ThrowsAsync(new InvalidOperationException("Failed"));
.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
.ThrowsAsync(new InvalidOperationException("Failed"));
// Act
var act = () => _sut.ResetEntitiesAsync();
@@ -305,7 +319,7 @@ public class PowerMonitorEntityServiceTests
// Assert
_entityManagerMock.Verify(em => em.RemoveEntity(It.Is<string>(id => SystemEntityIds.Contains(id))),
Times.Exactly(14));
Times.Exactly(14));
}
[Fact]
@@ -313,8 +327,8 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
_entityManagerMock
.Setup(em => em.RemoveEntity(It.IsAny<string>()))
.ThrowsAsync(new InvalidOperationException("Removal failed"));
.Setup(em => em.RemoveEntity(It.IsAny<string>()))
.ThrowsAsync(new InvalidOperationException("Removal failed"));
// Act
var act = () => _sut.RemoveSystemEntitiesAsync();
@@ -335,8 +349,8 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
var deviceNames = Enumerable.Range(0, deviceCount)
.Select(index => $"device_{index}")
.ToList();
.Select(index => $"device_{index}")
.ToList();
// Act
await _sut.RemoveDeviceEntitiesAsync(deviceNames);
@@ -350,8 +364,8 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
_entityManagerMock
.Setup(em => em.RemoveEntity(It.Is<string>(id => id.Contains("_cost_daily"))))
.ThrowsAsync(new InvalidOperationException("Removal failed"));
.Setup(em => em.RemoveEntity(It.Is<string>(id => id.Contains("_cost_daily"))))
.ThrowsAsync(new InvalidOperationException("Removal failed"));
// Act
var act = () => _sut.RemoveDeviceEntitiesAsync(["device_a", "device_b"]);
@@ -398,16 +412,18 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
var context = PowerMonitorTestData.CreateUpdateContext(
deviceReadings: PowerMonitorTestData.CreateReadings((entityId, 42.0)));
deviceReadings: PowerMonitorTestData.CreateReadings((entityId, 42.0)));
// Act
await _sut.UpdateEntitiesAsync(context);
// Assert
foreach (var window in new[] { "15min", "1h", "daily", "monthly" })
{
_entityManagerMock.Verify(
em => em.SetEntityState($"sensor.powermonitor_{deviceName}_cost_{window}", It.IsAny<string>()),
Times.Once);
em => em.SetEntityState($"sensor.powermonitor_{deviceName}_cost_{window}", It.IsAny<string>()),
Times.Once);
}
}
[Theory]
@@ -423,7 +439,7 @@ public class PowerMonitorEntityServiceTests
// Assert
_entityManagerMock.Verify(em => em.SetEntityState("binary_sensor.powermonitor_anomaly", expectedState),
Times.Once);
Times.Once);
}
[Theory]
@@ -447,7 +463,7 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
var context = PowerMonitorTestData.CreateUpdateContext(
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.tv_power", 42.0)));
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.tv_power", 42.0)));
// Act
await _sut.UpdateEntitiesAsync(context);
@@ -455,8 +471,10 @@ public class PowerMonitorEntityServiceTests
// Assert
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_total_power", "342.0"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_other_power", "128.5"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_total_cost_daily", "1.0000"),
Times.Once);
Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_tv_cost_15min", "0.0005"), Times.Once);
}
@@ -465,8 +483,9 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
_entityManagerMock
.Setup(em => em.SetEntityState("sensor.powermonitor_total_power", It.IsAny<string>()))
.ThrowsAsync(new InvalidOperationException("Failed"));
.Setup(em => em.SetEntityState("sensor.powermonitor_total_power", It.IsAny<string>()))
.ThrowsAsync(new InvalidOperationException("Failed"));
var context = PowerMonitorTestData.CreateUpdateContext();
// Act
@@ -475,8 +494,9 @@ public class PowerMonitorEntityServiceTests
// Assert
await act.Should().NotThrowAsync();
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_status", "ok"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.powermonitor_total_power", It.IsAny<object>()),
Times.Once);
Times.Once);
}
#endregion
@@ -488,8 +508,8 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
_entityManagerMock
.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
.ThrowsAsync(new InvalidOperationException("Failed"));
.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
.ThrowsAsync(new InvalidOperationException("Failed"));
// Act
var act = () => _sut.SetEntityStateSafe("sensor.test", "42");
@@ -503,8 +523,8 @@ public class PowerMonitorEntityServiceTests
{
// Arrange
_entityManagerMock
.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
.ThrowsAsync(new InvalidOperationException("Failed"));
.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
.ThrowsAsync(new InvalidOperationException("Failed"));
// Act
var act = () => _sut.SetEntityAttributesSafe("sensor.test", new { });