feat: migrate boiler control implementation to BoilerMonitor namespace and add budget handling logic

This commit is contained in:
Marius
2026-03-24 15:29:59 +02:00
parent c4015277a3
commit 11c2574235
61 changed files with 3250 additions and 728 deletions
@@ -1,215 +0,0 @@
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Moq;
using NordPoolManager;
using NordPoolManager.Models.Enums;
namespace NordPool.Manager.Tests;
public class BoilerControlServiceTests
{
private readonly BoilerControlService _sut;
public BoilerControlServiceTests()
{
var loggerMock = new Mock<ILogger<BoilerControlService>>();
_sut = new BoilerControlService(loggerMock.Object);
}
#region TurnOn scenarios
[Theory]
[InlineData("off")]
[InlineData("OFF")]
[InlineData("Off")]
[InlineData(null)]
[InlineData("unavailable")]
public void DetermineAction_WhenCheapAndWaterNotReady_ShouldReturnTurnOn(string? switchState)
{
// Arrange
const bool isNowCheap = true;
const bool waterReady = false;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, switchState);
// Assert
result.Should().Be(BoilerAction.TurnOn);
}
#endregion
#region TurnOff scenarios
[Fact]
public void DetermineAction_WhenNotCheapAndSwitchIsOn_ShouldReturnTurnOff()
{
// Arrange
const bool isNowCheap = false;
const bool waterReady = false;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, "on");
// Assert
result.Should().Be(BoilerAction.TurnOff);
}
[Fact]
public void DetermineAction_WhenWaterReadyAndSwitchIsOn_ShouldReturnTurnOff()
{
// Arrange
const bool isNowCheap = true;
const bool waterReady = true;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, "on");
// Assert
result.Should().Be(BoilerAction.TurnOff);
}
[Fact]
public void DetermineAction_WhenNotCheapAndWaterReadyAndSwitchIsOn_ShouldReturnTurnOff()
{
// Arrange
const bool isNowCheap = false;
const bool waterReady = true;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, "on");
// Assert
result.Should().Be(BoilerAction.TurnOff);
}
[Theory]
[InlineData("on")]
[InlineData("ON")]
[InlineData("On")]
public void DetermineAction_WhenNotCheapAndSwitchIsOnCaseInsensitive_ShouldReturnTurnOff(string switchState)
{
// Arrange
const bool isNowCheap = false;
const bool waterReady = false;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, switchState);
// Assert
result.Should().Be(BoilerAction.TurnOff);
}
#endregion
#region NoChange scenarios
[Fact]
public void DetermineAction_WhenCheapAndNotReadyAndAlreadyOn_ShouldReturnNoChange()
{
// Arrange
const bool isNowCheap = true;
const bool waterReady = false;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, "on");
// Assert
result.Should().Be(BoilerAction.NoChange);
}
[Fact]
public void DetermineAction_WhenNotCheapAndAlreadyOff_ShouldReturnNoChange()
{
// Arrange
const bool isNowCheap = false;
const bool waterReady = false;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, "off");
// Assert
result.Should().Be(BoilerAction.NoChange);
}
[Fact]
public void DetermineAction_WhenWaterReadyAndAlreadyOff_ShouldReturnNoChange()
{
// Arrange
const bool isNowCheap = true;
const bool waterReady = true;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, "off");
// Assert
result.Should().Be(BoilerAction.NoChange);
}
[Fact]
public void DetermineAction_WhenNotCheapAndWaterReadyAndAlreadyOff_ShouldReturnNoChange()
{
// Arrange
const bool isNowCheap = false;
const bool waterReady = true;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, "off");
// Assert
result.Should().Be(BoilerAction.NoChange);
}
[Fact]
public void DetermineAction_WhenNotCheapAndSwitchStateIsNull_ShouldReturnNoChange()
{
// Arrange
const bool isNowCheap = false;
const bool waterReady = false;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, null);
// Assert
result.Should().Be(BoilerAction.NoChange);
}
[Fact]
public void DetermineAction_WhenNotCheapAndSwitchStateIsUnavailable_ShouldReturnNoChange()
{
// Arrange
const bool isNowCheap = false;
const bool waterReady = false;
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, "unavailable");
// Assert
result.Should().Be(BoilerAction.NoChange);
}
#endregion
#region Comprehensive theory covering all combinations
[Theory]
[InlineData(true, false, "off", BoilerAction.TurnOn)] // cheap, not ready, off → turn on
[InlineData(true, false, "on", BoilerAction.NoChange)] // cheap, not ready, already on → no change
[InlineData(true, true, "on", BoilerAction.TurnOff)] // cheap, ready, on → turn off (water heated)
[InlineData(true, true, "off", BoilerAction.NoChange)] // cheap, ready, off → no change
[InlineData(false, false, "on", BoilerAction.TurnOff)] // not cheap, not ready, on → turn off
[InlineData(false, false, "off", BoilerAction.NoChange)] // not cheap, not ready, off → no change
[InlineData(false, true, "on", BoilerAction.TurnOff)] // not cheap, ready, on → turn off
[InlineData(false, true, "off", BoilerAction.NoChange)] // not cheap, ready, off → no change
public void DetermineAction_AllCombinations_ShouldReturnExpectedAction(
bool isNowCheap, bool waterReady, string switchState, BoilerAction expectedAction)
{
// Act
var result = _sut.DetermineAction(isNowCheap, waterReady, switchState);
// Assert
result.Should().Be(expectedAction);
}
#endregion
}
@@ -441,128 +441,4 @@ public class CheapestIntervalsFinderTests
}
#endregion
#region IsWaterReady
[Fact]
public void IsWaterReady_SwitchOnAndLowPower_ShouldReturnTrue()
{
// Arrange
var sut = CreateSut();
// Act
var result = sut.IsWaterReady("on", 0.0);
// Assert
result.Should().BeTrue();
}
[Fact]
public void IsWaterReady_SwitchOnAndPowerBelowThreshold_ShouldReturnTrue()
{
// Arrange
var sut = CreateSut();
// Act
var result = sut.IsWaterReady("on", 4.9);
// Assert
result.Should().BeTrue();
}
[Fact]
public void IsWaterReady_SwitchOnAndPowerAtThreshold_ShouldReturnFalse()
{
// Arrange
var sut = CreateSut();
// Act
var result = sut.IsWaterReady("on", 5.0);
// Assert
result.Should().BeFalse();
}
[Fact]
public void IsWaterReady_SwitchOnAndHighPower_ShouldReturnFalse()
{
// Arrange
var sut = CreateSut();
// Act
var result = sut.IsWaterReady("on", 2000.0);
// Assert
result.Should().BeFalse();
}
[Fact]
public void IsWaterReady_SwitchOff_ShouldReturnFalse()
{
// Arrange
var sut = CreateSut();
// Act
var result = sut.IsWaterReady("off", 0.0);
// Assert
result.Should().BeFalse();
}
[Fact]
public void IsWaterReady_NullSwitchState_ShouldReturnFalse()
{
// Arrange
var sut = CreateSut();
// Act
var result = sut.IsWaterReady(null, 0.0);
// Assert
result.Should().BeFalse();
}
[Fact]
public void IsWaterReady_NullPowerWatts_ShouldReturnFalse()
{
// Arrange
var sut = CreateSut();
// Act
var result = sut.IsWaterReady("on", null);
// Assert
result.Should().BeFalse();
}
[Fact]
public void IsWaterReady_BothNull_ShouldReturnFalse()
{
// Arrange
var sut = CreateSut();
// Act
var result = sut.IsWaterReady(null, null);
// Assert
result.Should().BeFalse();
}
[Theory]
[InlineData("ON")] // uppercase
[InlineData("On")] // mixed case
[InlineData("on")] // lowercase
public void IsWaterReady_SwitchOnCaseInsensitive_ShouldReturnTrue(string switchState)
{
// Arrange
var sut = CreateSut();
// Act
var result = sut.IsWaterReady(switchState, 0.0);
// Assert
result.Should().BeTrue();
}
#endregion
}
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
@@ -28,6 +28,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BoilerMonitor.Models\BoilerMonitor.Models.csproj" />
<ProjectReference Include="..\NordPool.Manager\NordPool.Manager.csproj" />
<ProjectReference Include="..\NordPool.Models\NordPool.Models.csproj" />
</ItemGroup>
@@ -31,15 +31,13 @@ public class NordPoolEntityServiceTests
// Assert
result.Should().NotBeNull();
result.Should().HaveCount(8);
result.Should().HaveCount(6);
result.Should().Contain(e => e.EntityId == "sensor.nordpool_last_update_time");
result.Should().Contain(e => e.EntityId == "sensor.nordpool_current_price");
result.Should().Contain(e => e.EntityId == "sensor.nordpool_today_prices");
result.Should().Contain(e => e.EntityId == "sensor.nordpool_tomorrow_prices");
result.Should().Contain(e => e.EntityId == "sensor.nordpool_today_tomorrow_prices");
result.Should().Contain(e => e.EntityId == "sensor.nordpool_cheapest_intervals");
result.Should().Contain(e => e.EntityId == "sensor.nordpool_boiler_water_ready");
result.Should().Contain(e => e.EntityId == "switch.nordpool_boiler_skip_price_cap");
}
[Fact]
@@ -76,9 +74,7 @@ public class NordPoolEntityServiceTests
var result = _sut.GetInitEntitiesList("EUR/kWh");
// Assert
result.Should().Contain(e => e.Domain == "sensor");
result.Should().Contain(e => e.Domain == "switch");
result.Should().OnlyContain(e => e.Domain == "sensor" || e.Domain == "switch");
result.Should().OnlyContain(e => e.Domain == "sensor");
}
[Fact]
@@ -106,7 +102,7 @@ public class NordPoolEntityServiceTests
await _sut.CreateEntitiesAsync("EUR/kWh");
// Assert
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(8));
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(6));
}
[Fact]
@@ -130,7 +126,7 @@ public class NordPoolEntityServiceTests
await _sut.CreateEntitiesAsync("EUR/kWh");
// Assert
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(8));
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(6));
}
#endregion
@@ -154,8 +150,6 @@ public class NordPoolEntityServiceTests
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_tomorrow_prices"), Times.Once);
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_today_tomorrow_prices"), Times.Once);
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_cheapest_intervals"), Times.Once);
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_boiler_water_ready"), Times.Once);
_entityManagerMock.Verify(em => em.RemoveEntity("switch.nordpool_boiler_skip_price_cap"), Times.Once);
}
[Fact]
@@ -256,13 +250,11 @@ public class NordPoolEntityServiceTests
// Assert
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_last_update_time", "unknown"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_current_price", "0"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_current_price", ""), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_prices", "0"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_tomorrow_prices", "0"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices", "unknown"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_cheapest_intervals", "0"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_boiler_water_ready", "unknown"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("switch.nordpool_boiler_skip_price_cap", "OFF"), Times.Once);
}
[Fact]
@@ -284,7 +276,6 @@ public class NordPoolEntityServiceTests
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_tomorrow_prices", It.IsAny<object>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.IsAny<object>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_cheapest_intervals", It.IsAny<object>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_boiler_water_ready", It.IsAny<object>()), Times.Once);
}
[Fact]
@@ -308,7 +299,7 @@ public class NordPoolEntityServiceTests
[Fact]
public async Task ResetEntitiesAsync_ShouldCallSetStateAndAttributesCorrectly()
{
// Arrange - 8 entities get state, 7 entities get attributes (switch has state only)
// Arrange - 6 entities get state and attributes
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
.Returns(Task.CompletedTask);
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
@@ -318,8 +309,8 @@ public class NordPoolEntityServiceTests
await _sut.ResetEntitiesAsync();
// Assert
_entityManagerMock.Verify(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(8));
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()), Times.Exactly(7));
_entityManagerMock.Verify(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(6));
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()), Times.Exactly(6));
}
#endregion
@@ -353,10 +344,7 @@ public class NordPoolEntityServiceTests
SkipPriceCap = false,
ConfiguredReadyBy = "06:00",
IsNowCheap = false,
NextCheapIntervalStart = null,
WaterReady = false,
BoilerSwitchState = "off",
BoilerPowerWatts = 0.0
NextCheapIntervalStart = null
};
}
@@ -381,7 +369,6 @@ public class NordPoolEntityServiceTests
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_tomorrow_prices", "1"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices", It.Is<string>(s => s.Contains('-') && s.Contains(':'))), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_cheapest_intervals", It.IsAny<string>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_boiler_water_ready", It.IsAny<string>()), Times.Once);
}
[Fact]
+55 -6
View File
@@ -1,11 +1,15 @@
using System.Reactive.Concurrency;
using BoilerMonitor.Models;
using FluentAssertions;
using HomeAssistantGenerated;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
using NordPool.Models;
using NordPoolManager;
using NordPoolManager.Interface;
using NordPoolManager.Models;
namespace NordPool.Manager.Tests;
@@ -16,7 +20,7 @@ public class NordPoolInitTests
private readonly Mock<INordPoolEntityService> _entityServiceMock;
private readonly Mock<IScheduler> _schedulerMock;
private readonly Mock<ICheapestIntervalsFinder> _cheapestIntervalsFinderMock;
private readonly Mock<IBoilerControlService> _boilerControlServiceMock;
private readonly Mock<IBoilerConfigReader> _boilerConfigReaderMock;
private readonly Mock<IHaContext> _haContextMock;
private readonly Entities _entities;
@@ -28,23 +32,48 @@ public class NordPoolInitTests
_schedulerMock = new Mock<IScheduler>();
_schedulerMock.Setup(s => s.Now).Returns(DateTimeOffset.UtcNow);
_cheapestIntervalsFinderMock = new Mock<ICheapestIntervalsFinder>();
_boilerControlServiceMock = new Mock<IBoilerControlService>();
_boilerConfigReaderMock = new Mock<IBoilerConfigReader>();
_haContextMock = new Mock<IHaContext>();
_entities = new Entities(_haContextMock.Object);
_priceServiceMock.Setup(p => p.FetchPricesAsync()).Returns(Task.CompletedTask);
_entityServiceMock.Setup(e => e.CleanupEntitiesAsync(It.IsAny<CancellationToken>())).Returns(Task.CompletedTask);
_entityServiceMock.Setup(e => e.CreateEntitiesAsync(It.IsAny<string>())).Returns(Task.CompletedTask);
_entityServiceMock.Setup(e => e.ResetEntitiesAsync()).Returns(Task.CompletedTask);
_entityServiceMock.Setup(e => e.UpdateEntitiesAsync(It.IsAny<EntityUpdateContext>())).Returns(Task.CompletedTask);
_priceServiceMock.Setup(p => p.UpdateCurrentPrice()).Returns(0.0);
_priceServiceMock.Setup(p => p.ShouldFetchNewPrices(It.IsAny<DateOnly>())).Returns(false);
_priceServiceMock.SetupGet(p => p.DayQuartersIndex).Returns(0);
_priceServiceMock.SetupGet(p => p.TodayNordPoolPrice).Returns((NordPoolPrice?)null);
_priceServiceMock.SetupGet(p => p.TomorrowNordPoolPrice).Returns((NordPoolPrice?)null);
_priceServiceMock.SetupGet(p => p.CurrentPrice).Returns(0.0);
_priceServiceMock.SetupGet(p => p.LastUpdatedDate).Returns((DateTime?)null);
_cheapestIntervalsFinderMock
.Setup(c => c.IsCurrentIntervalCheap(It.IsAny<IReadOnlyList<CheapestIntervalResult>>(), It.IsAny<DateTime>()))
.Returns(false);
_boilerConfigReaderMock.Setup(b => b.ReadConfig()).Returns(new BoilerConfig
{
IntervalCount = 4,
PriceCap = double.MaxValue,
Deadline = DateTime.Today.AddDays(1),
SkipPriceCap = false
});
}
private NordPoolInit CreateSut()
private NordPoolInit CreateSut(NordPoolOptions? nordPoolOptions = null)
{
var options = Options.Create(
nordPoolOptions ?? new NordPoolOptions { CleanupEntitiesOnStartup = false });
return new NordPoolInit(
_entities,
_haContextMock.Object,
_loggerMock.Object,
_priceServiceMock.Object,
_entityServiceMock.Object,
_schedulerMock.Object,
_cheapestIntervalsFinderMock.Object,
_boilerControlServiceMock.Object
);
_boilerConfigReaderMock.Object,
options);
}
[Fact]
@@ -71,4 +100,24 @@ public class NordPoolInitTests
// Assert
_entityServiceMock.Verify(es => es.CleanupEntitiesAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task InitializeAsync_WhenCleanupEntitiesOnStartupIsTrue_ShouldCallEntityCleanup()
{
var sut = CreateSut(new NordPoolOptions { CleanupEntitiesOnStartup = true });
await sut.InitializeAsync();
_entityServiceMock.Verify(es => es.CleanupEntitiesAsync(It.IsAny<CancellationToken>()), Times.AtLeastOnce);
}
[Fact]
public async Task InitializeAsync_WhenCleanupEntitiesOnStartupIsFalse_ShouldNotCallEntityCleanup()
{
var sut = CreateSut(new NordPoolOptions { CleanupEntitiesOnStartup = false });
await sut.InitializeAsync();
_entityServiceMock.Verify(es => es.CleanupEntitiesAsync(It.IsAny<CancellationToken>()), Times.Never);
}
}
@@ -209,7 +209,7 @@ public class NordPoolPriceServiceTests
// Arrange
var dto = new NordPoolPriceResultDto
{
Prices = [new() { Start = DateTime.UtcNow.ToString("o"), End = DateTime.UtcNow.AddHours(1).ToString("o"), Price = 100.0 }]
Prices = [new NordPoolDayPriceDto { Start = DateTime.UtcNow.ToString("o"), End = DateTime.UtcNow.AddHours(1).ToString("o"), Price = 100.0 }]
};
var jsonString = JsonSerializer.Serialize(dto);
@@ -241,7 +241,7 @@ public class NordPoolPriceServiceTests
// Arrange
var dto = new NordPoolPriceResultDto
{
Prices = [new() { Start = DateTime.UtcNow.ToString("o"), End = DateTime.UtcNow.AddHours(1).ToString("o"), Price = 100.0 }]
Prices = [new NordPoolDayPriceDto { Start = DateTime.UtcNow.ToString("o"), End = DateTime.UtcNow.AddHours(1).ToString("o"), Price = 100.0 }]
};
var jsonString = JsonSerializer.Serialize(dto);