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
+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);