Add input_boolean for water heater preparation logic

Refreshed logic to allow users to toggle hot water preparation. Introduced new `input_boolean.boiler_prepare_hot_water` and updated config, services, and tests to ensure water readiness only when required.
This commit is contained in:
Marius
2026-03-16 11:42:49 +02:00
parent 5cc0ab29a6
commit 53eb53539a
13 changed files with 434 additions and 39 deletions
@@ -161,6 +161,23 @@ public class CheapestIntervalsFinderTests
result.Should().OnlyContain(r => r.Price <= 6.0);
}
[Fact]
public void FindCheapestIntervals_WithMaxPriceCap_ShouldIncludeAllIntervalsBeforeDeadline()
{
// Arrange: prices 10, 3, 7, 1, 5, 15 - with cap 6.0 only 3 would qualify; with double.MaxValue all qualify
var sut = CreateSut();
var dayStart = new DateTime(2026, 2, 26, 0, 0, 0);
var prices = CreatePricesWithValues(dayStart, 10.0, 3.0, 7.0, 1.0, 5.0, 15.0);
var deadline = dayStart.AddDays(1);
// Act
var result = sut.FindCheapestIntervals(prices, 4, double.MaxValue, deadline);
// Assert: all 6 intervals are eligible, we take 4 cheapest: 1, 3, 5, 7
result.Should().HaveCount(4);
result.Select(r => r.Price).OrderBy(p => p).Should().ContainInOrder(1.0, 3.0, 5.0, 7.0);
}
[Fact]
public void FindCheapestIntervals_WithDeadlineAndPriceCap_ShouldApplyBothFilters()
{
@@ -18,7 +18,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="AutoMapper" Version="16.0.0" />
<PackageReference Include="AutoMapper" Version="16.1.1" />
<PackageReference Include="FluentAssertions" Version="8.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
</ItemGroup>
@@ -31,13 +31,15 @@ public class NordPoolEntityServiceTests
// Assert
result.Should().NotBeNull();
result.Should().HaveCount(6);
result.Should().HaveCount(8);
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]
@@ -68,13 +70,15 @@ public class NordPoolEntityServiceTests
}
[Fact]
public void GetInitEntitiesList_AllEntities_ShouldHaveSensorDomain()
public void GetInitEntitiesList_ShouldContainExpectedDomains()
{
// Act
var result = _sut.GetInitEntitiesList("EUR/kWh");
// Assert
result.Should().OnlyContain(e => e.Domain == "sensor");
result.Should().Contain(e => e.Domain == "sensor");
result.Should().Contain(e => e.Domain == "switch");
result.Should().OnlyContain(e => e.Domain == "sensor" || e.Domain == "switch");
}
[Fact]
@@ -102,7 +106,7 @@ public class NordPoolEntityServiceTests
await _sut.CreateEntitiesAsync("EUR/kWh");
// Assert
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(6));
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(8));
}
[Fact]
@@ -123,7 +127,7 @@ public class NordPoolEntityServiceTests
await _sut.CreateEntitiesAsync("EUR/kWh");
// Assert
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(6));
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(8));
}
#endregion
@@ -145,8 +149,10 @@ public class NordPoolEntityServiceTests
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_current_price"), Times.Once);
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_today_prices"), Times.Once);
_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]
@@ -250,8 +256,10 @@ public class NordPoolEntityServiceTests
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_current_price", "0"), 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]
@@ -271,6 +279,7 @@ public class NordPoolEntityServiceTests
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_current_price", It.IsAny<object>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_today_prices", It.IsAny<object>()), Times.Once);
_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);
}
@@ -294,9 +303,9 @@ public class NordPoolEntityServiceTests
}
[Fact]
public async Task ResetEntitiesAsync_ShouldCallSetStateTwelveTimesTotal()
public async Task ResetEntitiesAsync_ShouldCallSetStateAndAttributesCorrectly()
{
// Arrange - 6 entities x (1 state + 1 attributes) = 12 calls total
// Arrange - 8 entities get state, 7 entities get attributes (switch has state only)
_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>()))
@@ -306,8 +315,8 @@ public class NordPoolEntityServiceTests
await _sut.ResetEntitiesAsync();
// Assert
_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));
_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));
}
#endregion
@@ -338,6 +347,7 @@ public class NordPoolEntityServiceTests
ConfiguredHours = 1.0,
ConfiguredIntervalCount = 4,
ConfiguredPriceCap = 100.0,
SkipPriceCap = false,
ConfiguredReadyBy = "06:00",
IsNowCheap = false,
NextCheapIntervalStart = null,
@@ -366,6 +376,7 @@ public class NordPoolEntityServiceTests
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_current_price", "12.50"), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_prices", "1"), Times.Once);
_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);
}
@@ -388,6 +399,7 @@ public class NordPoolEntityServiceTests
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_current_price", It.IsAny<object>()), Times.Once);
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_today_prices", It.IsAny<object>()), Times.Once);
_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);
}
[Fact]
@@ -409,6 +421,112 @@ public class NordPoolEntityServiceTests
// Assert
_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", It.Is<string>(s =>
s.Length > 0 && s != "0" && s.Contains('-'))),
Times.Once);
}
[Fact]
public async Task UpdateEntitiesAsync_WithTodayAndTomorrowPrices_ShouldUpdateCombinedSensor()
{
// Arrange
var todayStart = new DateTime(2025, 3, 16, 6, 0, 0, DateTimeKind.Utc);
var tomorrowStart = new DateTime(2025, 3, 17, 6, 0, 0, DateTimeKind.Utc);
var todayPrice = new NordPoolPrice
{
Prices =
[
new NordPoolDayPrice { Start = todayStart.AddHours(2), End = todayStart.AddHours(2).AddMinutes(15), Price = 15.0 },
new NordPoolDayPrice { Start = todayStart, End = todayStart.AddMinutes(15), Price = 10.0 }
]
};
var tomorrowPrice = new NordPoolPrice
{
Prices =
[
new NordPoolDayPrice { Start = tomorrowStart, End = tomorrowStart.AddMinutes(15), Price = 20.0 }
]
};
var context = CreateDefaultContext(todayPrice: todayPrice, tomorrowPrice: tomorrowPrice);
_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>()))
.Returns(Task.CompletedTask);
// Act
await _sut.UpdateEntitiesAsync(context);
// Assert - state is update time in yyyy-MM-dd HH:mm:ss format
_entityManagerMock.Verify(
em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices", It.Is<string>(s =>
s.Length > 0 && s.Contains('-') && s.Contains(':') && s != "0" && s != "3")),
Times.Once);
// Assert - attributes contain merged, sorted prices (today 06:00, today 08:00, tomorrow 06:00)
_entityManagerMock.Verify(
em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.Is<object>(o =>
o.GetType().GetProperty("prices_count")!.GetValue(o)!.Equals(3))),
Times.Once);
}
[Fact]
public async Task UpdateEntitiesAsync_WithOnlyTodayPrices_ShouldUpdateCombinedSensor()
{
// Arrange
var todayPrice = new NordPoolPrice
{
Prices = [new NordPoolDayPrice { Start = DateTime.UtcNow, End = DateTime.UtcNow.AddMinutes(15), Price = 12.0 }]
};
var context = CreateDefaultContext(todayPrice: todayPrice, tomorrowPrice: new NordPoolPrice { Prices = [] });
_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>()))
.Returns(Task.CompletedTask);
// Act
await _sut.UpdateEntitiesAsync(context);
// Assert
_entityManagerMock.Verify(
em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices", It.Is<string>(s =>
s.Length > 0 && s.Contains('-') && s != "0")),
Times.Once);
_entityManagerMock.Verify(
em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.Is<object>(o =>
o.GetType().GetProperty("prices_count")!.GetValue(o)!.Equals(1))),
Times.Once);
}
[Fact]
public async Task UpdateEntitiesAsync_WithOnlyTomorrowPrices_ShouldUpdateCombinedSensor()
{
// Arrange
var tomorrowPrice = new NordPoolPrice
{
Prices = [new NordPoolDayPrice { Start = DateTime.UtcNow.AddDays(1), End = DateTime.UtcNow.AddDays(1).AddMinutes(15), Price = 18.0 }]
};
var context = CreateDefaultContext(todayPrice: new NordPoolPrice { Prices = [] }, tomorrowPrice: tomorrowPrice);
_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>()))
.Returns(Task.CompletedTask);
// Act
await _sut.UpdateEntitiesAsync(context);
// Assert
_entityManagerMock.Verify(
em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices", It.Is<string>(s =>
s.Length > 0 && s.Contains('-') && s != "0")),
Times.Once);
_entityManagerMock.Verify(
em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.Is<object>(o =>
o.GetType().GetProperty("prices_count")!.GetValue(o)!.Equals(1))),
Times.Once);
}
#endregion
@@ -37,6 +37,7 @@ public class NordPoolInitTests
{
return new NordPoolInit(
_entities,
_haContextMock.Object,
_loggerMock.Object,
_priceServiceMock.Object,
_entityServiceMock.Object,