Files
HomeAssistant/LeakMonitor.Manager/LeakMonitorInit.cs
T
gitea_admin f919d6b25b Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests
- Expand single-statement guards and loops into braced blocks
- Realign wrapped method arguments, chained calls and continuations
- Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty`
- Flatten the boiler config expression and reindent the remaining touched files
2026-12-01 12:33:31 +02:00

111 lines
3.4 KiB
C#

#region
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;
#endregion
namespace LeakMonitorManager;
[NetDaemonApp]
public sealed class LeakMonitorInit
{
private readonly IHaContext _haContext;
private readonly ILogger<LeakMonitorInit> _logger;
private readonly ILeakNotificationService _notificationService;
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);
}
}
}