Update
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace LeakMonitorManager.Interface;
|
||||
|
||||
public interface ILeakMonitorInit
|
||||
{
|
||||
Task InitializeAsync();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace LeakMonitorManager.Interface;
|
||||
|
||||
public interface ILeakNotificationService
|
||||
{
|
||||
void SendLeakAlert(string sensorEntityId);
|
||||
|
||||
void DismissLeakAlert(string sensorEntityId);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>LeakMonitorManager</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>LeakMonitor.Manager.Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HomeAssistant.Entities\HomeAssistant.Entities.csproj" />
|
||||
<ProjectReference Include="..\LeakMonitor.Models\LeakMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.5" />
|
||||
<PackageReference Include="NetDaemon.Extensions.Scheduling" Version="26.11.0" />
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.11.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,96 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NetDaemon.AppModel;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager.Interface;
|
||||
using LeakMonitorManager.Models;
|
||||
|
||||
namespace LeakMonitorManager;
|
||||
|
||||
[NetDaemonApp]
|
||||
public sealed class LeakMonitorInit : ILeakMonitorInit
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILeakNotificationService _notificationService;
|
||||
private readonly ILogger<LeakMonitorInit> _logger;
|
||||
private readonly LeakMonitorOptions _options;
|
||||
|
||||
public LeakMonitorInit(
|
||||
IHaContext haContext,
|
||||
ILeakNotificationService notificationService,
|
||||
ILogger<LeakMonitorInit> logger,
|
||||
IOptions<LeakMonitorOptions> options)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_notificationService = notificationService;
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
|
||||
_ = InitializeAsync();
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_options.SensorEntityIds.Length == 0)
|
||||
{
|
||||
_logger.LogWarning("LeakMonitor enabled but no sensor entity IDs configured");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
foreach (var sensorEntityId in _options.SensorEntityIds)
|
||||
{
|
||||
SubscribeToLeakSensor(sensorEntityId);
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"LeakMonitor initialized, monitoring {Count} sensor(s)",
|
||||
_options.SensorEntityIds.Length);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during LeakMonitor initialization");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void SubscribeToLeakSensor(string sensorEntityId)
|
||||
{
|
||||
var entity = new Entity(_haContext, sensorEntityId);
|
||||
|
||||
entity.StateChanges()
|
||||
.Subscribe(change =>
|
||||
{
|
||||
try
|
||||
{
|
||||
HandleStateChange(sensorEntityId, change);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error handling state change for {SensorId}", sensorEntityId);
|
||||
}
|
||||
});
|
||||
|
||||
_logger.LogInformation("Subscribed to leak sensor {SensorId}", sensorEntityId);
|
||||
}
|
||||
|
||||
internal void HandleStateChange(string sensorEntityId, StateChange change)
|
||||
{
|
||||
var newState = change.New?.State;
|
||||
|
||||
if (string.Equals(newState, LeakMonitorConsts.WaterDetectedState, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogWarning("Water leak detected by {SensorId}!", sensorEntityId);
|
||||
_notificationService.SendLeakAlert(sensorEntityId);
|
||||
}
|
||||
else if (string.Equals(newState, LeakMonitorConsts.WaterClearedState, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogInformation("Water leak cleared by {SensorId}", sensorEntityId);
|
||||
_notificationService.DismissLeakAlert(sensorEntityId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NetDaemon.AppModel;
|
||||
using LeakMonitorManager.Interface;
|
||||
using LeakMonitorManager.Models;
|
||||
|
||||
namespace LeakMonitorManager;
|
||||
|
||||
public static class LeakMonitorServiceRegistration
|
||||
{
|
||||
public static IServiceCollection AddLeakMonitor(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var options = configuration
|
||||
.GetSection(LeakMonitorOptions.SectionName)
|
||||
.Get<LeakMonitorOptions>() ?? new LeakMonitorOptions();
|
||||
|
||||
if (!options.Enabled)
|
||||
{
|
||||
return services;
|
||||
}
|
||||
|
||||
services
|
||||
.AddNetDaemonApp<LeakMonitorInit>()
|
||||
.AddScoped<ILeakNotificationService, LeakNotificationService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
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 : ILeakNotificationService
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILogger<LeakNotificationService> _logger;
|
||||
private readonly LeakMonitorOptions _options;
|
||||
private readonly ConcurrentDictionary<string, DateTimeOffset> _lastNotificationTimes = new();
|
||||
|
||||
public LeakNotificationService(
|
||||
IHaContext haContext,
|
||||
ILogger<LeakNotificationService> logger,
|
||||
IOptions<LeakMonitorOptions> options)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
public void SendLeakAlert(string sensorEntityId)
|
||||
{
|
||||
if (IsWithinCooldown(sensorEntityId))
|
||||
{
|
||||
_logger.LogDebug(
|
||||
"Skipping leak notification for {SensorId} -- within cooldown period",
|
||||
sensorEntityId);
|
||||
return;
|
||||
}
|
||||
|
||||
_lastNotificationTimes[sensorEntityId] = DateTimeOffset.UtcNow;
|
||||
|
||||
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 });
|
||||
|
||||
_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 DateTimeOffset.UtcNow - lastTime < cooldown;
|
||||
}
|
||||
|
||||
internal static string BuildNotificationId(string sensorEntityId)
|
||||
{
|
||||
var sanitized = sensorEntityId.Replace('.', '_');
|
||||
return $"{LeakMonitorConsts.NotificationIdPrefix}{sanitized}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using LeakMonitor.Models;
|
||||
|
||||
namespace LeakMonitorManager.Models;
|
||||
|
||||
public sealed class LeakMonitorOptions
|
||||
{
|
||||
public const string SectionName = "LeakMonitor";
|
||||
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
public string[] SensorEntityIds { get; set; } = [];
|
||||
|
||||
public int NotificationCooldownMinutes { get; set; } = LeakMonitorConsts.DefaultNotificationCooldownMinutes;
|
||||
}
|
||||
Reference in New Issue
Block a user