Files
HomeAssistant/LightMonitor.Manager/PresenceService.cs
T

49 lines
1.4 KiB
C#

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<PresenceService> _logger;
private readonly LightMonitorOptions _options;
public PresenceService(
IHaContext haContext,
ILogger<PresenceService> logger,
IOptions<LightMonitorOptions> 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;
}
}