feat: add BatteryMonitor implementation with discovery, notification, and scheduling services

This commit is contained in:
Marius
2026-04-01 10:55:33 +03:00
parent 11c2574235
commit 6e4d684359
114 changed files with 22820 additions and 527 deletions
@@ -0,0 +1,72 @@
using System.Text;
using Microsoft.Extensions.Logging;
using NetDaemon.HassModel;
using BatteryMonitor.Models;
using BatteryMonitorManager.Interface;
namespace BatteryMonitorManager;
public sealed class BatteryNotificationService : IBatteryNotificationService
{
private readonly IHaContext _haContext;
private readonly ILogger<BatteryNotificationService> _logger;
public BatteryNotificationService(
IHaContext haContext,
ILogger<BatteryNotificationService> logger)
{
_haContext = haContext;
_logger = logger;
}
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
});
_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();
}
}