39 lines
1.1 KiB
C#
39 lines
1.1 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(
|
|
IHaContext haContext,
|
|
ILogger<PresenceService> logger,
|
|
IOptions<LightMonitorOptions> options) : IPresenceService
|
|
{
|
|
private readonly LightMonitorOptions _options = options.Value;
|
|
|
|
public bool IsAnyoneHome()
|
|
{
|
|
foreach (var personEntityId in _options.PersonEntityIds)
|
|
{
|
|
var state = haContext.GetState(personEntityId);
|
|
|
|
if (state?.State is null or LightMonitorConsts.UnavailableState or LightMonitorConsts.UnknownState)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (string.Equals(state.State, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
logger.LogDebug("Person {EntityId} is home", personEntityId);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
return false;
|
|
}
|
|
}
|