Apply code formatting cleanup across the monitor projects
- 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
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#region
|
||||
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using BoilerMonitor.Models;
|
||||
@@ -9,6 +11,8 @@ using NetDaemon.Extensions.MqttEntityManager;
|
||||
using NetDaemon.HassModel;
|
||||
using static BoilerMonitor.Models.BoilerMonitorConsts;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BoilerMonitorManager;
|
||||
|
||||
public sealed class BoilerMonitorEntityService(
|
||||
@@ -41,21 +45,21 @@ public sealed class BoilerMonitorEntityService(
|
||||
var entities = GetAllEntities();
|
||||
|
||||
var createTasks = entities.Select(async entity =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation("Creating/re-registering MQTT entity: {EntityId}", entity.EntityId);
|
||||
}
|
||||
{
|
||||
try
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation("Creating/re-registering MQTT entity: {EntityId}", entity.EntityId);
|
||||
}
|
||||
|
||||
await entityManager.CreateEntityAsync(entity).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to create MQTT entity: {EntityId}", entity.EntityId);
|
||||
}
|
||||
});
|
||||
await entityManager.CreateEntityAsync(entity).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to create MQTT entity: {EntityId}", entity.EntityId);
|
||||
}
|
||||
});
|
||||
|
||||
await Task.WhenAll(createTasks).ConfigureAwait(false);
|
||||
}
|
||||
@@ -67,17 +71,18 @@ public sealed class BoilerMonitorEntityService(
|
||||
logger.LogInformation("Resetting all BoilerMonitor runtime entity states to defaults");
|
||||
|
||||
await Task.WhenAll(
|
||||
ResetStatusAsync(),
|
||||
ResetWaterReadyAsync(),
|
||||
ResetSkipPriceCapAsync(),
|
||||
ResetTodayRuntimeAsync(),
|
||||
ResetRemainingTimeAsync(),
|
||||
ResetBudgetPercentageAsync(),
|
||||
ResetBudgetExhaustedAsync(),
|
||||
ResetSessionCountAsync(),
|
||||
ResetBudgetOverrideAsync(),
|
||||
ResetAutoManagementAsync()
|
||||
).ConfigureAwait(false);
|
||||
ResetStatusAsync(),
|
||||
ResetWaterReadyAsync(),
|
||||
ResetSkipPriceCapAsync(),
|
||||
ResetTodayRuntimeAsync(),
|
||||
ResetRemainingTimeAsync(),
|
||||
ResetBudgetPercentageAsync(),
|
||||
ResetBudgetExhaustedAsync(),
|
||||
ResetSessionCountAsync(),
|
||||
ResetBudgetOverrideAsync(),
|
||||
ResetAutoManagementAsync()
|
||||
)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
logger.LogInformation("All BoilerMonitor runtime entity states reset to defaults");
|
||||
}
|
||||
@@ -118,6 +123,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error cleaning up BoilerMonitor entities");
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -129,9 +135,10 @@ public sealed class BoilerMonitorEntityService(
|
||||
logger.LogInformation("Resetting transient BoilerMonitor entity states (status, water ready)");
|
||||
|
||||
await Task.WhenAll(
|
||||
ResetStatusAsync(),
|
||||
ResetWaterReadyAsync()
|
||||
).ConfigureAwait(false);
|
||||
ResetStatusAsync(),
|
||||
ResetWaterReadyAsync()
|
||||
)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public PersistedBoilerState? ReadPersistedState()
|
||||
@@ -149,8 +156,8 @@ public sealed class BoilerMonitorEntityService(
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"No valid persisted BoilerMonitor state found in HA entities (runtime={Runtime}, sessions={Sessions})",
|
||||
runtimeState, sessionCountState);
|
||||
"No valid persisted BoilerMonitor state found in HA entities (runtime={Runtime}, sessions={Sessions})",
|
||||
runtimeState, sessionCountState);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -167,9 +174,9 @@ public sealed class BoilerMonitorEntityService(
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Restored persisted BoilerMonitor state: runtime={Runtime}, sessions={Sessions}, skipPriceCap={SkipPriceCap}, budgetOverride={BudgetOverride}",
|
||||
persisted.AccumulatedRunTime, persisted.SessionCount, persisted.SkipPriceCapState,
|
||||
persisted.BudgetOverrideState);
|
||||
"Restored persisted BoilerMonitor state: runtime={Runtime}, sessions={Sessions}, skipPriceCap={SkipPriceCap}, budgetOverride={BudgetOverride}",
|
||||
persisted.AccumulatedRunTime, persisted.SessionCount, persisted.SkipPriceCapState,
|
||||
persisted.BudgetOverrideState);
|
||||
}
|
||||
|
||||
return persisted;
|
||||
@@ -177,6 +184,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error reading persisted BoilerMonitor state from HA entities");
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -191,9 +199,16 @@ public sealed class BoilerMonitorEntityService(
|
||||
try
|
||||
{
|
||||
var entity = haContext.GetState(WaterReadyEntityId);
|
||||
if (entity?.Attributes is not IDictionary<string, object> attributes) return null;
|
||||
|
||||
if (!attributes.TryGetValue("cooldown_since", out var value)) return null;
|
||||
if (entity?.Attributes is not IDictionary<string, object> attributes)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!attributes.TryGetValue("cooldown_since", out var value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var stringValue = value switch
|
||||
{
|
||||
@@ -205,7 +220,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
|
||||
if (stringValue is not null &&
|
||||
DateTimeOffset.TryParse(stringValue, CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.RoundtripKind, out var parsed))
|
||||
DateTimeStyles.RoundtripKind, out var parsed))
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
@@ -237,10 +252,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
private static bool TryParseEntitySeconds(string? state, out double seconds)
|
||||
{
|
||||
seconds = 0;
|
||||
var tryParseEntitySeconds = !string.IsNullOrWhiteSpace(state)
|
||||
&& state is not ("unavailable" or "unknown")
|
||||
&& double.TryParse(state, CultureInfo.InvariantCulture, out seconds)
|
||||
&& seconds >= 0;
|
||||
var tryParseEntitySeconds = !string.IsNullOrWhiteSpace(state) && state is not ("unavailable" or "unknown") && double.TryParse(state, CultureInfo.InvariantCulture, out seconds) && seconds >= 0;
|
||||
|
||||
return tryParseEntitySeconds;
|
||||
}
|
||||
@@ -248,10 +260,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
private static bool TryParseEntityInt(string? state, out int value)
|
||||
{
|
||||
value = 0;
|
||||
var tryParseEntityInt = !string.IsNullOrWhiteSpace(state)
|
||||
&& state is not ("unavailable" or "unknown")
|
||||
&& int.TryParse(state, CultureInfo.InvariantCulture, out value)
|
||||
&& value >= 0;
|
||||
var tryParseEntityInt = !string.IsNullOrWhiteSpace(state) && state is not ("unavailable" or "unknown") && int.TryParse(state, CultureInfo.InvariantCulture, out value) && value >= 0;
|
||||
|
||||
return tryParseEntityInt;
|
||||
}
|
||||
@@ -283,7 +292,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
}
|
||||
},
|
||||
new BoilerMonitorEntity("number", "boilermonitor_config_heating_threshold",
|
||||
"Heating power threshold (watts)")
|
||||
"Heating power threshold (watts)")
|
||||
{
|
||||
AdditionalConfig = new
|
||||
{
|
||||
@@ -362,14 +371,16 @@ public sealed class BoilerMonitorEntityService(
|
||||
private async Task ResetWaterReadyAsync()
|
||||
{
|
||||
await SetEntityStateSafe(WaterReadyEntityId, "unknown").ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(WaterReadyEntityId, new
|
||||
{
|
||||
boiler_switch_state = "unknown",
|
||||
boiler_power_watts = 0.0,
|
||||
power_threshold_watts = DefaultHeatingPowerThresholdWatts,
|
||||
friendly_name = "Boiler water ready",
|
||||
last_updated = (string?)null
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
boiler_switch_state = "unknown",
|
||||
boiler_power_watts = 0.0,
|
||||
power_threshold_watts = DefaultHeatingPowerThresholdWatts,
|
||||
friendly_name = "Boiler water ready",
|
||||
last_updated = (string?)null
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ResetSkipPriceCapAsync()
|
||||
@@ -380,35 +391,41 @@ public sealed class BoilerMonitorEntityService(
|
||||
private async Task ResetTodayRuntimeAsync()
|
||||
{
|
||||
await SetEntityStateSafe(TodayRuntimeEntityId, "0").ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(TodayRuntimeEntityId, new
|
||||
{
|
||||
formatted = "0h 0m",
|
||||
friendly_name = "Today runtime",
|
||||
last_updated = (string?)null
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
formatted = "0h 0m",
|
||||
friendly_name = "Today runtime",
|
||||
last_updated = (string?)null
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ResetRemainingTimeAsync()
|
||||
{
|
||||
await SetEntityStateSafe(RemainingTimeEntityId, "0").ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(RemainingTimeEntityId, new
|
||||
{
|
||||
formatted = "0h 0m",
|
||||
budget_hours = 0.0,
|
||||
friendly_name = "Remaining budget time",
|
||||
last_updated = (string?)null
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
formatted = "0h 0m",
|
||||
budget_hours = 0.0,
|
||||
friendly_name = "Remaining budget time",
|
||||
last_updated = (string?)null
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ResetBudgetPercentageAsync()
|
||||
{
|
||||
await SetEntityStateSafe(BudgetPercentageEntityId, "0").ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(BudgetPercentageEntityId, new
|
||||
{
|
||||
unit_of_measurement = "%",
|
||||
friendly_name = "Budget usage",
|
||||
last_updated = (string?)null
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
unit_of_measurement = "%",
|
||||
friendly_name = "Budget usage",
|
||||
last_updated = (string?)null
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ResetBudgetExhaustedAsync()
|
||||
@@ -419,11 +436,13 @@ public sealed class BoilerMonitorEntityService(
|
||||
private async Task ResetSessionCountAsync()
|
||||
{
|
||||
await SetEntityStateSafe(SessionCountEntityId, "0").ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(SessionCountEntityId, new
|
||||
{
|
||||
friendly_name = "Heating sessions today",
|
||||
last_updated = (string?)null
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
friendly_name = "Heating sessions today",
|
||||
last_updated = (string?)null
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ResetBudgetOverrideAsync()
|
||||
@@ -439,54 +458,68 @@ public sealed class BoilerMonitorEntityService(
|
||||
private async Task UpdateWaterReadyAsync(BoilerMonitorUpdateContext ctx, string timestamp)
|
||||
{
|
||||
await SetEntityStateSafe(WaterReadyEntityId, ctx.WaterReady ? "yes" : "no").ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(WaterReadyEntityId, new
|
||||
{
|
||||
boiler_switch_state = ctx.BoilerSwitchState ?? "unknown",
|
||||
boiler_power_watts = ctx.BoilerPowerWatts,
|
||||
cooldown_since = ctx.CooldownSince?.ToString("O", CultureInfo.InvariantCulture),
|
||||
friendly_name = "Boiler water ready",
|
||||
last_updated = timestamp
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
boiler_switch_state = ctx.BoilerSwitchState ?? "unknown",
|
||||
boiler_power_watts = ctx.BoilerPowerWatts,
|
||||
cooldown_since = ctx.CooldownSince?.ToString("O", CultureInfo.InvariantCulture),
|
||||
friendly_name = "Boiler water ready",
|
||||
last_updated = timestamp
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UpdateTodayRuntimeAsync(TimeSpan runTime, string formatted, string timestamp)
|
||||
{
|
||||
await SetEntityStateSafe(TodayRuntimeEntityId,
|
||||
((int)runTime.TotalSeconds).ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
|
||||
((int)runTime.TotalSeconds).ToString(CultureInfo.InvariantCulture))
|
||||
.ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(TodayRuntimeEntityId, new
|
||||
{
|
||||
formatted,
|
||||
friendly_name = "Today runtime",
|
||||
last_updated = timestamp
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
formatted,
|
||||
friendly_name = "Today runtime",
|
||||
last_updated = timestamp
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UpdateRemainingTimeAsync(TimeSpan remaining, string formatted, double budgetHours,
|
||||
string timestamp)
|
||||
private async Task UpdateRemainingTimeAsync(TimeSpan remaining,
|
||||
string formatted,
|
||||
double budgetHours,
|
||||
string timestamp)
|
||||
{
|
||||
var remainingSeconds = Math.Max(0, (int)remaining.TotalSeconds);
|
||||
|
||||
await SetEntityStateSafe(RemainingTimeEntityId, remainingSeconds.ToString(CultureInfo.InvariantCulture))
|
||||
.ConfigureAwait(false);
|
||||
.ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(RemainingTimeEntityId, new
|
||||
{
|
||||
formatted,
|
||||
budget_hours = budgetHours,
|
||||
friendly_name = "Remaining budget time",
|
||||
last_updated = timestamp
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
formatted,
|
||||
budget_hours = budgetHours,
|
||||
friendly_name = "Remaining budget time",
|
||||
last_updated = timestamp
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UpdateBudgetPercentageAsync(double percentage, string timestamp)
|
||||
{
|
||||
var clampedPercentage = Math.Clamp(percentage, 0, 100);
|
||||
|
||||
await SetEntityStateSafe(BudgetPercentageEntityId,
|
||||
((int)clampedPercentage).ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
|
||||
((int)clampedPercentage).ToString(CultureInfo.InvariantCulture))
|
||||
.ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(BudgetPercentageEntityId, new
|
||||
{
|
||||
unit_of_measurement = "%",
|
||||
friendly_name = "Budget usage",
|
||||
last_updated = timestamp
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
unit_of_measurement = "%",
|
||||
friendly_name = "Budget usage",
|
||||
last_updated = timestamp
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UpdateBudgetExhaustedAsync(bool exhausted)
|
||||
@@ -497,12 +530,14 @@ public sealed class BoilerMonitorEntityService(
|
||||
private async Task UpdateSessionCountAsync(int sessionCount, string timestamp)
|
||||
{
|
||||
await SetEntityStateSafe(SessionCountEntityId, sessionCount.ToString(CultureInfo.InvariantCulture))
|
||||
.ConfigureAwait(false);
|
||||
.ConfigureAwait(false);
|
||||
|
||||
await SetEntityAttributesSafe(SessionCountEntityId, new
|
||||
{
|
||||
friendly_name = "Heating sessions today",
|
||||
last_updated = timestamp
|
||||
}).ConfigureAwait(false);
|
||||
{
|
||||
friendly_name = "Heating sessions today",
|
||||
last_updated = timestamp
|
||||
})
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task SetEntityStateSafe(string entityId, string state)
|
||||
@@ -510,6 +545,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
try
|
||||
{
|
||||
await entityManager.SetEntityState(entityId, state).ConfigureAwait(false);
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug("Successfully set state for {EntityId}: {State}", entityId, state);
|
||||
@@ -526,6 +562,7 @@ public sealed class BoilerMonitorEntityService(
|
||||
try
|
||||
{
|
||||
await entityManager.SetEntityAttributes(entityId, attributes).ConfigureAwait(false);
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug("Successfully set attributes for {EntityId}", entityId);
|
||||
|
||||
Reference in New Issue
Block a user