Files
HomeAssistant/LightMonitor.Manager/LightControlService.cs
T
gitea_admin ada3c77778 Fix culture-sensitive parsing, log swallowing and closure capture
- DateTimeTypeConverter parses with the invariant culture instead of the
  ambient one (NordPool timestamps arrive as ISO-8601)
- Shower last-shower-time state is formatted with the invariant culture
- PresenceServiceTests no longer captures the loop variable in a Moq setup
- CheapestIntervalsFinderTests compare prices with a tolerance instead of
  floating-point equality
- MqttRecreateApp logs the unit_of_measurement read failure instead of
  silently swallowing every exception
- LightMonitorOptions moves to LightMonitor.Models so the namespace matches
  the project and file location
2026-11-29 21:31:45 +02:00

43 lines
1.4 KiB
C#

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using LightMonitor.Models;
using LightMonitorManager.Interface;
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))
{
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);
}
}