feat: migrate boiler control implementation to BoilerMonitor namespace and add budget handling logic
This commit is contained in:
@@ -11,24 +11,19 @@ namespace NordPoolManager;
|
||||
public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoolEntityService> logger) : INordPoolEntityService
|
||||
{
|
||||
private const string LastUpdateTimeEntityId = "sensor.nordpool_last_update_time";
|
||||
private const string CurrentPriceEntityId = "sensor.nordpool_current_price";
|
||||
private const string TodayPricesEntityId = "sensor.nordpool_today_prices";
|
||||
private const string TomorrowPricesEntityId = "sensor.nordpool_tomorrow_prices";
|
||||
private const string TodayTomorrowPricesEntityId = "sensor.nordpool_today_tomorrow_prices";
|
||||
private const string CheapestIntervalsEntityId = "sensor.nordpool_cheapest_intervals";
|
||||
private const string BoilerWaterReadyEntityId = "sensor.nordpool_boiler_water_ready";
|
||||
private const string BoilerSkipPriceCapEntityId = "switch.nordpool_boiler_skip_price_cap";
|
||||
|
||||
private static readonly string[] AllEntityIds =
|
||||
[
|
||||
LastUpdateTimeEntityId,
|
||||
CurrentPriceEntityId,
|
||||
NordPoolConsts.CurrentPriceEntityId,
|
||||
TodayPricesEntityId,
|
||||
TomorrowPricesEntityId,
|
||||
TodayTomorrowPricesEntityId,
|
||||
CheapestIntervalsEntityId,
|
||||
BoilerWaterReadyEntityId,
|
||||
BoilerSkipPriceCapEntityId
|
||||
CheapestIntervalsEntityId
|
||||
];
|
||||
|
||||
public async Task CreateEntitiesAsync(string? unitOfMeasurement, CancellationToken cancellationToken = default)
|
||||
@@ -65,9 +60,7 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
ResetTodayPricesAsync(),
|
||||
ResetTomorrowPricesAsync(),
|
||||
ResetTodayTomorrowPricesAsync(),
|
||||
ResetCheapestIntervalsAsync(),
|
||||
ResetBoilerWaterReadyAsync(),
|
||||
ResetBoilerSkipPriceCapAsync()
|
||||
ResetCheapestIntervalsAsync()
|
||||
).ConfigureAwait(false);
|
||||
|
||||
logger.LogInformation("All NordPool entity states reset to defaults");
|
||||
@@ -84,8 +77,8 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
|
||||
private async Task ResetCurrentPriceAsync()
|
||||
{
|
||||
await SetEntityStateSafe(CurrentPriceEntityId, string.Empty).ConfigureAwait(false);
|
||||
await SetEntityAttributesSafe(CurrentPriceEntityId, new
|
||||
await SetEntityStateSafe(NordPoolConsts.CurrentPriceEntityId, string.Empty).ConfigureAwait(false);
|
||||
await SetEntityAttributesSafe(NordPoolConsts.CurrentPriceEntityId, new
|
||||
{
|
||||
unit_of_measurement = (string?)null,
|
||||
device_class = "monetary",
|
||||
@@ -150,24 +143,6 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ResetBoilerWaterReadyAsync()
|
||||
{
|
||||
await SetEntityStateSafe(BoilerWaterReadyEntityId, "unknown").ConfigureAwait(false);
|
||||
await SetEntityAttributesSafe(BoilerWaterReadyEntityId, new
|
||||
{
|
||||
boiler_switch_state = "unknown",
|
||||
boiler_power_watts = 0.0,
|
||||
power_threshold_watts = NordPoolConsts.WaterReadyPowerThresholdWatts,
|
||||
friendly_name = "Boiler water ready",
|
||||
last_updated = (string?)null
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ResetBoilerSkipPriceCapAsync()
|
||||
{
|
||||
await SetEntityStateSafe(BoilerSkipPriceCapEntityId, "OFF").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task UpdateEntitiesAsync(EntityUpdateContext ctx, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
@@ -182,8 +157,7 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
|
||||
if (mergedPrices.Count > NordPoolConsts.MaxExpectedPriceEntries)
|
||||
{
|
||||
logger.LogWarning(
|
||||
"Merged price count ({Count}) exceeds expected maximum ({Max}). Attribute payload may be oversized",
|
||||
logger.LogWarning("Merged price count ({Count}) exceeds expected maximum ({Max}). Attribute payload may be oversized",
|
||||
mergedPrices.Count, NordPoolConsts.MaxExpectedPriceEntries);
|
||||
}
|
||||
|
||||
@@ -194,7 +168,6 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
logger.LogInformation("Updating sensor.nordpool_tomorrow_prices with {Count} prices", tomorrowPricesCount);
|
||||
logger.LogInformation("Updating sensor.nordpool_today_tomorrow_prices with {Count} merged prices", mergedPrices.Count);
|
||||
logger.LogInformation("Updating sensor.nordpool_cheapest_intervals with {Count} intervals, is_now_cheap={IsNow}", cheapestCount, ctx.IsNowCheap);
|
||||
logger.LogInformation("Updating sensor.nordpool_boiler_water_ready: {WaterReady} (switch={Switch}, power={Power}W)", ctx.WaterReady, ctx.BoilerSwitchState, ctx.BoilerPowerWatts);
|
||||
|
||||
var updateTasks = new[]
|
||||
{
|
||||
@@ -203,8 +176,7 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
UpdateTodayPricesAsync(ctx, todayPricesCount, timestamp),
|
||||
UpdateTomorrowPricesAsync(ctx, tomorrowPricesCount, timestamp),
|
||||
UpdateTodayTomorrowPricesAsync(mergedPrices, updateTimeState, timestamp),
|
||||
UpdateCheapestIntervalsAsync(ctx, cheapestCount, timestamp),
|
||||
UpdateBoilerWaterReadyAsync(ctx, timestamp)
|
||||
UpdateCheapestIntervalsAsync(ctx, cheapestCount, timestamp)
|
||||
};
|
||||
|
||||
await Task.WhenAll(updateTasks).ConfigureAwait(false);
|
||||
@@ -219,8 +191,8 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
|
||||
private async Task UpdateCurrentPriceAsync(string priceValue, EntityUpdateContext ctx, string timestamp)
|
||||
{
|
||||
await SetEntityStateSafe(CurrentPriceEntityId, priceValue).ConfigureAwait(false);
|
||||
await SetEntityAttributesSafe(CurrentPriceEntityId, new
|
||||
await SetEntityStateSafe(NordPoolConsts.CurrentPriceEntityId, priceValue).ConfigureAwait(false);
|
||||
await SetEntityAttributesSafe(NordPoolConsts.CurrentPriceEntityId, new
|
||||
{
|
||||
unit_of_measurement = ctx.UnitOfMeasurement,
|
||||
device_class = "monetary",
|
||||
@@ -286,26 +258,13 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UpdateBoilerWaterReadyAsync(EntityUpdateContext ctx, string timestamp)
|
||||
{
|
||||
await SetEntityStateSafe(BoilerWaterReadyEntityId, ctx.WaterReady ? "yes" : "no").ConfigureAwait(false);
|
||||
await SetEntityAttributesSafe(BoilerWaterReadyEntityId, new
|
||||
{
|
||||
boiler_switch_state = ctx.BoilerSwitchState ?? "unknown",
|
||||
boiler_power_watts = ctx.BoilerPowerWatts,
|
||||
power_threshold_watts = NordPoolConsts.WaterReadyPowerThresholdWatts,
|
||||
friendly_name = "Boiler water ready",
|
||||
last_updated = timestamp
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
try
|
||||
{
|
||||
var removeTasks = AllEntityIds.Select(entityId => entityManager.RemoveEntity(entityId));
|
||||
var removeTasks = AllEntityIds.Select(entityManager.RemoveEntity);
|
||||
await Task.WhenAll(removeTasks).ConfigureAwait(false);
|
||||
|
||||
logger.LogInformation("NordPool entities cleaned up successfully");
|
||||
@@ -319,39 +278,32 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
|
||||
internal ICollection<NordPoolEntity> GetInitEntitiesList(string? unitOfMeasurement)
|
||||
{
|
||||
var entries = new List<NordPoolEntity>();
|
||||
var currentPriceEntityId = NordPoolConsts.CurrentPriceEntityId;
|
||||
var currentPriceDomainSeparator = currentPriceEntityId.IndexOf('.', StringComparison.Ordinal);
|
||||
var currentPriceDomain = currentPriceEntityId[..currentPriceDomainSeparator];
|
||||
var currentPriceObjectId = currentPriceEntityId[(currentPriceDomainSeparator + 1)..];
|
||||
|
||||
entries.AddRange([
|
||||
return
|
||||
[
|
||||
new NordPoolEntity("sensor", "nordpool_last_update_time", "NordPool last update time"),
|
||||
new NordPoolEntity("sensor", "nordpool_current_price", "NordPool current price")
|
||||
new NordPoolEntity(currentPriceDomain, currentPriceObjectId, "NordPool current price")
|
||||
{
|
||||
Options = new EntityCreationOptions
|
||||
{
|
||||
UniqueId = CurrentPriceEntityId,
|
||||
UniqueId = currentPriceEntityId,
|
||||
Name = "NordPool current price"
|
||||
},
|
||||
AdditionalConfig = new
|
||||
AdditionalConfig = NordPoolEntity.CreateAdditionalConfig(new
|
||||
{
|
||||
unit_of_measurement = unitOfMeasurement,
|
||||
state_class = "measurement"
|
||||
}
|
||||
})
|
||||
},
|
||||
new NordPoolEntity("sensor", "nordpool_today_prices", "NordPool today prices"),
|
||||
new NordPoolEntity("sensor", "nordpool_tomorrow_prices", "NordPool tomorrow prices"),
|
||||
new NordPoolEntity("sensor", "nordpool_today_tomorrow_prices", "NordPool today and tomorrow prices"),
|
||||
new NordPoolEntity("sensor", "nordpool_cheapest_intervals", "NordPool cheapest boiler intervals"),
|
||||
new NordPoolEntity("sensor", "nordpool_boiler_water_ready", "Boiler water ready"),
|
||||
new NordPoolEntity("switch", "nordpool_boiler_skip_price_cap", "Skip price cap, heat until ready")
|
||||
{
|
||||
AdditionalConfig = new
|
||||
{
|
||||
optimistic = true,
|
||||
icon = "mdi:water-thermometer-outline"
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
return entries;
|
||||
new NordPoolEntity("sensor", "nordpool_cheapest_intervals", "NordPool cheapest boiler intervals")
|
||||
];
|
||||
}
|
||||
|
||||
private static List<NordPoolDayPrice> MergeTodayTomorrowPrices(NordPoolPrice? today, NordPoolPrice? tomorrow)
|
||||
|
||||
Reference in New Issue
Block a user