feat: add PowerMonitor shared models, CostAccumulator, and entity types
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,117 @@
|
|||||||
|
namespace PowerMonitorManager.Models;
|
||||||
|
|
||||||
|
public sealed class CostAccumulator
|
||||||
|
{
|
||||||
|
private readonly object _lock = new();
|
||||||
|
|
||||||
|
private double _cost15Min;
|
||||||
|
private double _cost1Hour;
|
||||||
|
private double _costDaily;
|
||||||
|
private double _costMonthly;
|
||||||
|
|
||||||
|
private double _previous15Min;
|
||||||
|
private double _previous1Hour;
|
||||||
|
private double _previousDaily;
|
||||||
|
private double _previousMonthly;
|
||||||
|
|
||||||
|
private DateTime _last15MinReset;
|
||||||
|
private DateTime _last1HourReset;
|
||||||
|
private DateTime _lastDailyReset;
|
||||||
|
private DateTime _lastMonthlyReset;
|
||||||
|
|
||||||
|
private bool _partialWindow = true;
|
||||||
|
|
||||||
|
public CostAccumulator(DateTime now)
|
||||||
|
{
|
||||||
|
_last15MinReset = now;
|
||||||
|
_last1HourReset = now;
|
||||||
|
_lastDailyReset = now;
|
||||||
|
_lastMonthlyReset = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddCost(double costIncrement, DateTime now)
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
CheckAndResetWindows(now);
|
||||||
|
|
||||||
|
_cost15Min += costIncrement;
|
||||||
|
_cost1Hour += costIncrement;
|
||||||
|
_costDaily += costIncrement;
|
||||||
|
_costMonthly += costIncrement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public CostSnapshot GetSnapshot()
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
return new CostSnapshot
|
||||||
|
{
|
||||||
|
Cost15Min = _cost15Min,
|
||||||
|
Cost1Hour = _cost1Hour,
|
||||||
|
CostDaily = _costDaily,
|
||||||
|
CostMonthly = _costMonthly,
|
||||||
|
Previous15Min = _previous15Min,
|
||||||
|
Previous1Hour = _previous1Hour,
|
||||||
|
PreviousDaily = _previousDaily,
|
||||||
|
PreviousMonthly = _previousMonthly,
|
||||||
|
PartialWindow = _partialWindow
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckAndResetWindows(DateTime now)
|
||||||
|
{
|
||||||
|
if (IsNew15MinWindow(now, _last15MinReset))
|
||||||
|
{
|
||||||
|
_previous15Min = _cost15Min;
|
||||||
|
_cost15Min = 0;
|
||||||
|
_last15MinReset = now;
|
||||||
|
_partialWindow = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsNewHourWindow(now, _last1HourReset))
|
||||||
|
{
|
||||||
|
_previous1Hour = _cost1Hour;
|
||||||
|
_cost1Hour = 0;
|
||||||
|
_last1HourReset = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsNewDayWindow(now, _lastDailyReset))
|
||||||
|
{
|
||||||
|
_previousDaily = _costDaily;
|
||||||
|
_costDaily = 0;
|
||||||
|
_lastDailyReset = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsNewMonthWindow(now, _lastMonthlyReset))
|
||||||
|
{
|
||||||
|
_previousMonthly = _costMonthly;
|
||||||
|
_costMonthly = 0;
|
||||||
|
_lastMonthlyReset = now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static bool IsNew15MinWindow(DateTime now, DateTime lastReset)
|
||||||
|
{
|
||||||
|
var currentSlot = now.Minute / 15;
|
||||||
|
var lastSlot = lastReset.Minute / 15;
|
||||||
|
return now.Date != lastReset.Date || now.Hour != lastReset.Hour || currentSlot != lastSlot;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static bool IsNewHourWindow(DateTime now, DateTime lastReset)
|
||||||
|
{
|
||||||
|
return now.Date != lastReset.Date || now.Hour != lastReset.Hour;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static bool IsNewDayWindow(DateTime now, DateTime lastReset)
|
||||||
|
{
|
||||||
|
return now.Date != lastReset.Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static bool IsNewMonthWindow(DateTime now, DateTime lastReset)
|
||||||
|
{
|
||||||
|
return now.Year != lastReset.Year || now.Month != lastReset.Month;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace PowerMonitorManager.Models;
|
||||||
|
|
||||||
|
public sealed class CostSnapshot
|
||||||
|
{
|
||||||
|
public double Cost15Min { get; init; }
|
||||||
|
public double Cost1Hour { get; init; }
|
||||||
|
public double CostDaily { get; init; }
|
||||||
|
public double CostMonthly { get; init; }
|
||||||
|
public double Previous15Min { get; init; }
|
||||||
|
public double Previous1Hour { get; init; }
|
||||||
|
public double PreviousDaily { get; init; }
|
||||||
|
public double PreviousMonthly { get; init; }
|
||||||
|
public bool PartialWindow { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace PowerMonitorManager.Models;
|
||||||
|
|
||||||
|
public sealed class PowerCalculationResult
|
||||||
|
{
|
||||||
|
public double TotalPowerWatts { get; init; }
|
||||||
|
public double KnownDevicesSumWatts { get; init; }
|
||||||
|
public double RawOtherPowerWatts { get; init; }
|
||||||
|
public double SmoothedOtherPowerWatts { get; init; }
|
||||||
|
public string DataQuality { get; init; } = "ok";
|
||||||
|
public IReadOnlyDictionary<string, double> DevicePowerReadings { get; init; } = new Dictionary<string, double>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
namespace PowerMonitorManager.Models;
|
||||||
|
|
||||||
|
public sealed class PowerUpdateContext
|
||||||
|
{
|
||||||
|
public required PowerCalculationResult Calculation { get; init; }
|
||||||
|
public required double CurrentNordPoolPrice { get; init; }
|
||||||
|
public required string PriceUnit { get; init; }
|
||||||
|
public required string PriceSource { get; init; }
|
||||||
|
public required IReadOnlyDictionary<string, CostSnapshot> DeviceCosts { get; init; }
|
||||||
|
public required CostSnapshot TotalCost { get; init; }
|
||||||
|
public required CostSnapshot OtherCost { get; init; }
|
||||||
|
public required bool IsAnomaly { get; init; }
|
||||||
|
public required TimeSpan AnomalyDuration { get; init; }
|
||||||
|
public required double AnomalyThresholdWatts { get; init; }
|
||||||
|
public required int TrackedSensorCount { get; init; }
|
||||||
|
public required IReadOnlyList<string> DegradedSensors { get; init; }
|
||||||
|
public required string SystemStatus { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using HomeAssistant.Models;
|
||||||
|
using NetDaemon.Extensions.MqttEntityManager;
|
||||||
|
|
||||||
|
namespace PowerMonitor.Models;
|
||||||
|
|
||||||
|
public class PowerMonitorEntity : EntityCreateModel
|
||||||
|
{
|
||||||
|
public string Domain { get; set; }
|
||||||
|
public string Identifier { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public PowerMonitorEntity(string domain, string identifier, string name)
|
||||||
|
{
|
||||||
|
Domain = domain;
|
||||||
|
Identifier = identifier;
|
||||||
|
EntityId = $"{Domain}.{Identifier}";
|
||||||
|
Name = string.IsNullOrWhiteSpace(name) ? identifier : name;
|
||||||
|
|
||||||
|
Options = new EntityCreationOptions
|
||||||
|
{
|
||||||
|
UniqueId = EntityId,
|
||||||
|
Name = Name
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace PowerMonitor.Models;
|
||||||
|
|
||||||
|
public sealed class PowerSensorInfo
|
||||||
|
{
|
||||||
|
public required string EntityId { get; init; }
|
||||||
|
public required string FriendlyName { get; init; }
|
||||||
|
public required string DeviceName { get; init; }
|
||||||
|
public string UnitOfMeasurement { get; init; } = "W";
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user