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:
@@ -28,7 +28,7 @@
|
|||||||
},
|
},
|
||||||
"PowerMonitor": {
|
"PowerMonitor": {
|
||||||
"MainMeterEntityId": "sensor.sonoff_100267d63f_power",
|
"MainMeterEntityId": "sensor.sonoff_100267d63f_power",
|
||||||
"PollIntervalSeconds": 5,
|
"PollIntervalSeconds": 1,
|
||||||
"SmoothingWindowSize": 10,
|
"SmoothingWindowSize": 10,
|
||||||
"AnomalyThresholdWatts": 500,
|
"AnomalyThresholdWatts": 500,
|
||||||
"ExcludedSensorPatterns": [
|
"ExcludedSensorPatterns": [
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\HomeAssistant.Entities\HomeAssistant.Entities.csproj" />
|
<ProjectReference Include="..\HomeAssistant.Entities\HomeAssistant.Entities.csproj" />
|
||||||
<ProjectReference Include="..\PowerMonitor.Models\PowerMonitor.Models.csproj" />
|
<ProjectReference Include="..\PowerMonitor.Models\PowerMonitor.Models.csproj" />
|
||||||
<ProjectReference Include="..\NordPool.Manager\NordPool.Manager.csproj" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Globalization;
|
||||||
using System.Reactive.Concurrency;
|
using System.Reactive.Concurrency;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
@@ -5,7 +6,6 @@ using NetDaemon.AppModel;
|
|||||||
using NetDaemon.Extensions.Scheduler;
|
using NetDaemon.Extensions.Scheduler;
|
||||||
using NetDaemon.HassModel;
|
using NetDaemon.HassModel;
|
||||||
using NetDaemon.HassModel.Entities;
|
using NetDaemon.HassModel.Entities;
|
||||||
using NordPoolManager.Interface;
|
|
||||||
using PowerMonitorManager.Interface;
|
using PowerMonitorManager.Interface;
|
||||||
using PowerMonitorManager.Models;
|
using PowerMonitorManager.Models;
|
||||||
using PowerMonitorManager.Models.Consts;
|
using PowerMonitorManager.Models.Consts;
|
||||||
@@ -23,7 +23,8 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
|||||||
private readonly IOtherPowerCalculationService _calculationService;
|
private readonly IOtherPowerCalculationService _calculationService;
|
||||||
private readonly IPowerCostCalculationService _costService;
|
private readonly IPowerCostCalculationService _costService;
|
||||||
private readonly IPowerMonitorEntityService _entityService;
|
private readonly IPowerMonitorEntityService _entityService;
|
||||||
private readonly INordPoolPriceService _priceService;
|
|
||||||
|
private const string NordPoolCurrentPriceEntityId = "sensor.nordpool_current_price";
|
||||||
|
|
||||||
private int _mainMeterConsecutiveFailures;
|
private int _mainMeterConsecutiveFailures;
|
||||||
private DateTime? _anomalyStartTime;
|
private DateTime? _anomalyStartTime;
|
||||||
@@ -36,8 +37,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
|||||||
IPowerSensorDiscoveryService discoveryService,
|
IPowerSensorDiscoveryService discoveryService,
|
||||||
IOtherPowerCalculationService calculationService,
|
IOtherPowerCalculationService calculationService,
|
||||||
IPowerCostCalculationService costService,
|
IPowerCostCalculationService costService,
|
||||||
IPowerMonitorEntityService entityService,
|
IPowerMonitorEntityService entityService)
|
||||||
INordPoolPriceService priceService)
|
|
||||||
{
|
{
|
||||||
_haContext = haContext;
|
_haContext = haContext;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
@@ -47,7 +47,6 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
|||||||
_calculationService = calculationService;
|
_calculationService = calculationService;
|
||||||
_costService = costService;
|
_costService = costService;
|
||||||
_entityService = entityService;
|
_entityService = entityService;
|
||||||
_priceService = priceService;
|
|
||||||
|
|
||||||
_ = InitializeAndScheduleAsync();
|
_ = InitializeAndScheduleAsync();
|
||||||
}
|
}
|
||||||
@@ -189,7 +188,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
|||||||
|
|
||||||
var calculation = _calculationService.Calculate(totalPower.Value, deviceReadings);
|
var calculation = _calculationService.Calculate(totalPower.Value, deviceReadings);
|
||||||
|
|
||||||
var nordPoolPrice = _priceService.CurrentPrice;
|
var nordPoolPrice = ReadNordPoolPrice();
|
||||||
var priceSource = nordPoolPrice > 0 ? "live" : _costService.PriceSource;
|
var priceSource = nordPoolPrice > 0 ? "live" : _costService.PriceSource;
|
||||||
var effectivePrice = nordPoolPrice > 0 ? nordPoolPrice : _costService.LastKnownPrice;
|
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()
|
private double? ReadMainMeterPower()
|
||||||
{
|
{
|
||||||
var state = _haContext.GetState(_options.MainMeterEntityId);
|
var state = _haContext.GetState(_options.MainMeterEntityId);
|
||||||
|
|||||||
Reference in New Issue
Block a user