49 lines
1.6 KiB
C#
49 lines
1.6 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
using LightMonitor.Models;
|
|
using LightMonitorManager.Interface;
|
|
using LightMonitorManager.Models;
|
|
|
|
namespace LightMonitorManager;
|
|
|
|
public sealed class LightControlService : ILightControlService
|
|
{
|
|
private readonly IHaContext _haContext;
|
|
private readonly ILogger<LightControlService> _logger;
|
|
private readonly LightMonitorOptions _options;
|
|
|
|
public LightControlService(
|
|
IHaContext haContext,
|
|
ILogger<LightControlService> logger,
|
|
IOptions<LightMonitorOptions> options)
|
|
{
|
|
_haContext = haContext;
|
|
_logger = logger;
|
|
_options = options.Value;
|
|
}
|
|
|
|
public void TurnOnLight()
|
|
{
|
|
var sunState = _haContext.GetState(_options.SunEntityId)?.State;
|
|
|
|
if (!string.Equals(sunState, LightMonitorConsts.SunBelowHorizon, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
_logger.LogDebug("Sun is not below horizon (state={SunState}), skipping light turn on", sunState);
|
|
return;
|
|
}
|
|
|
|
var lightState = _haContext.GetState(_options.LightEntityId)?.State;
|
|
|
|
if (string.Equals(lightState, LightMonitorConsts.LightOnState, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
_logger.LogDebug("Light {EntityId} is already on, skipping", _options.LightEntityId);
|
|
return;
|
|
}
|
|
|
|
_haContext.CallService("light", "turn_on", ServiceTarget.FromEntity(_options.LightEntityId));
|
|
_logger.LogInformation("Light {EntityId} turned ON (dark outside + person home)", _options.LightEntityId);
|
|
}
|
|
}
|