24729ee267
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
45 lines
1.5 KiB
C#
45 lines
1.5 KiB
C#
using LightMonitor.Models;
|
|
using LightMonitorManager.Interface;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
|
|
namespace LightMonitorManager;
|
|
|
|
public sealed class LightControlService(
|
|
IHaContext haContext,
|
|
ILogger<LightControlService> logger,
|
|
IOptions<LightMonitorOptions> options)
|
|
: ILightControlService
|
|
{
|
|
private readonly LightMonitorOptions _options = options.Value;
|
|
|
|
public void TurnOnLight()
|
|
{
|
|
var sunState = haContext.GetState(_options.SunEntityId)?.State;
|
|
|
|
if (!string.Equals(sunState, LightMonitorConsts.SunBelowHorizon, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
if (logger.IsEnabled(LogLevel.Debug))
|
|
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))
|
|
{
|
|
if (logger.IsEnabled(LogLevel.Debug))
|
|
logger.LogDebug("Light {EntityId} is already on, skipping", _options.LightEntityId);
|
|
|
|
return;
|
|
}
|
|
|
|
haContext.CallService("light", "turn_on", ServiceTarget.FromEntity(_options.LightEntityId));
|
|
|
|
if (logger.IsEnabled(LogLevel.Information))
|
|
logger.LogInformation("Light {EntityId} turned ON (dark outside + person home)", _options.LightEntityId);
|
|
}
|
|
} |