24729ee267
- 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
59 lines
1.9 KiB
C#
59 lines
1.9 KiB
C#
using System.Text;
|
|
using BatteryMonitor.Models;
|
|
using BatteryMonitorManager.Interface;
|
|
using Microsoft.Extensions.Logging;
|
|
using NetDaemon.HassModel;
|
|
|
|
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();
|
|
}
|
|
} |