Enable PowerMonitor limit enforcement and prune unused boiler/replay code
PowerMonitor now ships enabled with power-limit enforcement settings: watt limit, recovery and cooldown windows, and switch-off plus pause/restore entity lists. Remove the unused BoilerRunTimeTracker, its DI registration and tests, the StatusCleanedUp constant, and the boiler replay runner; simplify BoilerConfigReader, control-service logging and entity parsing. Switch CostAccumulator to System.Threading.Lock, update the Shower exhaust fan service and tests, drop the legacy HistoryReplay/McpServer sources and reporting tooling, and expand AGENTS.md with the current project wiring and DI notes.
This commit is contained in:
@@ -17,6 +17,7 @@ public sealed class BoilerConfigReader(IHaContext haContext, ILogger<BoilerConfi
|
||||
var dailyHours = double.TryParse(dailyHoursState, NumberStyles.Any, CultureInfo.InvariantCulture, out var dh) ? dh : DefaultDailyHours;
|
||||
|
||||
var intervalCount = (int)(dailyHours * IntervalsPerHour);
|
||||
|
||||
if (intervalCount <= 0)
|
||||
{
|
||||
intervalCount = IntervalsPerHour;
|
||||
@@ -24,9 +25,10 @@ public sealed class BoilerConfigReader(IHaContext haContext, ILogger<BoilerConfi
|
||||
|
||||
var priceCapState = haContext.GetState(BoilerPriceCapEntityId)?.State;
|
||||
var priceCap = double.TryParse(priceCapState, NumberStyles.Any, CultureInfo.InvariantCulture, out var pc) && pc > 0 ? pc : double.MaxValue;
|
||||
|
||||
|
||||
var deadline = DateTime.Today.AddDays(1);
|
||||
var readyByState = haContext.GetState(BoilerReadyByEntityId)?.State;
|
||||
|
||||
if (!string.IsNullOrEmpty(readyByState) && TimeSpan.TryParse(readyByState, out var readyByTime))
|
||||
{
|
||||
deadline = DateTime.Today.Add(readyByTime);
|
||||
@@ -35,15 +37,17 @@ public sealed class BoilerConfigReader(IHaContext haContext, ILogger<BoilerConfi
|
||||
var skipPriceCapState = haContext.GetState(SkipPriceCapEntityId)?.State;
|
||||
var skipPriceCap = string.Equals(skipPriceCapState, SwitchOnState, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
logger.LogDebug("Boiler config: intervals={Intervals}, priceCap={Cap}, skipPriceCap={Skip}, deadline={Deadline}",
|
||||
intervalCount, priceCap, skipPriceCap, deadline);
|
||||
logger.LogDebug("Boiler config: intervals={Intervals}, priceCap={Cap}, skipPriceCap={Skip}, deadline={Deadline}", intervalCount, priceCap, skipPriceCap, deadline);
|
||||
|
||||
return new BoilerConfig
|
||||
var config = new BoilerConfig
|
||||
{
|
||||
IntervalCount = intervalCount,
|
||||
PriceCap = priceCap,
|
||||
Deadline = deadline,
|
||||
SkipPriceCap = skipPriceCap
|
||||
};
|
||||
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,9 +21,7 @@ public sealed class BoilerControlService(ILogger<BoilerControlService> logger) :
|
||||
|
||||
if (shouldBeOn && !isCurrentlyOn)
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Boiler should turn ON — cheap interval active, water not ready, budget available (current state: {State})",
|
||||
currentSwitchState);
|
||||
logger.LogInformation("Boiler should turn ON — cheap interval active, water not ready, budget available (current state: {State})", currentSwitchState);
|
||||
|
||||
return BoilerAction.TurnOn;
|
||||
}
|
||||
@@ -37,12 +35,8 @@ public sealed class BoilerControlService(ILogger<BoilerControlService> logger) :
|
||||
return BoilerAction.TurnOff;
|
||||
}
|
||||
|
||||
logger.LogDebug(
|
||||
"Boiler state unchanged: {State} (cheap={Cheap}, waterReady={WaterReady}, budgetExhausted={BudgetExhausted})",
|
||||
currentSwitchState,
|
||||
isNowCheap,
|
||||
waterReady,
|
||||
effectiveBudgetExhausted);
|
||||
logger.LogDebug("Boiler state unchanged: {State} (cheap={Cheap}, waterReady={WaterReady}, budgetExhausted={BudgetExhausted})",
|
||||
currentSwitchState, isNowCheap, waterReady, effectiveBudgetExhausted);
|
||||
|
||||
return BoilerAction.NoChange;
|
||||
}
|
||||
|
||||
@@ -93,9 +93,6 @@ public sealed class BoilerMonitorEntityService(
|
||||
var formattedRunTime = FormatTimeSpan(ctx.TodayRunTime);
|
||||
var formattedRemaining = FormatTimeSpan(ctx.RemainingTime);
|
||||
|
||||
// logger.LogInformation("Updating BoilerMonitor entities: runtime={RunTime}, remaining={Remaining}, budget={Budget}%, sessions={Sessions}",
|
||||
// formattedRunTime, formattedRemaining, ctx.BudgetPercentage, ctx.SessionCount);
|
||||
|
||||
var updateTasks = new[]
|
||||
{
|
||||
UpdateStatusAsync(StatusConfigured),
|
||||
@@ -116,7 +113,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
|
||||
try
|
||||
{
|
||||
var removeTasks = CleanableEntityIds.Select(entityId => entityManager.RemoveEntity(entityId));
|
||||
var removeTasks = CleanableEntityIds.Select(entityManager.RemoveEntity);
|
||||
await Task.WhenAll(removeTasks).ConfigureAwait(false);
|
||||
|
||||
logger.LogInformation("BoilerMonitor entities cleaned up successfully (cleanup button retained)");
|
||||
@@ -149,8 +146,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
var skipPriceCapState = haContext.GetState(SkipPriceCapEntityId)?.State;
|
||||
var budgetOverrideState = haContext.GetState(BudgetOverrideEntityId)?.State;
|
||||
|
||||
if (!TryParseEntitySeconds(runtimeState, out var runtimeSeconds)
|
||||
|| !TryParseEntityInt(sessionCountState, out var sessionCount))
|
||||
if (!TryParseEntitySeconds(runtimeState, out var runtimeSeconds) || !TryParseEntityInt(sessionCountState, out var sessionCount))
|
||||
{
|
||||
logger.LogInformation("No valid persisted BoilerMonitor state found in HA entities (runtime={Runtime}, sessions={Sessions})",
|
||||
runtimeState, sessionCountState);
|
||||
@@ -185,19 +181,23 @@ public sealed class BoilerMonitorEntityService(
|
||||
private static bool TryParseEntitySeconds(string? state, out double seconds)
|
||||
{
|
||||
seconds = 0;
|
||||
return !string.IsNullOrWhiteSpace(state)
|
||||
&& state is not ("unavailable" or "unknown")
|
||||
&& double.TryParse(state, CultureInfo.InvariantCulture, out seconds)
|
||||
&& seconds >= 0;
|
||||
var tryParseEntitySeconds = !string.IsNullOrWhiteSpace(state)
|
||||
&& state is not ("unavailable" or "unknown")
|
||||
&& double.TryParse(state, CultureInfo.InvariantCulture, out seconds)
|
||||
&& seconds >= 0;
|
||||
|
||||
return tryParseEntitySeconds;
|
||||
}
|
||||
|
||||
private static bool TryParseEntityInt(string? state, out int value)
|
||||
{
|
||||
value = 0;
|
||||
return !string.IsNullOrWhiteSpace(state)
|
||||
&& state is not ("unavailable" or "unknown")
|
||||
&& int.TryParse(state, CultureInfo.InvariantCulture, out value)
|
||||
&& value >= 0;
|
||||
var tryParseEntityInt = !string.IsNullOrWhiteSpace(state)
|
||||
&& state is not ("unavailable" or "unknown")
|
||||
&& int.TryParse(state, CultureInfo.InvariantCulture, out value)
|
||||
&& value >= 0;
|
||||
|
||||
return tryParseEntityInt;
|
||||
}
|
||||
|
||||
private ICollection<BoilerMonitorEntity> GetAllEntities()
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Globalization;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Text.Json;
|
||||
using BoilerMonitor.Models;
|
||||
using BoilerMonitor.Models.Enums;
|
||||
using BoilerMonitorManager.Interface;
|
||||
using BoilerMonitorManager.Models;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using BoilerMonitor.Models;
|
||||
using BoilerMonitorManager.Interface;
|
||||
using BoilerMonitorManager.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@@ -23,7 +22,6 @@ public static class BoilerMonitorServiceRegistration
|
||||
services
|
||||
.AddNetDaemonApp<BoilerMonitorInit>()
|
||||
.AddSingleton<IBoilerControlService, BoilerControlService>()
|
||||
.AddSingleton<IBoilerRunTimeTracker, BoilerRunTimeTracker>()
|
||||
.AddTransient<IBoilerMonitorEntityService, BoilerMonitorEntityService>()
|
||||
.AddSingleton<IWaterReadyService, WaterReadyService>();
|
||||
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
using BoilerMonitorManager.Interface;
|
||||
using BoilerMonitorManager.Models;
|
||||
|
||||
namespace BoilerMonitorManager;
|
||||
|
||||
public sealed class BoilerRunTimeTracker : IBoilerRunTimeTracker
|
||||
{
|
||||
private readonly Lock _lock = new();
|
||||
private TimeSpan _accumulatedRunTime = TimeSpan.Zero;
|
||||
private int _sessionCount;
|
||||
private bool _isInSession;
|
||||
|
||||
public void RecordPollResult(bool isHeating, TimeSpan pollInterval)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (isHeating)
|
||||
{
|
||||
_accumulatedRunTime += pollInterval;
|
||||
|
||||
if (!_isInSession)
|
||||
{
|
||||
_sessionCount++;
|
||||
_isInSession = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_isInSession = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public RunTimeSnapshot GetSnapshot()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return new RunTimeSnapshot
|
||||
{
|
||||
AccumulatedRunTime = _accumulatedRunTime,
|
||||
SessionCount = _sessionCount,
|
||||
IsInSession = _isInSession
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_accumulatedRunTime = TimeSpan.Zero;
|
||||
_sessionCount = 0;
|
||||
_isInSession = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void RestoreFromSnapshot(TimeSpan accumulatedRunTime, int sessionCount)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_accumulatedRunTime = accumulatedRunTime;
|
||||
_sessionCount = sessionCount;
|
||||
_isInSession = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using BoilerMonitor.Models;
|
||||
using BoilerMonitorManager.Models;
|
||||
|
||||
namespace BoilerMonitorManager.Interface;
|
||||
|
||||
@@ -59,9 +59,8 @@ public sealed class WaterReadyService(ILogger<WaterReadyService> logger, TimePro
|
||||
{
|
||||
if (!_hasObservedHeating)
|
||||
{
|
||||
logger.LogDebug(
|
||||
"Ignoring power reading ({PowerWatts}W) — boiler has not been observed heating yet",
|
||||
powerWatts.Value);
|
||||
logger.LogDebug("Ignoring power reading ({PowerWatts}W) — boiler has not been observed heating yet", powerWatts.Value);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -75,19 +74,15 @@ public sealed class WaterReadyService(ILogger<WaterReadyService> logger, TimePro
|
||||
|
||||
if (_consecutiveBelowThresholdCount < BoilerMonitorConsts.ConsecutiveReadingsRequired)
|
||||
{
|
||||
logger.LogDebug(
|
||||
"Below-threshold reading {Count}/{Required} ({PowerWatts}W), waiting for confirmation",
|
||||
_consecutiveBelowThresholdCount,
|
||||
BoilerMonitorConsts.ConsecutiveReadingsRequired,
|
||||
powerWatts.Value);
|
||||
logger.LogDebug("Below-threshold reading {Count}/{Required} ({PowerWatts}W), waiting for confirmation",
|
||||
_consecutiveBelowThresholdCount, BoilerMonitorConsts.ConsecutiveReadingsRequired, powerWatts.Value);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
_waterReadySince ??= timeProvider.GetUtcNow();
|
||||
|
||||
logger.LogInformation(
|
||||
"Water detected as ready after {Count} consecutive readings, cooldown active until shower or midnight reset",
|
||||
_consecutiveBelowThresholdCount);
|
||||
logger.LogInformation("Water detected as ready after {Count} consecutive readings, cooldown active until shower or midnight reset", _consecutiveBelowThresholdCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -137,8 +132,8 @@ public sealed class WaterReadyService(ILogger<WaterReadyService> logger, TimePro
|
||||
var todayMidnightUtc = new DateTimeOffset(timeProvider.GetUtcNow().Date, TimeSpan.Zero);
|
||||
if (waterReadySince < todayMidnightUtc)
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Persisted cooldown is from a previous day ({Since}), not restoring", waterReadySince);
|
||||
logger.LogInformation("Persisted cooldown is from a previous day ({Since}), not restoring", waterReadySince);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user