fix: read NordPool price from HA entity instead of scoped service to fix cross-app price isolation

Made-with: Cursor
This commit is contained in:
Marius
2026-03-17 13:53:10 +02:00
parent 1909eedc6b
commit 89ce8b46ea
3 changed files with 24 additions and 8 deletions
+1 -1
View File
@@ -28,7 +28,7 @@
},
"PowerMonitor": {
"MainMeterEntityId": "sensor.sonoff_100267d63f_power",
"PollIntervalSeconds": 5,
"PollIntervalSeconds": 1,
"SmoothingWindowSize": 10,
"AnomalyThresholdWatts": 500,
"ExcludedSensorPatterns": [
@@ -16,7 +16,6 @@
<ItemGroup>
<ProjectReference Include="..\HomeAssistant.Entities\HomeAssistant.Entities.csproj" />
<ProjectReference Include="..\PowerMonitor.Models\PowerMonitor.Models.csproj" />
<ProjectReference Include="..\NordPool.Manager\NordPool.Manager.csproj" />
</ItemGroup>
<ItemGroup>
+23 -6
View File
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Reactive.Concurrency;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@@ -5,7 +6,6 @@ using NetDaemon.AppModel;
using NetDaemon.Extensions.Scheduler;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using NordPoolManager.Interface;
using PowerMonitorManager.Interface;
using PowerMonitorManager.Models;
using PowerMonitorManager.Models.Consts;
@@ -23,7 +23,8 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
private readonly IOtherPowerCalculationService _calculationService;
private readonly IPowerCostCalculationService _costService;
private readonly IPowerMonitorEntityService _entityService;
private readonly INordPoolPriceService _priceService;
private const string NordPoolCurrentPriceEntityId = "sensor.nordpool_current_price";
private int _mainMeterConsecutiveFailures;
private DateTime? _anomalyStartTime;
@@ -36,8 +37,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
IPowerSensorDiscoveryService discoveryService,
IOtherPowerCalculationService calculationService,
IPowerCostCalculationService costService,
IPowerMonitorEntityService entityService,
INordPoolPriceService priceService)
IPowerMonitorEntityService entityService)
{
_haContext = haContext;
_logger = logger;
@@ -47,7 +47,6 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
_calculationService = calculationService;
_costService = costService;
_entityService = entityService;
_priceService = priceService;
_ = InitializeAndScheduleAsync();
}
@@ -189,7 +188,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
var calculation = _calculationService.Calculate(totalPower.Value, deviceReadings);
var nordPoolPrice = _priceService.CurrentPrice;
var nordPoolPrice = ReadNordPoolPrice();
var priceSource = nordPoolPrice > 0 ? "live" : _costService.PriceSource;
var effectivePrice = nordPoolPrice > 0 ? nordPoolPrice : _costService.LastKnownPrice;
@@ -245,6 +244,24 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
}
}
private double ReadNordPoolPrice()
{
var state = _haContext.GetState(NordPoolCurrentPriceEntityId);
if (state?.State is null || state.State == "unavailable" || state.State == "unknown")
{
_logger.LogDebug("NordPool price entity unavailable, using last known price");
return 0;
}
if (double.TryParse(state.State, CultureInfo.InvariantCulture, out var price) && price > 0)
{
return price;
}
_logger.LogDebug("NordPool price entity returned non-positive value: {State}", state.State);
return 0;
}
private double? ReadMainMeterPower()
{
var state = _haContext.GetState(_options.MainMeterEntityId);