using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NetDaemon.HassModel; using LightMonitor.Models; using LightMonitorManager.Interface; using LightMonitorManager.Models; namespace LightMonitorManager; public sealed class PresenceService : IPresenceService { private readonly IHaContext _haContext; private readonly ILogger _logger; private readonly LightMonitorOptions _options; public PresenceService( IHaContext haContext, ILogger logger, IOptions options) { _haContext = haContext; _logger = logger; _options = options.Value; } public bool IsAnyoneHome() { foreach (var personEntityId in _options.PersonEntityIds) { var state = _haContext.GetState(personEntityId); if (state?.State is null or "unavailable" or "unknown") { _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); return true; } } _logger.LogDebug("No person is currently home"); return false; } }