Apply code formatting cleanup across monitor projects
- 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
This commit is contained in:
@@ -5,4 +5,4 @@ public interface ILeakNotificationService
|
||||
void SendLeakAlert(string sensorEntityId);
|
||||
|
||||
void DismissLeakAlert(string sensorEntityId);
|
||||
}
|
||||
}
|
||||
@@ -8,20 +8,20 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>LeakMonitor.Manager.Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
<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" />
|
||||
<ProjectReference Include="..\HomeAssistant.Entities\HomeAssistant.Entities.csproj"/>
|
||||
<ProjectReference Include="..\LeakMonitor.Models\LeakMonitor.Models.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="11.0.0" />
|
||||
<PackageReference Include="NetDaemon.Extensions.Scheduling" Version="26.43.1" />
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.43.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="11.0.0"/>
|
||||
<PackageReference Include="NetDaemon.Extensions.Scheduling" Version="26.43.1"/>
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.43.1"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager.Interface;
|
||||
using LeakMonitorManager.Models;
|
||||
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;
|
||||
|
||||
@@ -13,8 +13,8 @@ namespace LeakMonitorManager;
|
||||
public sealed class LeakMonitorInit
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILeakNotificationService _notificationService;
|
||||
private readonly ILogger<LeakMonitorInit> _logger;
|
||||
private readonly ILeakNotificationService _notificationService;
|
||||
private readonly LeakMonitorOptions _options;
|
||||
|
||||
public LeakMonitorInit(
|
||||
@@ -38,19 +38,15 @@ public sealed class LeakMonitorInit
|
||||
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);
|
||||
}
|
||||
foreach (var sensorEntityId in _options.SensorEntityIds) SubscribeToLeakSensor(sensorEntityId);
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("LeakMonitor initialized, monitoring {Count} sensor(s)", _options.SensorEntityIds.Length);
|
||||
}
|
||||
_logger.LogInformation("LeakMonitor initialized, monitoring {Count} sensor(s)",
|
||||
_options.SensorEntityIds.Length);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -78,9 +74,7 @@ public sealed class LeakMonitorInit
|
||||
});
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Subscribed to leak sensor {SensorId}", sensorEntityId);
|
||||
}
|
||||
}
|
||||
|
||||
internal void HandleStateChange(string sensorEntityId, StateChange change)
|
||||
@@ -90,17 +84,15 @@ public sealed class LeakMonitorInit
|
||||
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))
|
||||
{
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Water leak cleared by {SensorId}", sensorEntityId);
|
||||
}
|
||||
|
||||
_notificationService.DismissLeakAlert(sensorEntityId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
using LeakMonitorManager.Interface;
|
||||
using LeakMonitorManager.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NetDaemon.AppModel;
|
||||
using LeakMonitorManager.Interface;
|
||||
using LeakMonitorManager.Models;
|
||||
|
||||
namespace LeakMonitorManager;
|
||||
|
||||
@@ -14,11 +14,8 @@ public static class LeakMonitorServiceRegistration
|
||||
.GetSection(LeakMonitorOptions.SectionName)
|
||||
.Get<LeakMonitorOptions>() ?? new LeakMonitorOptions();
|
||||
|
||||
if (!options.Enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!options.Enabled) return;
|
||||
|
||||
services.AddNetDaemonApp<LeakMonitorInit>().AddScoped<ILeakNotificationService, LeakNotificationService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NetDaemon.HassModel;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager.Interface;
|
||||
using LeakMonitorManager.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NetDaemon.HassModel;
|
||||
|
||||
namespace LeakMonitorManager;
|
||||
|
||||
@@ -14,19 +14,17 @@ public sealed class LeakNotificationService(
|
||||
IOptions<LeakMonitorOptions> options,
|
||||
TimeProvider timeProvider) : ILeakNotificationService
|
||||
{
|
||||
private readonly LeakMonitorOptions _options = options.Value;
|
||||
private readonly ConcurrentDictionary<string, DateTimeOffset> _lastNotificationTimes = new();
|
||||
private readonly LeakMonitorOptions _options = options.Value;
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -52,9 +50,7 @@ public sealed class LeakNotificationService(
|
||||
data: new { notification_id = notificationId });
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation("Leak alert dismissed for sensor {SensorId}", sensorEntityId);
|
||||
}
|
||||
}
|
||||
|
||||
private void SendMobilePushNotification(string message)
|
||||
@@ -95,10 +91,7 @@ public sealed class LeakNotificationService(
|
||||
|
||||
private bool IsWithinCooldown(string sensorEntityId)
|
||||
{
|
||||
if (!_lastNotificationTimes.TryGetValue(sensorEntityId, out var lastTime))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!_lastNotificationTimes.TryGetValue(sensorEntityId, out var lastTime)) return false;
|
||||
|
||||
var cooldown = TimeSpan.FromMinutes(_options.NotificationCooldownMinutes);
|
||||
return timeProvider.GetUtcNow() - lastTime < cooldown;
|
||||
@@ -109,4 +102,4 @@ public sealed class LeakNotificationService(
|
||||
var sanitized = sensorEntityId.Replace('.', '_');
|
||||
return $"{LeakMonitorConsts.NotificationIdPrefix}{sanitized}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,4 @@ public sealed class LeakMonitorOptions
|
||||
public string[] SensorEntityIds { get; init; } = [];
|
||||
|
||||
public int NotificationCooldownMinutes { get; init; } = LeakMonitorConsts.DefaultNotificationCooldownMinutes;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user