|
|
@@ -8,7 +8,7 @@ using NordPoolManager.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace NordPoolManager;
|
|
|
|
namespace NordPoolManager;
|
|
|
|
|
|
|
|
|
|
|
|
public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoolEntityService> logger) : INordPoolEntityService
|
|
|
|
public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoolEntityService> logger) : INordPoolEntityService
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private const string LastUpdateTimeEntityId = "sensor.nordpool_last_update_time";
|
|
|
|
private const string LastUpdateTimeEntityId = "sensor.nordpool_last_update_time";
|
|
|
|
private const string CurrentPriceEntityId = "sensor.nordpool_current_price";
|
|
|
|
private const string CurrentPriceEntityId = "sensor.nordpool_current_price";
|
|
|
@@ -31,27 +31,32 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|
|
|
BoilerSkipPriceCapEntityId
|
|
|
|
BoilerSkipPriceCapEntityId
|
|
|
|
];
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
public async Task CreateEntitiesAsync(string? unitOfMeasurement)
|
|
|
|
public async Task CreateEntitiesAsync(string? unitOfMeasurement, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
var entitiesToCreate = GetInitEntitiesList(unitOfMeasurement);
|
|
|
|
var entitiesToCreate = GetInitEntitiesList(unitOfMeasurement);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var nordPoolEntity in entitiesToCreate)
|
|
|
|
var createTasks = entitiesToCreate.Select(async nordPoolEntity =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
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).ConfigureAwait(false);
|
|
|
|
await entityManager.CreateEntityAsync(nordPoolEntity);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.LogError(ex, "Failed to create MQTT entity: {EntityId}", nordPoolEntity.EntityId);
|
|
|
|
logger.LogError(ex, "Failed to create MQTT entity: {EntityId}", nordPoolEntity.EntityId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(createTasks).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task ResetEntitiesAsync()
|
|
|
|
public async Task ResetEntitiesAsync(CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Resetting all NordPool entity states to defaults");
|
|
|
|
logger.LogInformation("Resetting all NordPool entity states to defaults");
|
|
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(
|
|
|
|
await Task.WhenAll(
|
|
|
@@ -63,23 +68,23 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|
|
|
ResetCheapestIntervalsAsync(),
|
|
|
|
ResetCheapestIntervalsAsync(),
|
|
|
|
ResetBoilerWaterReadyAsync(),
|
|
|
|
ResetBoilerWaterReadyAsync(),
|
|
|
|
ResetBoilerSkipPriceCapAsync()
|
|
|
|
ResetBoilerSkipPriceCapAsync()
|
|
|
|
);
|
|
|
|
).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("All NordPool entity states reset to defaults");
|
|
|
|
logger.LogInformation("All NordPool entity states reset to defaults");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ResetLastUpdateTimeAsync()
|
|
|
|
private async Task ResetLastUpdateTimeAsync()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await SetEntityStateSafe(LastUpdateTimeEntityId, "unknown");
|
|
|
|
await SetEntityStateSafe(LastUpdateTimeEntityId, "unknown").ConfigureAwait(false);
|
|
|
|
await SetEntityAttributesSafe(LastUpdateTimeEntityId, new
|
|
|
|
await SetEntityAttributesSafe(LastUpdateTimeEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
friendly_name = "NordPool last update time"
|
|
|
|
friendly_name = "NordPool last update time"
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ResetCurrentPriceAsync()
|
|
|
|
private async Task ResetCurrentPriceAsync()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await SetEntityStateSafe(CurrentPriceEntityId, "0");
|
|
|
|
await SetEntityStateSafe(CurrentPriceEntityId, "0").ConfigureAwait(false);
|
|
|
|
await SetEntityAttributesSafe(CurrentPriceEntityId, new
|
|
|
|
await SetEntityAttributesSafe(CurrentPriceEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
unit_of_measurement = (string?)null,
|
|
|
|
unit_of_measurement = (string?)null,
|
|
|
@@ -90,46 +95,46 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|
|
|
last_api_fetch = (string?)null,
|
|
|
|
last_api_fetch = (string?)null,
|
|
|
|
using_cached_data = false,
|
|
|
|
using_cached_data = false,
|
|
|
|
current_quarter_index = 0
|
|
|
|
current_quarter_index = 0
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ResetTodayPricesAsync()
|
|
|
|
private async Task ResetTodayPricesAsync()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await SetEntityStateSafe(TodayPricesEntityId, "0");
|
|
|
|
await SetEntityStateSafe(TodayPricesEntityId, "0").ConfigureAwait(false);
|
|
|
|
await SetEntityAttributesSafe(TodayPricesEntityId, new
|
|
|
|
await SetEntityAttributesSafe(TodayPricesEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
prices = Array.Empty<NordPoolDayPrice>(),
|
|
|
|
prices = Array.Empty<NordPoolDayPrice>(),
|
|
|
|
friendly_name = "NordPool today prices",
|
|
|
|
friendly_name = "NordPool today prices",
|
|
|
|
last_updated = (string?)null
|
|
|
|
last_updated = (string?)null
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ResetTomorrowPricesAsync()
|
|
|
|
private async Task ResetTomorrowPricesAsync()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await SetEntityStateSafe(TomorrowPricesEntityId, "0");
|
|
|
|
await SetEntityStateSafe(TomorrowPricesEntityId, "0").ConfigureAwait(false);
|
|
|
|
await SetEntityAttributesSafe(TomorrowPricesEntityId, new
|
|
|
|
await SetEntityAttributesSafe(TomorrowPricesEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
prices = Array.Empty<NordPoolDayPrice>(),
|
|
|
|
prices = Array.Empty<NordPoolDayPrice>(),
|
|
|
|
friendly_name = "NordPool tomorrow prices",
|
|
|
|
friendly_name = "NordPool tomorrow prices",
|
|
|
|
last_updated = (string?)null
|
|
|
|
last_updated = (string?)null
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ResetTodayTomorrowPricesAsync()
|
|
|
|
private async Task ResetTodayTomorrowPricesAsync()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await SetEntityStateSafe(TodayTomorrowPricesEntityId, "unknown");
|
|
|
|
await SetEntityStateSafe(TodayTomorrowPricesEntityId, "unknown").ConfigureAwait(false);
|
|
|
|
await SetEntityAttributesSafe(TodayTomorrowPricesEntityId, new
|
|
|
|
await SetEntityAttributesSafe(TodayTomorrowPricesEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
prices = Array.Empty<NordPoolDayPrice>(),
|
|
|
|
prices = Array.Empty<NordPoolDayPrice>(),
|
|
|
|
prices_count = 0,
|
|
|
|
prices_count = 0,
|
|
|
|
friendly_name = "NordPool today and tomorrow prices",
|
|
|
|
friendly_name = "NordPool today and tomorrow prices",
|
|
|
|
last_updated = (string?)null
|
|
|
|
last_updated = (string?)null
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ResetCheapestIntervalsAsync()
|
|
|
|
private async Task ResetCheapestIntervalsAsync()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await SetEntityStateSafe(CheapestIntervalsEntityId, "0");
|
|
|
|
await SetEntityStateSafe(CheapestIntervalsEntityId, "0").ConfigureAwait(false);
|
|
|
|
await SetEntityAttributesSafe(CheapestIntervalsEntityId, new
|
|
|
|
await SetEntityAttributesSafe(CheapestIntervalsEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
intervals = Array.Empty<CheapestIntervalResult>(),
|
|
|
|
intervals = Array.Empty<CheapestIntervalResult>(),
|
|
|
@@ -142,39 +147,71 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|
|
|
next_cheap_interval = (string?)null,
|
|
|
|
next_cheap_interval = (string?)null,
|
|
|
|
friendly_name = "NordPool cheapest boiler intervals",
|
|
|
|
friendly_name = "NordPool cheapest boiler intervals",
|
|
|
|
last_updated = (string?)null
|
|
|
|
last_updated = (string?)null
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ResetBoilerWaterReadyAsync()
|
|
|
|
private async Task ResetBoilerWaterReadyAsync()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await SetEntityStateSafe(BoilerWaterReadyEntityId, "unknown");
|
|
|
|
await SetEntityStateSafe(BoilerWaterReadyEntityId, "unknown").ConfigureAwait(false);
|
|
|
|
await SetEntityAttributesSafe(BoilerWaterReadyEntityId, new
|
|
|
|
await SetEntityAttributesSafe(BoilerWaterReadyEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boiler_switch_state = "unknown",
|
|
|
|
boiler_switch_state = "unknown",
|
|
|
|
boiler_power_watts = 0.0,
|
|
|
|
boiler_power_watts = 0.0,
|
|
|
|
power_threshold_watts = 5.0,
|
|
|
|
power_threshold_watts = NordPoolConsts.WaterReadyPowerThresholdWatts,
|
|
|
|
friendly_name = "Boiler water ready",
|
|
|
|
friendly_name = "Boiler water ready",
|
|
|
|
last_updated = (string?)null
|
|
|
|
last_updated = (string?)null
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ResetBoilerSkipPriceCapAsync()
|
|
|
|
private async Task ResetBoilerSkipPriceCapAsync()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await SetEntityStateSafe(BoilerSkipPriceCapEntityId, "OFF");
|
|
|
|
await SetEntityStateSafe(BoilerSkipPriceCapEntityId, "OFF").ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UpdateEntitiesAsync(EntityUpdateContext ctx)
|
|
|
|
public async Task UpdateEntitiesAsync(EntityUpdateContext ctx, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
var utcNow = DateTime.UtcNow;
|
|
|
|
var utcNow = DateTime.UtcNow;
|
|
|
|
var timestamp = utcNow.ToString("O");
|
|
|
|
var timestamp = utcNow.ToString("O");
|
|
|
|
|
|
|
|
var updateTimeState = utcNow.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
await SetEntityStateSafe(LastUpdateTimeEntityId, utcNow.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var priceValue = ctx.CurrentPrice.ToString("F2", CultureInfo.InvariantCulture);
|
|
|
|
var priceValue = ctx.CurrentPrice.ToString("F2", CultureInfo.InvariantCulture);
|
|
|
|
|
|
|
|
var todayPricesCount = ctx.TodayNordPoolPrice?.Prices?.Count ?? 0;
|
|
|
|
|
|
|
|
var tomorrowPricesCount = ctx.TomorrowNordPoolPrice?.Prices?.Count ?? 0;
|
|
|
|
|
|
|
|
var mergedPrices = MergeTodayTomorrowPrices(ctx.TodayNordPoolPrice, ctx.TomorrowNordPoolPrice);
|
|
|
|
|
|
|
|
var cheapestCount = ctx.CheapestIntervals?.Count ?? 0;
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Updating nordpool_current_price with value: {Price} {Unit}", priceValue, ctx.UnitOfMeasurement);
|
|
|
|
logger.LogInformation("Updating nordpool_current_price with value: {Price} {Unit}", priceValue, ctx.UnitOfMeasurement);
|
|
|
|
|
|
|
|
logger.LogInformation("Updating sensor.nordpool_today_prices with {Count} prices", todayPricesCount);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
await SetEntityStateSafe(CurrentPriceEntityId, priceValue);
|
|
|
|
var updateTasks = new[]
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
UpdateLastUpdateTimeAsync(updateTimeState),
|
|
|
|
|
|
|
|
UpdateCurrentPriceAsync(priceValue, ctx, timestamp),
|
|
|
|
|
|
|
|
UpdateTodayPricesAsync(ctx, todayPricesCount, timestamp),
|
|
|
|
|
|
|
|
UpdateTomorrowPricesAsync(ctx, tomorrowPricesCount, timestamp),
|
|
|
|
|
|
|
|
UpdateTodayTomorrowPricesAsync(mergedPrices, updateTimeState, timestamp),
|
|
|
|
|
|
|
|
UpdateCheapestIntervalsAsync(ctx, cheapestCount, timestamp),
|
|
|
|
|
|
|
|
UpdateBoilerWaterReadyAsync(ctx, timestamp)
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(updateTasks).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("NordPool entities updated successfully. Today prices count: {TodayCount}, Tomorrow prices count: {TomorrowCount}", todayPricesCount, tomorrowPricesCount);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task UpdateLastUpdateTimeAsync(string updateTimeState)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
await SetEntityStateSafe(LastUpdateTimeEntityId, updateTimeState).ConfigureAwait(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task UpdateCurrentPriceAsync(string priceValue, EntityUpdateContext ctx, string timestamp)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
await SetEntityStateSafe(CurrentPriceEntityId, priceValue).ConfigureAwait(false);
|
|
|
|
await SetEntityAttributesSafe(CurrentPriceEntityId, new
|
|
|
|
await SetEntityAttributesSafe(CurrentPriceEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
unit_of_measurement = ctx.UnitOfMeasurement,
|
|
|
|
unit_of_measurement = ctx.UnitOfMeasurement,
|
|
|
@@ -185,47 +222,46 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|
|
|
last_api_fetch = ctx.LastApiUpdateDate?.ToString("O"),
|
|
|
|
last_api_fetch = ctx.LastApiUpdateDate?.ToString("O"),
|
|
|
|
using_cached_data = ctx.UsingCachedData,
|
|
|
|
using_cached_data = ctx.UsingCachedData,
|
|
|
|
current_quarter_index = ctx.DayQuartersIndex
|
|
|
|
current_quarter_index = ctx.DayQuartersIndex
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var todayPricesCount = ctx.TodayNordPoolPrice?.Prices?.Count ?? 0;
|
|
|
|
private async Task UpdateTodayPricesAsync(EntityUpdateContext ctx, int todayPricesCount, string timestamp)
|
|
|
|
logger.LogInformation("Updating sensor.nordpool_today_prices with {Count} prices", todayPricesCount);
|
|
|
|
{
|
|
|
|
|
|
|
|
await SetEntityStateSafe(TodayPricesEntityId, todayPricesCount.ToString()).ConfigureAwait(false);
|
|
|
|
await SetEntityStateSafe(TodayPricesEntityId, todayPricesCount.ToString());
|
|
|
|
|
|
|
|
await SetEntityAttributesSafe(TodayPricesEntityId, new
|
|
|
|
await SetEntityAttributesSafe(TodayPricesEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
prices = ctx.TodayNordPoolPrice?.Prices ?? Array.Empty<NordPoolDayPrice>(),
|
|
|
|
prices = ctx.TodayNordPoolPrice?.Prices ?? Array.Empty<NordPoolDayPrice>(),
|
|
|
|
friendly_name = "NordPool today prices",
|
|
|
|
friendly_name = "NordPool today prices",
|
|
|
|
last_updated = timestamp
|
|
|
|
last_updated = timestamp
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var tomorrowPricesCount = ctx.TomorrowNordPoolPrice?.Prices?.Count ?? 0;
|
|
|
|
private async Task UpdateTomorrowPricesAsync(EntityUpdateContext ctx, int tomorrowPricesCount, string timestamp)
|
|
|
|
logger.LogInformation("Updating sensor.nordpool_tomorrow_prices with {Count} prices", tomorrowPricesCount);
|
|
|
|
{
|
|
|
|
|
|
|
|
await SetEntityStateSafe(TomorrowPricesEntityId, tomorrowPricesCount.ToString()).ConfigureAwait(false);
|
|
|
|
await SetEntityStateSafe(TomorrowPricesEntityId, tomorrowPricesCount.ToString());
|
|
|
|
|
|
|
|
await SetEntityAttributesSafe(TomorrowPricesEntityId, new
|
|
|
|
await SetEntityAttributesSafe(TomorrowPricesEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
prices = ctx.TomorrowNordPoolPrice?.Prices ?? Array.Empty<NordPoolDayPrice>(),
|
|
|
|
prices = ctx.TomorrowNordPoolPrice?.Prices ?? Array.Empty<NordPoolDayPrice>(),
|
|
|
|
friendly_name = "NordPool tomorrow prices",
|
|
|
|
friendly_name = "NordPool tomorrow prices",
|
|
|
|
last_updated = timestamp
|
|
|
|
last_updated = timestamp
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var mergedPrices = MergeTodayTomorrowPrices(ctx.TodayNordPoolPrice, ctx.TomorrowNordPoolPrice);
|
|
|
|
private async Task UpdateTodayTomorrowPricesAsync(List<NordPoolDayPrice> mergedPrices, string updateTimeState, string timestamp)
|
|
|
|
var updateTimeState = utcNow.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
{
|
|
|
|
logger.LogInformation("Updating sensor.nordpool_today_tomorrow_prices with {Count} merged prices", mergedPrices.Count);
|
|
|
|
await SetEntityStateSafe(TodayTomorrowPricesEntityId, updateTimeState).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
await SetEntityStateSafe(TodayTomorrowPricesEntityId, updateTimeState);
|
|
|
|
|
|
|
|
await SetEntityAttributesSafe(TodayTomorrowPricesEntityId, new
|
|
|
|
await SetEntityAttributesSafe(TodayTomorrowPricesEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
prices = mergedPrices,
|
|
|
|
prices = mergedPrices,
|
|
|
|
prices_count = mergedPrices.Count,
|
|
|
|
prices_count = mergedPrices.Count,
|
|
|
|
friendly_name = "NordPool today and tomorrow prices",
|
|
|
|
friendly_name = "NordPool today and tomorrow prices",
|
|
|
|
last_updated = timestamp
|
|
|
|
last_updated = timestamp
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var cheapestCount = ctx.CheapestIntervals?.Count ?? 0;
|
|
|
|
private async Task UpdateCheapestIntervalsAsync(EntityUpdateContext ctx, int cheapestCount, string timestamp)
|
|
|
|
logger.LogInformation("Updating sensor.nordpool_cheapest_intervals with {Count} intervals, is_now_cheap={IsNow}", cheapestCount, ctx.IsNowCheap);
|
|
|
|
{
|
|
|
|
|
|
|
|
await SetEntityStateSafe(CheapestIntervalsEntityId, cheapestCount.ToString()).ConfigureAwait(false);
|
|
|
|
await SetEntityStateSafe(CheapestIntervalsEntityId, cheapestCount.ToString());
|
|
|
|
|
|
|
|
await SetEntityAttributesSafe(CheapestIntervalsEntityId, new
|
|
|
|
await SetEntityAttributesSafe(CheapestIntervalsEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
intervals = ctx.CheapestIntervals ?? [],
|
|
|
|
intervals = ctx.CheapestIntervals ?? [],
|
|
|
@@ -234,36 +270,35 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|
|
|
configured_price_cap = ctx.ConfiguredPriceCap,
|
|
|
|
configured_price_cap = ctx.ConfiguredPriceCap,
|
|
|
|
skip_price_cap = ctx.SkipPriceCap,
|
|
|
|
skip_price_cap = ctx.SkipPriceCap,
|
|
|
|
configured_ready_by = ctx.ConfiguredReadyBy,
|
|
|
|
configured_ready_by = ctx.ConfiguredReadyBy,
|
|
|
|
total_heating_minutes = cheapestCount * 15,
|
|
|
|
total_heating_minutes = cheapestCount * NordPoolConsts.MinutesPerInterval,
|
|
|
|
is_now_cheap = ctx.IsNowCheap,
|
|
|
|
is_now_cheap = ctx.IsNowCheap,
|
|
|
|
next_cheap_interval = ctx.NextCheapIntervalStart?.ToString("O"),
|
|
|
|
next_cheap_interval = ctx.NextCheapIntervalStart?.ToString("O"),
|
|
|
|
friendly_name = "NordPool cheapest boiler intervals",
|
|
|
|
friendly_name = "NordPool cheapest boiler intervals",
|
|
|
|
last_updated = timestamp
|
|
|
|
last_updated = timestamp
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Updating sensor.nordpool_boiler_water_ready: {WaterReady} (switch={Switch}, power={Power}W)", ctx.WaterReady, ctx.BoilerSwitchState, ctx.BoilerPowerWatts);
|
|
|
|
private async Task UpdateBoilerWaterReadyAsync(EntityUpdateContext ctx, string timestamp)
|
|
|
|
|
|
|
|
{
|
|
|
|
await SetEntityStateSafe(BoilerWaterReadyEntityId, ctx.WaterReady ? "yes" : "no");
|
|
|
|
await SetEntityStateSafe(BoilerWaterReadyEntityId, ctx.WaterReady ? "yes" : "no").ConfigureAwait(false);
|
|
|
|
await SetEntityAttributesSafe(BoilerWaterReadyEntityId, new
|
|
|
|
await SetEntityAttributesSafe(BoilerWaterReadyEntityId, new
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boiler_switch_state = ctx.BoilerSwitchState ?? "unknown",
|
|
|
|
boiler_switch_state = ctx.BoilerSwitchState ?? "unknown",
|
|
|
|
boiler_power_watts = ctx.BoilerPowerWatts,
|
|
|
|
boiler_power_watts = ctx.BoilerPowerWatts,
|
|
|
|
power_threshold_watts = 5.0,
|
|
|
|
power_threshold_watts = NordPoolConsts.WaterReadyPowerThresholdWatts,
|
|
|
|
friendly_name = "Boiler water ready",
|
|
|
|
friendly_name = "Boiler water ready",
|
|
|
|
last_updated = timestamp
|
|
|
|
last_updated = timestamp
|
|
|
|
});
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("NordPool entities updated successfully. Today prices count: {TodayCount}, Tomorrow prices count: {TomorrowCount}", todayPricesCount, tomorrowPricesCount);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task CleanupEntitiesAsync()
|
|
|
|
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
foreach (var entityId in AllEntityIds)
|
|
|
|
var removeTasks = AllEntityIds.Select(entityId => entityManager.RemoveEntity(entityId));
|
|
|
|
{
|
|
|
|
await Task.WhenAll(removeTasks).ConfigureAwait(false);
|
|
|
|
await entityManager.RemoveEntity(entityId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("NordPool entities cleaned up successfully");
|
|
|
|
logger.LogInformation("NordPool entities cleaned up successfully");
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -318,8 +353,8 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|
|
|
|
|
|
|
|
|
|
|
return todayList
|
|
|
|
return todayList
|
|
|
|
.Concat(tomorrowList)
|
|
|
|
.Concat(tomorrowList)
|
|
|
|
.Where(p => p?.Start != null)
|
|
|
|
.Where(p => p is { Start: not null })
|
|
|
|
.OrderBy(p => p!.Start!.Value)
|
|
|
|
.OrderBy(p => p.Start!.Value)
|
|
|
|
.ToList();
|
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@@ -327,7 +362,7 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await entityManager.SetEntityState(entityId, state);
|
|
|
|
await entityManager.SetEntityState(entityId, state).ConfigureAwait(false);
|
|
|
|
logger.LogDebug("Successfully set state for {EntityId}: {State}", entityId, state);
|
|
|
|
logger.LogDebug("Successfully set state for {EntityId}: {State}", entityId, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
@@ -340,7 +375,7 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await entityManager.SetEntityAttributes(entityId, attributes);
|
|
|
|
await entityManager.SetEntityAttributes(entityId, attributes).ConfigureAwait(false);
|
|
|
|
logger.LogDebug("Successfully set attributes for {EntityId}", entityId);
|
|
|
|
logger.LogDebug("Successfully set attributes for {EntityId}", entityId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
|