Add manual MQTT recreation and NordPool price fetch buttons

This commit is contained in:
2026-07-08 16:56:32 +03:00
parent 2706a6b188
commit f176a1b8fc
11 changed files with 458 additions and 22 deletions
+4 -3
View File
@@ -20,9 +20,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="16.1.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.5" />
<PackageReference Include="NetDaemon.Extensions.Scheduling" Version="26.11.0" />
<PackageReference Include="AutoMapper" Version="16.1.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.5" />
<PackageReference Include="NetDaemon.Extensions.Scheduling" Version="26.11.0" />
<PackageReference Include="NetDaemon.HassModel" Version="26.11.0" />
</ItemGroup>
</Project>
+15 -3
View File
@@ -23,7 +23,8 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
TodayPricesEntityId,
TomorrowPricesEntityId,
TodayTomorrowPricesEntityId,
CheapestIntervalsEntityId
CheapestIntervalsEntityId,
NordPoolConsts.FetchPricesButtonEntityId
];
public async Task CreateEntitiesAsync(string? unitOfMeasurement, CancellationToken cancellationToken = default)
@@ -57,7 +58,8 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
ResetTodayPricesAsync(),
ResetTomorrowPricesAsync(),
ResetTodayTomorrowPricesAsync(),
ResetCheapestIntervalsAsync()
ResetCheapestIntervalsAsync(),
ResetFetchPricesButtonAsync()
).ConfigureAwait(false);
}
@@ -138,6 +140,15 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
}).ConfigureAwait(false);
}
private async Task ResetFetchPricesButtonAsync()
{
await SetEntityStateSafe(NordPoolConsts.FetchPricesButtonEntityId, "unknown").ConfigureAwait(false);
await SetEntityAttributesSafe(NordPoolConsts.FetchPricesButtonEntityId, new
{
friendly_name = "NordPool fetch prices"
}).ConfigureAwait(false);
}
public async Task UpdateEntitiesAsync(EntityUpdateContext ctx, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
@@ -289,7 +300,8 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
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_cheapest_intervals", "NordPool cheapest boiler intervals"),
new NordPoolEntity("button", "nordpool_fetch_prices", "NordPool fetch prices")
];
}
+38
View File
@@ -6,6 +6,8 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NetDaemon.AppModel;
using NetDaemon.Extensions.Scheduler;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using NordPool.Models;
using NordPoolManager.Interface;
using NordPoolManager.Models;
@@ -16,6 +18,7 @@ namespace NordPoolManager;
public class NordPoolInit : INordPoolInit
{
private readonly ILogger<NordPoolInit> _logger;
private readonly IHaContext _haContext;
private readonly INordPoolPriceService _priceService;
private readonly INordPoolEntityService _entityService;
private readonly ICheapestIntervalsFinder _cheapestIntervalsFinder;
@@ -29,6 +32,7 @@ public class NordPoolInit : INordPoolInit
public NordPoolInit(
Entities entities,
IHaContext haContext,
ILogger<NordPoolInit> logger,
INordPoolPriceService priceService,
INordPoolEntityService entityService,
@@ -39,6 +43,7 @@ public class NordPoolInit : INordPoolInit
TimeProvider timeProvider)
{
_logger = logger;
_haContext = haContext;
_priceService = priceService;
_entityService = entityService;
_scheduler = scheduler;
@@ -58,6 +63,7 @@ public class NordPoolInit : INordPoolInit
try
{
await InitializeAsync();
SubscribeToFetchPricesButton();
SchedulePeriodicUpdates();
}
catch (Exception ex)
@@ -145,6 +151,38 @@ public class NordPoolInit : INordPoolInit
}
}
private void SubscribeToFetchPricesButton()
{
var button = new Entity(_haContext, NordPoolConsts.FetchPricesButtonEntityId);
button.StateChanges()
.Subscribe(_ =>
{
_logger.LogInformation("Fetch prices button pressed, triggering manual NordPool price update");
Task.Run(HandleFetchPricesAsync);
});
}
private async Task HandleFetchPricesAsync()
{
try
{
_logger.LogInformation("Manual fetch: forcing fresh NordPool price retrieval");
await _priceService.FetchPricesAsync();
_priceService.UpdateCurrentPrice();
var boilerConfig = _boilerConfigProvider.GetCurrentConfiguration();
UpdateCheapestIntervals(boilerConfig);
await UpdateEntitiesAsync(boilerConfig);
_logger.LogInformation("Manual fetch and update completed");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error during manual NordPool price fetch");
}
}
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
{
await _entityService.CleanupEntitiesAsync(cancellationToken);