|
|
|
@@ -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
|
|
|
|
|