Refactor UpdateCheapestIntervals logic to return a result object and improve code readability. Introduce CheapestIntervalsUpdateResult model for encapsulating update details and simplify inline tuples.
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Reflection;
|
|||||||
using HomeAssistant.Entities;
|
using HomeAssistant.Entities;
|
||||||
using HomeAssistant.Entities.Interfaces;
|
using HomeAssistant.Entities.Interfaces;
|
||||||
using HomeAssistantGenerated;
|
using HomeAssistantGenerated;
|
||||||
|
using HomeAssistantServer;
|
||||||
using Mappings;
|
using Mappings;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
@@ -13,7 +14,6 @@ using NetDaemon.Extensions.Tts;
|
|||||||
using NetDaemon.Runtime;
|
using NetDaemon.Runtime;
|
||||||
using NordPoolManager;
|
using NordPoolManager;
|
||||||
using NordPoolManager.Interface;
|
using NordPoolManager.Interface;
|
||||||
using HomeAssistantServer;
|
|
||||||
|
|
||||||
#pragma warning disable CA1812
|
#pragma warning disable CA1812
|
||||||
|
|
||||||
|
|||||||
@@ -119,7 +119,10 @@ public class NordPoolEntityServiceTests
|
|||||||
{
|
{
|
||||||
callCount++;
|
callCount++;
|
||||||
if (callCount == 2)
|
if (callCount == 2)
|
||||||
|
{
|
||||||
throw new InvalidOperationException("Entity creation failed");
|
throw new InvalidOperationException("Entity creation failed");
|
||||||
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace NordPoolManager.Models;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Result of updating cheapest intervals, containing the effective configuration used for the update.
|
||||||
|
/// </summary>
|
||||||
|
public class CheapestIntervalsUpdateResult
|
||||||
|
{
|
||||||
|
public int IntervalCount { get; init; }
|
||||||
|
public double PriceCap { get; init; }
|
||||||
|
public bool SkipPriceCap { get; init; }
|
||||||
|
}
|
||||||
@@ -40,6 +40,7 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
logger.LogInformation("Creating/re-registering MQTT entity: {EntityId}", nordPoolEntity.EntityId);
|
logger.LogInformation("Creating/re-registering MQTT entity: {EntityId}", nordPoolEntity.EntityId);
|
||||||
|
|
||||||
await entityManager.CreateEntityAsync(nordPoolEntity);
|
await entityManager.CreateEntityAsync(nordPoolEntity);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -310,11 +311,11 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IReadOnlyList<NordPoolDayPrice> MergeTodayTomorrowPrices(
|
private static List<NordPoolDayPrice> MergeTodayTomorrowPrices(NordPoolPrice? today, NordPoolPrice? tomorrow)
|
||||||
NordPoolPrice? today, NordPoolPrice? tomorrow)
|
|
||||||
{
|
{
|
||||||
var todayList = today?.Prices ?? [];
|
var todayList = today?.Prices ?? [];
|
||||||
var tomorrowList = tomorrow?.Prices ?? [];
|
var tomorrowList = tomorrow?.Prices ?? [];
|
||||||
|
|
||||||
return todayList
|
return todayList
|
||||||
.Concat(tomorrowList)
|
.Concat(tomorrowList)
|
||||||
.Where(p => p?.Start != null)
|
.Where(p => p?.Start != null)
|
||||||
|
|||||||
@@ -79,13 +79,10 @@ public class NordPoolInit : INordPoolInit
|
|||||||
|
|
||||||
private void ScheduleEndOfDayReminder()
|
private void ScheduleEndOfDayReminder()
|
||||||
{
|
{
|
||||||
_scheduler.ScheduleCron("0 20 * * *", () =>
|
_scheduler.ScheduleCron("0 20 * * *", CheckSkipPriceCapReminderAsync);
|
||||||
{
|
|
||||||
_ = CheckSkipPriceCapReminderAsync();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CheckSkipPriceCapReminderAsync()
|
private void CheckSkipPriceCapReminderAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -111,7 +108,11 @@ public class NordPoolInit : INordPoolInit
|
|||||||
BoilerSkipPriceCapEntity.StateChanges()
|
BoilerSkipPriceCapEntity.StateChanges()
|
||||||
.Subscribe(e =>
|
.Subscribe(e =>
|
||||||
{
|
{
|
||||||
if (e.Old?.State == e.New?.State) return;
|
if (e.Old?.State == e.New?.State)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_logger.LogInformation("Skip price cap switch changed, recalculating cheapest intervals");
|
_logger.LogInformation("Skip price cap switch changed, recalculating cheapest intervals");
|
||||||
_ = RecalculateOnConfigChangeAsync();
|
_ = RecalculateOnConfigChangeAsync();
|
||||||
});
|
});
|
||||||
@@ -121,8 +122,8 @@ public class NordPoolInit : INordPoolInit
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var (intervalCount, priceCap, skipPriceCap) = UpdateCheapestIntervals();
|
var result = UpdateCheapestIntervals();
|
||||||
await UpdateEntitiesAsync(intervalCount, priceCap, skipPriceCap);
|
await UpdateEntitiesAsync(result.IntervalCount, result.PriceCap, result.SkipPriceCap);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -152,8 +153,8 @@ public class NordPoolInit : INordPoolInit
|
|||||||
_logger.LogWarning("Current price is 0, which may indicate an issue with price retrieval");
|
_logger.LogWarning("Current price is 0, which may indicate an issue with price retrieval");
|
||||||
}
|
}
|
||||||
|
|
||||||
var (intervalCount, priceCap, skipPriceCap) = UpdateCheapestIntervals();
|
var result = UpdateCheapestIntervals();
|
||||||
await UpdateEntitiesAsync(intervalCount, priceCap, skipPriceCap);
|
await UpdateEntitiesAsync(result.IntervalCount, result.PriceCap, result.SkipPriceCap);
|
||||||
|
|
||||||
_logger.LogInformation("NordPool price initialized with current price: {CurrentPrice} {Unit}", _priceService.CurrentPrice, UnitOfMeasurement);
|
_logger.LogInformation("NordPool price initialized with current price: {CurrentPrice} {Unit}", _priceService.CurrentPrice, UnitOfMeasurement);
|
||||||
}
|
}
|
||||||
@@ -218,8 +219,8 @@ public class NordPoolInit : INordPoolInit
|
|||||||
|
|
||||||
_priceService.UpdateCurrentPrice();
|
_priceService.UpdateCurrentPrice();
|
||||||
|
|
||||||
var (intervalCount, priceCap, skipPriceCap) = UpdateCheapestIntervals();
|
var result = UpdateCheapestIntervals();
|
||||||
await UpdateEntitiesAsync(intervalCount, priceCap, skipPriceCap);
|
await UpdateEntitiesAsync(result.IntervalCount, result.PriceCap, result.SkipPriceCap);
|
||||||
|
|
||||||
_logger.LogInformation("NordPool prices updated successfully. Current price: {CurrentPrice} {Unit}", _priceService.CurrentPrice, UnitOfMeasurement);
|
_logger.LogInformation("NordPool prices updated successfully. Current price: {CurrentPrice} {Unit}", _priceService.CurrentPrice, UnitOfMeasurement);
|
||||||
}
|
}
|
||||||
@@ -234,7 +235,7 @@ public class NordPoolInit : INordPoolInit
|
|||||||
await _entityService.CleanupEntitiesAsync();
|
await _entityService.CleanupEntitiesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private (int IntervalCount, double PriceCap, bool SkipPriceCap) UpdateCheapestIntervals()
|
private CheapestIntervalsUpdateResult UpdateCheapestIntervals()
|
||||||
{
|
{
|
||||||
var boilerConfig = ReadBoilerConfig();
|
var boilerConfig = ReadBoilerConfig();
|
||||||
var effectivePriceCap = boilerConfig.SkipPriceCap ? double.MaxValue : boilerConfig.PriceCap;
|
var effectivePriceCap = boilerConfig.SkipPriceCap ? double.MaxValue : boilerConfig.PriceCap;
|
||||||
@@ -246,7 +247,12 @@ public class NordPoolInit : INordPoolInit
|
|||||||
{
|
{
|
||||||
_logger.LogDebug("No today prices available, skipping cheapest intervals calculation");
|
_logger.LogDebug("No today prices available, skipping cheapest intervals calculation");
|
||||||
CheapestIntervals = [];
|
CheapestIntervals = [];
|
||||||
return (boilerConfig.IntervalCount, boilerConfig.PriceCap, boilerConfig.SkipPriceCap);
|
return new CheapestIntervalsUpdateResult
|
||||||
|
{
|
||||||
|
IntervalCount = boilerConfig.IntervalCount,
|
||||||
|
PriceCap = boilerConfig.PriceCap,
|
||||||
|
SkipPriceCap = boilerConfig.SkipPriceCap
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
CheapestIntervals = _cheapestIntervalsFinder.FindCheapestIntervals(_priceService.TodayNordPoolPrice, effectiveIntervalCount, effectivePriceCap, boilerConfig.Deadline);
|
CheapestIntervals = _cheapestIntervalsFinder.FindCheapestIntervals(_priceService.TodayNordPoolPrice, effectiveIntervalCount, effectivePriceCap, boilerConfig.Deadline);
|
||||||
@@ -260,7 +266,12 @@ public class NordPoolInit : INordPoolInit
|
|||||||
CheapestIntervals = [];
|
CheapestIntervals = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return (boilerConfig.IntervalCount, boilerConfig.PriceCap, boilerConfig.SkipPriceCap);
|
return new CheapestIntervalsUpdateResult
|
||||||
|
{
|
||||||
|
IntervalCount = boilerConfig.IntervalCount,
|
||||||
|
PriceCap = boilerConfig.PriceCap,
|
||||||
|
SkipPriceCap = boilerConfig.SkipPriceCap
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private BoilerConfig ReadBoilerConfig()
|
private BoilerConfig ReadBoilerConfig()
|
||||||
|
|||||||
Reference in New Issue
Block a user