This commit is contained in:
2026-06-28 13:58:35 +03:00
parent 8f45028715
commit e6bf8258bb
114 changed files with 4365 additions and 874 deletions
@@ -0,0 +1,8 @@
namespace PowerMonitorManager.Interface;
public interface IPowerNotificationService
{
void NotifyHighPowerUsage(double currentPowerWatts, double thresholdWatts);
void DismissHighPowerNotification();
}
@@ -18,6 +18,14 @@ public static class PowerMonitorConsts
public const int MainMeterMaxConsecutiveFailures = 5;
public const double DefaultHighPowerThresholdWatts = 6000.0;
public const string HighPowerNotificationId = "power_monitor_high_power";
public const string HighPowerNotificationTitle = "High Power Usage Alert";
public const string PersistentNotificationDomain = "persistent_notification";
public const string PersistentNotificationCreateService = "create";
public const string PersistentNotificationDismissService = "dismiss";
public const int MinutesPerQuarterHour = 15;
public const int MinutesPerHour = 60;
}
@@ -12,4 +12,5 @@ public sealed class PowerMonitorOptions
public int SmoothingWindowSize { get; set; } = PowerMonitorConsts.DefaultSmoothingWindowSize;
public double AnomalyThresholdWatts { get; set; } = PowerMonitorConsts.DefaultAnomalyThresholdWatts;
public string[] ExcludedSensorPatterns { get; set; } = [];
public double HighPowerThresholdWatts { get; set; } = PowerMonitorConsts.DefaultHighPowerThresholdWatts;
}
+23 -1
View File
@@ -24,9 +24,11 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
private readonly IOtherPowerCalculationService _calculationService;
private readonly IPowerCostCalculationService _costService;
private readonly IPowerMonitorEntityService _entityService;
private readonly IPowerNotificationService _notificationService;
private int _mainMeterConsecutiveFailures;
private DateTime? _anomalyStartTime;
private bool _highPowerNotificationActive;
public PowerMonitorInit(
IHaContext haContext,
@@ -36,7 +38,8 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
IPowerSensorDiscoveryService discoveryService,
IOtherPowerCalculationService calculationService,
IPowerCostCalculationService costService,
IPowerMonitorEntityService entityService)
IPowerMonitorEntityService entityService,
IPowerNotificationService notificationService)
{
_haContext = haContext;
_logger = logger;
@@ -46,6 +49,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
_calculationService = calculationService;
_costService = costService;
_entityService = entityService;
_notificationService = notificationService;
_ = InitializeAndScheduleAsync();
}
@@ -230,6 +234,8 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
await _entityService.UpdateEntitiesAsync(context);
EvaluateHighPowerNotification(calculation.TotalPowerWatts);
_logger.LogDebug(
"Poll complete: total={Total}W, other={Other}W (smoothed), devices={Devices}, price={Price}",
calculation.TotalPowerWatts,
@@ -261,6 +267,22 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
return 0;
}
private void EvaluateHighPowerNotification(double totalPowerWatts)
{
var isAboveThreshold = totalPowerWatts > _options.HighPowerThresholdWatts;
if (isAboveThreshold && !_highPowerNotificationActive)
{
_notificationService.NotifyHighPowerUsage(totalPowerWatts, _options.HighPowerThresholdWatts);
_highPowerNotificationActive = true;
}
else if (!isAboveThreshold && _highPowerNotificationActive)
{
_notificationService.DismissHighPowerNotification();
_highPowerNotificationActive = false;
}
}
private double? ReadMainMeterPower()
{
var state = _haContext.GetState(_options.MainMeterEntityId);
@@ -24,7 +24,8 @@ public static class PowerMonitorServiceRegistration
.AddScoped<IPowerSensorDiscoveryService, PowerSensorDiscoveryService>()
.AddSingleton<IOtherPowerCalculationService, OtherPowerCalculationService>()
.AddSingleton<IPowerCostCalculationService, PowerCostCalculationService>()
.AddTransient<IPowerMonitorEntityService, PowerMonitorEntityService>();
.AddTransient<IPowerMonitorEntityService, PowerMonitorEntityService>()
.AddTransient<IPowerNotificationService, PowerNotificationService>();
return services;
}
@@ -0,0 +1,64 @@
using System.Globalization;
using Microsoft.Extensions.Logging;
using NetDaemon.HassModel;
using PowerMonitorManager.Interface;
using PowerMonitorManager.Models.Consts;
namespace PowerMonitorManager;
public sealed class PowerNotificationService : IPowerNotificationService
{
private readonly IHaContext _haContext;
private readonly ILogger<PowerNotificationService> _logger;
public PowerNotificationService(
IHaContext haContext,
ILogger<PowerNotificationService> logger)
{
_haContext = haContext;
_logger = logger;
}
public void NotifyHighPowerUsage(double currentPowerWatts, double thresholdWatts)
{
var currentKw = currentPowerWatts / PowerMonitorConsts.WattsPerKilowatt;
var thresholdKw = thresholdWatts / PowerMonitorConsts.WattsPerKilowatt;
var message = BuildHighPowerMessage(currentKw, thresholdKw);
_haContext.CallService(
PowerMonitorConsts.PersistentNotificationDomain,
PowerMonitorConsts.PersistentNotificationCreateService,
data: new
{
title = PowerMonitorConsts.HighPowerNotificationTitle,
message,
notification_id = PowerMonitorConsts.HighPowerNotificationId
});
_logger.LogWarning(
"High power usage notification sent: {CurrentKw:F2} kW (threshold: {ThresholdKw:F1} kW)",
currentKw, thresholdKw);
}
public void DismissHighPowerNotification()
{
_haContext.CallService(
PowerMonitorConsts.PersistentNotificationDomain,
PowerMonitorConsts.PersistentNotificationDismissService,
data: new
{
notification_id = PowerMonitorConsts.HighPowerNotificationId
});
_logger.LogDebug("Dismissed high power usage notification");
}
internal static string BuildHighPowerMessage(double currentKw, double thresholdKw)
{
return string.Format(
CultureInfo.InvariantCulture,
"Total home power usage is **{0:F2} kW**, which exceeds the threshold of **{1:F1} kW**.\n\nConsider turning off high-power appliances to reduce consumption.",
currentKw,
thresholdKw);
}
}