Files
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

70 lines
2.3 KiB
C#

#region
using System.Text;
using BatteryMonitor.Models;
using BatteryMonitorManager.Interface;
using Microsoft.Extensions.Logging;
using NetDaemon.HassModel;
#endregion
namespace BatteryMonitorManager;
public sealed class BatteryNotificationService(
IHaContext haContext,
ILogger<BatteryNotificationService> logger) : IBatteryNotificationService
{
public void NotifyLowBattery(IReadOnlyList<BatterySensorInfo> lowBatteryDevices)
{
if (lowBatteryDevices.Count == 0)
{
return;
}
var message = BuildNotificationMessage(lowBatteryDevices);
haContext.CallService(
BatteryMonitorConsts.PersistentNotificationDomain,
BatteryMonitorConsts.PersistentNotificationCreateService,
data: new
{
title = BatteryMonitorConsts.NotificationTitle,
message,
notification_id = BatteryMonitorConsts.NotificationId
});
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation(
"Sent low battery notification for {Count} device(s)",
lowBatteryDevices.Count);
}
}
public void DismissNotification()
{
haContext.CallService(
BatteryMonitorConsts.PersistentNotificationDomain,
BatteryMonitorConsts.PersistentNotificationDismissService,
data: new
{
notification_id = BatteryMonitorConsts.NotificationId
});
logger.LogDebug("Dismissed low battery notification");
}
internal static string BuildNotificationMessage(IReadOnlyList<BatterySensorInfo> devices)
{
var sb = new StringBuilder();
sb.AppendLine("The following devices have low battery:");
sb.AppendLine();
foreach (var device in devices.OrderBy(d => d.BatteryLevel))
{
sb.AppendLine($"- **{device.FriendlyName}**: {device.BatteryLevel:F0}%");
}
return sb.ToString().TrimEnd();
}
}