Files
HomeAssistant/BatteryMonitor.Manager/BatteryNotificationService.cs
T
gitea_admin 77458383d1 Add braces to single-statement guards and refactor NordPool price fetching
- Expand inline guard, return and logger blocks across the monitor services, init classes and tests
- Have INordPoolPriceService take a per-date fetch delegate and expose ParseAndAdjust, moving the HA fetch call into NordPoolInit so the service can be registered singleton
- Record the NordPool and boiler service lifetimes in AGENTS.md
- Drop the unused persisted-state argument from the boiler test data helper
2026-12-01 11:59:31 +02:00

61 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();
}
}