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.Interfaces;
|
||||
using HomeAssistantGenerated;
|
||||
using HomeAssistantServer;
|
||||
using Mappings;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@@ -13,7 +14,6 @@ using NetDaemon.Extensions.Tts;
|
||||
using NetDaemon.Runtime;
|
||||
using NordPoolManager;
|
||||
using NordPoolManager.Interface;
|
||||
using HomeAssistantServer;
|
||||
|
||||
#pragma warning disable CA1812
|
||||
|
||||
|
||||
@@ -119,7 +119,10 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
callCount++;
|
||||
if (callCount == 2)
|
||||
{
|
||||
throw new InvalidOperationException("Entity creation failed");
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
logger.LogInformation("Creating/re-registering MQTT entity: {EntityId}", nordPoolEntity.EntityId);
|
||||
|
||||
await entityManager.CreateEntityAsync(nordPoolEntity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -310,11 +311,11 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
||||
return entries;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<NordPoolDayPrice> MergeTodayTomorrowPrices(
|
||||
NordPoolPrice? today, NordPoolPrice? tomorrow)
|
||||
private static List<NordPoolDayPrice> MergeTodayTomorrowPrices(NordPoolPrice? today, NordPoolPrice? tomorrow)
|
||||
{
|
||||
var todayList = today?.Prices ?? [];
|
||||
var tomorrowList = tomorrow?.Prices ?? [];
|
||||
|
||||
return todayList
|
||||
.Concat(tomorrowList)
|
||||
.Where(p => p?.Start != null)
|
||||
|
||||
@@ -79,13 +79,10 @@ public class NordPoolInit : INordPoolInit
|
||||
|
||||
private void ScheduleEndOfDayReminder()
|
||||
{
|
||||
_scheduler.ScheduleCron("0 20 * * *", () =>
|
||||
{
|
||||
_ = CheckSkipPriceCapReminderAsync();
|
||||
});
|
||||
_scheduler.ScheduleCron("0 20 * * *", CheckSkipPriceCapReminderAsync);
|
||||
}
|
||||
|
||||
private async Task CheckSkipPriceCapReminderAsync()
|
||||
private void CheckSkipPriceCapReminderAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -111,7 +108,11 @@ public class NordPoolInit : INordPoolInit
|
||||
BoilerSkipPriceCapEntity.StateChanges()
|
||||
.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");
|
||||
_ = RecalculateOnConfigChangeAsync();
|
||||
});
|
||||
@@ -121,8 +122,8 @@ public class NordPoolInit : INordPoolInit
|
||||
{
|
||||
try
|
||||
{
|
||||
var (intervalCount, priceCap, skipPriceCap) = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(intervalCount, priceCap, skipPriceCap);
|
||||
var result = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(result.IntervalCount, result.PriceCap, result.SkipPriceCap);
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
var (intervalCount, priceCap, skipPriceCap) = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(intervalCount, priceCap, skipPriceCap);
|
||||
var result = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(result.IntervalCount, result.PriceCap, result.SkipPriceCap);
|
||||
|
||||
_logger.LogInformation("NordPool price initialized with current price: {CurrentPrice} {Unit}", _priceService.CurrentPrice, UnitOfMeasurement);
|
||||
}
|
||||
@@ -218,8 +219,8 @@ public class NordPoolInit : INordPoolInit
|
||||
|
||||
_priceService.UpdateCurrentPrice();
|
||||
|
||||
var (intervalCount, priceCap, skipPriceCap) = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(intervalCount, priceCap, skipPriceCap);
|
||||
var result = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(result.IntervalCount, result.PriceCap, result.SkipPriceCap);
|
||||
|
||||
_logger.LogInformation("NordPool prices updated successfully. Current price: {CurrentPrice} {Unit}", _priceService.CurrentPrice, UnitOfMeasurement);
|
||||
}
|
||||
@@ -234,7 +235,7 @@ public class NordPoolInit : INordPoolInit
|
||||
await _entityService.CleanupEntitiesAsync();
|
||||
}
|
||||
|
||||
private (int IntervalCount, double PriceCap, bool SkipPriceCap) UpdateCheapestIntervals()
|
||||
private CheapestIntervalsUpdateResult UpdateCheapestIntervals()
|
||||
{
|
||||
var boilerConfig = ReadBoilerConfig();
|
||||
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");
|
||||
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);
|
||||
@@ -260,7 +266,12 @@ public class NordPoolInit : INordPoolInit
|
||||
CheapestIntervals = [];
|
||||
}
|
||||
|
||||
return (boilerConfig.IntervalCount, boilerConfig.PriceCap, boilerConfig.SkipPriceCap);
|
||||
return new CheapestIntervalsUpdateResult
|
||||
{
|
||||
IntervalCount = boilerConfig.IntervalCount,
|
||||
PriceCap = boilerConfig.PriceCap,
|
||||
SkipPriceCap = boilerConfig.SkipPriceCap
|
||||
};
|
||||
}
|
||||
|
||||
private BoilerConfig ReadBoilerConfig()
|
||||
|
||||
Reference in New Issue
Block a user