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,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);
}
}