Files
HomeAssistant/LeakMonitor.Manager/LeakNotificationService.cs
T
gitea_admin 41ac93733e Clear Qodana findings in monitor runtime code
- Guard logger calls with IsEnabled so value-type arguments are not boxed
  when the level is disabled
- Make helper members static, narrow write-only properties to init or
  private get, and delete members no call site reaches
- Flatten nested conditionals and convert if/else chains into switch and
  ternary form
- Drop the unused *MonitorInit interfaces; NetDaemon instantiates the
  concrete app classes
- Drop the unused IConfiguration parameter from AddNordPool and
  AddPowerMonitor, and the unused IEntityManager.CreateEntitiesAsync
- Remove the ignored parameters of the boiler run-time tracker stub
- Pin the NordPool DTO price mapping explicitly so the getter use is
  visible to static analysis
- Fail Qodana on any finding and record the registration changes in
  AGENTS.md
2026-11-29 21:42:47 +02:00

113 lines
3.5 KiB
C#

using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NetDaemon.HassModel;
using LeakMonitor.Models;
using LeakMonitorManager.Interface;
using LeakMonitorManager.Models;
namespace LeakMonitorManager;
public sealed class LeakNotificationService(
IHaContext haContext,
ILogger<LeakNotificationService> logger,
IOptions<LeakMonitorOptions> options,
TimeProvider timeProvider) : ILeakNotificationService
{
private readonly LeakMonitorOptions _options = options.Value;
private readonly ConcurrentDictionary<string, DateTimeOffset> _lastNotificationTimes = new();
public void SendLeakAlert(string sensorEntityId)
{
if (IsWithinCooldown(sensorEntityId))
{
if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug(
"Skipping leak notification for {SensorId} -- within cooldown period",
sensorEntityId);
}
return;
}
_lastNotificationTimes[sensorEntityId] = timeProvider.GetUtcNow();
var notificationId = BuildNotificationId(sensorEntityId);
var message = $"Water detected by {sensorEntityId}! Check immediately.";
SendMobilePushNotification(message);
CreatePersistentNotification(notificationId, message);
logger.LogWarning("Leak alert sent for sensor {SensorId}", sensorEntityId);
}
public void DismissLeakAlert(string sensorEntityId)
{
var notificationId = BuildNotificationId(sensorEntityId);
haContext.CallService(
LeakMonitorConsts.PersistentNotificationDomain,
LeakMonitorConsts.PersistentNotificationDismissService,
data: new { notification_id = notificationId });
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation("Leak alert dismissed for sensor {SensorId}", sensorEntityId);
}
}
private void SendMobilePushNotification(string message)
{
haContext.CallService(
LeakMonitorConsts.NotifyDomain,
LeakMonitorConsts.MobileAppService,
data: new
{
message,
title = LeakMonitorConsts.NotificationTitle,
data = new
{
push = new
{
sound = new
{
critical = 1,
volume = 1.0
}
}
}
});
}
private void CreatePersistentNotification(string notificationId, string message)
{
haContext.CallService(
LeakMonitorConsts.PersistentNotificationDomain,
LeakMonitorConsts.PersistentNotificationCreateService,
data: new
{
title = LeakMonitorConsts.NotificationTitle,
message,
notification_id = notificationId
});
}
private bool IsWithinCooldown(string sensorEntityId)
{
if (!_lastNotificationTimes.TryGetValue(sensorEntityId, out var lastTime))
{
return false;
}
var cooldown = TimeSpan.FromMinutes(_options.NotificationCooldownMinutes);
return timeProvider.GetUtcNow() - lastTime < cooldown;
}
internal static string BuildNotificationId(string sensorEntityId)
{
var sanitized = sensorEntityId.Replace('.', '_');
return $"{LeakMonitorConsts.NotificationIdPrefix}{sanitized}";
}
}