Files
HomeAssistant/PowerMonitor.Manager/PowerNotificationService.cs
T
gitea_admin f919d6b25b Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests
- Expand single-statement guards and loops into braced blocks
- Realign wrapped method arguments, chained calls and continuations
- Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty`
- Flatten the boiler config expression and reindent the remaining touched files
2026-12-01 12:33:31 +02:00

59 lines
2.3 KiB
C#

#region
using System.Globalization;
using Microsoft.Extensions.Logging;
using NetDaemon.HassModel;
using PowerMonitorManager.Interface;
using PowerMonitorManager.Models.Consts;
#endregion
namespace PowerMonitorManager;
public sealed class PowerNotificationService(
IHaContext haContext,
ILogger<PowerNotificationService> logger)
: IPowerNotificationService
{
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");
}
private 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);
}
}