Files
HomeAssistant/PowerMonitor.Manager/PowerNotificationService.cs
T
gitea_admin 24729ee267 Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files
- Collapse single-statement guard blocks and tidy using and member order
- Normalize XML doc indentation and file endings
- Reorder BatteryMonitor test-data helpers and move the exclusion tests into
  their own region
- Carry the same formatting through the boiler config, control, entity and
  test files
2026-11-30 11:52:06 +02:00

55 lines
2.0 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(
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);
}
}