Add braces to single-statement guards and refactor NordPool price fetching
- Expand inline guard, return and logger blocks across the monitor services, init classes and tests - Have INordPoolPriceService take a per-date fetch delegate and expose ParseAndAdjust, moving the HA fetch call into NordPoolInit so the service can be registered singleton - Record the NordPool and boiler service lifetimes in AGENTS.md - Drop the unused persisted-state argument from the boiler test data helper
This commit is contained in:
@@ -22,7 +22,9 @@ public sealed class LightControlService(
|
||||
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;
|
||||
}
|
||||
@@ -32,7 +34,9 @@ public sealed class LightControlService(
|
||||
if (string.Equals(lightState, LightMonitorConsts.LightOnState, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug("Light {EntityId} is already on, skipping", _options.LightEntityId);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -40,6 +44,8 @@ public sealed class LightControlService(
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,8 +41,10 @@ public sealed class LightMonitorInit
|
||||
SubscribeToPersonStateChanges();
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("LightMonitor initialized with {PersonCount} tracked persons",
|
||||
_options.PersonEntityIds.Length);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -61,12 +63,17 @@ public sealed class LightMonitorInit
|
||||
try
|
||||
{
|
||||
if (!string.Equals(e.New?.State, LightMonitorConsts.SunBelowHorizon,
|
||||
StringComparison.OrdinalIgnoreCase)) return;
|
||||
StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsRecoveryTransition(e.Old?.State))
|
||||
{
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Sun state recovered from {OldState}, ignoring", e.Old?.State);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -93,20 +100,27 @@ public sealed class LightMonitorInit
|
||||
try
|
||||
{
|
||||
if (!string.Equals(e.New?.State, LightMonitorConsts.PersonHomeState,
|
||||
StringComparison.OrdinalIgnoreCase)) return;
|
||||
StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsRecoveryTransition(e.Old?.State))
|
||||
{
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Person {EntityId} state recovered from {OldState}, ignoring",
|
||||
personEntityId, e.Old?.State);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Person {EntityId} arrived home, checking if it is dark",
|
||||
personEntityId);
|
||||
}
|
||||
|
||||
_lightControlService.TurnOnLight();
|
||||
}
|
||||
|
||||
@@ -20,10 +20,15 @@ public sealed class PresenceService(
|
||||
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)) continue;
|
||||
StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug("Person {EntityId} is home", personEntityId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user