Files
HomeAssistant/BoilerMonitor.Manager/WaterReadyService.cs
T
gitea_admin f0bbd4f10b 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.
2026-11-29 13:58:08 +02:00

147 lines
4.1 KiB
C#

using BoilerMonitor.Models;
using BoilerMonitorManager.Interface;
using Microsoft.Extensions.Logging;
namespace BoilerMonitorManager;
public sealed class WaterReadyService(ILogger<WaterReadyService> logger, TimeProvider timeProvider) : IWaterReadyService
{
private const string SwitchOnState = "on";
private readonly Lock _lock = new();
private DateTimeOffset? _waterReadySince;
private bool _hasObservedHeating;
private int _consecutiveBelowThresholdCount;
public bool IsInCooldown
{
get
{
lock (_lock)
{
return IsWithinCooldownPeriod();
}
}
}
public DateTimeOffset? WaterReadySince
{
get
{
lock (_lock)
{
return _waterReadySince;
}
}
}
public bool IsWaterReady(string? switchState, double? powerWatts, double heatingThresholdWatts)
{
lock (_lock)
{
if (IsWithinCooldownPeriod())
{
logger.LogDebug("Water ready cooldown active (since {Since}), suppressing turn-on", _waterReadySince);
return true;
}
}
if (switchState is null || powerWatts is null)
{
return false;
}
if (!string.Equals(switchState, SwitchOnState, StringComparison.OrdinalIgnoreCase))
{
return false;
}
lock (_lock)
{
if (!_hasObservedHeating)
{
logger.LogDebug("Ignoring power reading ({PowerWatts}W) — boiler has not been observed heating yet", powerWatts.Value);
return false;
}
if (powerWatts.Value >= heatingThresholdWatts)
{
_consecutiveBelowThresholdCount = 0;
return false;
}
_consecutiveBelowThresholdCount++;
if (_consecutiveBelowThresholdCount < BoilerMonitorConsts.ConsecutiveReadingsRequired)
{
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);
return true;
}
}
public void NotifyTurnedOn()
{
lock (_lock)
{
_hasObservedHeating = false;
_consecutiveBelowThresholdCount = 0;
}
logger.LogDebug("Boiler turn-on recorded, awaiting first heating observation before water ready detection is enabled");
}
public void RecordHeatingObserved()
{
lock (_lock)
{
if (_hasObservedHeating)
{
return;
}
_hasObservedHeating = true;
}
logger.LogDebug("Heating observed, water ready detection is now enabled");
}
public void ResetCooldown()
{
lock (_lock)
{
_waterReadySince = null;
_consecutiveBelowThresholdCount = 0;
}
logger.LogInformation("Water ready cooldown has been reset");
}
public void RestoreCooldownState(DateTimeOffset waterReadySince)
{
lock (_lock)
{
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);
return;
}
_waterReadySince = waterReadySince;
logger.LogInformation("Restored water ready cooldown from {Since}", waterReadySince);
}
}
private bool IsWithinCooldownPeriod() => _waterReadySince is not null;
}