65 lines
2.2 KiB
C#
65 lines
2.2 KiB
C#
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);
|
|
}
|
|
}
|