f919d6b25b
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
#region
|
|
|
|
using LightMonitor.Models;
|
|
using LightMonitorManager.Interface;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
|
|
#endregion
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |