This commit is contained in:
2026-06-28 13:58:35 +03:00
parent 8f45028715
commit e6bf8258bb
114 changed files with 4365 additions and 874 deletions
@@ -31,6 +31,7 @@ public sealed class LightControlService : ILightControlService
if (!string.Equals(sunState, LightMonitorConsts.SunBelowHorizon, StringComparison.OrdinalIgnoreCase))
{
_logger.LogDebug("Sun is not below horizon (state={SunState}), skipping light turn on", sunState);
return;
}
@@ -39,6 +40,7 @@ public sealed class LightControlService : ILightControlService
if (string.Equals(lightState, LightMonitorConsts.LightOnState, StringComparison.OrdinalIgnoreCase))
{
_logger.LogDebug("Light {EntityId} is already on, skipping", _options.LightEntityId);
return;
}
+15
View File
@@ -63,6 +63,13 @@ public sealed class LightMonitorInit : ILightMonitorInit
return;
}
if (IsRecoveryTransition(e.Old?.State))
{
_logger.LogInformation("Sun state recovered from {OldState}, ignoring", e.Old?.State);
return;
}
_logger.LogInformation("Sun went below horizon, checking if anyone is home");
if (_presenceService.IsAnyoneHome())
@@ -92,6 +99,12 @@ public sealed class LightMonitorInit : ILightMonitorInit
return;
}
if (IsRecoveryTransition(e.Old?.State))
{
_logger.LogInformation("Person {EntityId} state recovered from {OldState}, ignoring", personEntityId, e.Old?.State);
return;
}
_logger.LogInformation("Person {EntityId} arrived home, checking if it is dark", personEntityId);
_lightControlService.TurnOnLight();
}
@@ -102,4 +115,6 @@ public sealed class LightMonitorInit : ILightMonitorInit
});
}
}
private static bool IsRecoveryTransition(string? oldState) => oldState is null or LightMonitorConsts.UnavailableState or LightMonitorConsts.UnknownState;
}
@@ -19,10 +19,9 @@ public static class LightMonitorServiceRegistration
return services;
}
services
.AddNetDaemonApp<LightMonitorInit>()
.AddScoped<IPresenceService, PresenceService>()
.AddScoped<ILightControlService, LightControlService>();
services.AddNetDaemonApp<LightMonitorInit>()
.AddScoped<IPresenceService, PresenceService>()
.AddScoped<ILightControlService, LightControlService>();
return services;
}
@@ -1,13 +0,0 @@
using static LightMonitor.Models.LightMonitorConsts;
namespace LightMonitorManager.Models;
public sealed class LightMonitorOptions
{
public const string SectionName = "LightMonitor";
public bool Enabled { get; set; }
public string LightEntityId { get; set; } = DefaultLightEntityId;
public string SunEntityId { get; set; } = DefaultSunEntityId;
public string[] PersonEntityIds { get; set; } = DefaultPersonEntityIds;
}
+10 -20
View File
@@ -7,42 +7,32 @@ using LightMonitorManager.Models;
namespace LightMonitorManager;
public sealed class PresenceService : IPresenceService
public sealed class PresenceService(
IHaContext haContext,
ILogger<PresenceService> logger,
IOptions<LightMonitorOptions> options) : IPresenceService
{
private readonly IHaContext _haContext;
private readonly ILogger<PresenceService> _logger;
private readonly LightMonitorOptions _options;
public PresenceService(
IHaContext haContext,
ILogger<PresenceService> logger,
IOptions<LightMonitorOptions> options)
{
_haContext = haContext;
_logger = logger;
_options = options.Value;
}
private readonly LightMonitorOptions _options = options.Value;
public bool IsAnyoneHome()
{
foreach (var personEntityId in _options.PersonEntityIds)
{
var state = _haContext.GetState(personEntityId);
var state = haContext.GetState(personEntityId);
if (state?.State is null or "unavailable" or "unknown")
if (state?.State is null or LightMonitorConsts.UnavailableState or LightMonitorConsts.UnknownState)
{
_logger.LogDebug("Person entity {EntityId} state unavailable", personEntityId);
continue;
}
if (string.Equals(state.State, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase))
{
_logger.LogDebug("Person {EntityId} is home", personEntityId);
logger.LogDebug("Person {EntityId} is home", personEntityId);
return true;
}
}
_logger.LogDebug("No person is currently home");
return false;
}
}