41ac93733e
- 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
107 lines
3.2 KiB
C#
107 lines
3.2 KiB
C#
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
|
|
{
|
|
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();
|
|
}
|
|
|
|
private 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);
|
|
}
|
|
|
|
if (_logger.IsEnabled(LogLevel.Information))
|
|
{
|
|
_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);
|
|
}
|
|
});
|
|
|
|
if (_logger.IsEnabled(LogLevel.Information))
|
|
{
|
|
_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))
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Information))
|
|
{
|
|
_logger.LogInformation("Water leak cleared by {SensorId}", sensorEntityId);
|
|
}
|
|
|
|
_notificationService.DismissLeakAlert(sensorEntityId);
|
|
}
|
|
}
|
|
}
|