Apply code formatting cleanup across monitor projects

- 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
This commit is contained in:
2026-11-30 11:52:06 +02:00
parent cf8c9434c8
commit 24729ee267
165 changed files with 2513 additions and 2470 deletions
@@ -49,9 +49,7 @@ public sealed class PowerMonitorEntityService(
try
{
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation("Creating MQTT entity: {EntityId}", entity.EntityId);
}
await entityManager.CreateEntityAsync(entity).ConfigureAwait(false);
}
@@ -64,7 +62,8 @@ public sealed class PowerMonitorEntityService(
await Task.WhenAll(tasks).ConfigureAwait(false);
}
public async Task CreateDeviceEntitiesAsync(IReadOnlyList<PowerSensorInfo> sensors, CancellationToken cancellationToken = default)
public async Task CreateDeviceEntitiesAsync(IReadOnlyList<PowerSensorInfo> sensors,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
@@ -74,9 +73,7 @@ public sealed class PowerMonitorEntityService(
try
{
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation("Creating device cost entity: {EntityId}", entity.EntityId);
}
await entityManager.CreateEntityAsync(entity).ConfigureAwait(false);
}
@@ -98,15 +95,23 @@ public sealed class PowerMonitorEntityService(
var tasks = new List<Task>
{
UpdateTotalPowerAsync(context, timestamp),
UpdateCostEntityAsync(TotalCost15MinEntityId, context.TotalCost.Cost15Min, context.TotalCost, "Total cost (15 min)", timestamp),
UpdateCostEntityAsync(TotalCost1HEntityId, context.TotalCost.Cost1Hour, context.TotalCost, "Total cost (1h)", timestamp),
UpdateCostEntityAsync(TotalCostDailyEntityId, context.TotalCost.CostDaily, context.TotalCost, "Total cost (daily)", timestamp),
UpdateCostEntityAsync(TotalCostMonthlyEntityId, context.TotalCost.CostMonthly, context.TotalCost, "Total cost (monthly)", timestamp),
UpdateCostEntityAsync(TotalCost15MinEntityId, context.TotalCost.Cost15Min, context.TotalCost,
"Total cost (15 min)", timestamp),
UpdateCostEntityAsync(TotalCost1HEntityId, context.TotalCost.Cost1Hour, context.TotalCost,
"Total cost (1h)", timestamp),
UpdateCostEntityAsync(TotalCostDailyEntityId, context.TotalCost.CostDaily, context.TotalCost,
"Total cost (daily)", timestamp),
UpdateCostEntityAsync(TotalCostMonthlyEntityId, context.TotalCost.CostMonthly, context.TotalCost,
"Total cost (monthly)", timestamp),
UpdateOtherPowerAsync(context, timestamp),
UpdateCostEntityAsync(OtherCost15MinEntityId, context.OtherCost.Cost15Min, context.OtherCost, "Other cost (15 min)", timestamp),
UpdateCostEntityAsync(OtherCost1HEntityId, context.OtherCost.Cost1Hour, context.OtherCost, "Other cost (1h)", timestamp),
UpdateCostEntityAsync(OtherCostDailyEntityId, context.OtherCost.CostDaily, context.OtherCost, "Other cost (daily)", timestamp),
UpdateCostEntityAsync(OtherCostMonthlyEntityId, context.OtherCost.CostMonthly, context.OtherCost, "Other cost (monthly)", timestamp),
UpdateCostEntityAsync(OtherCost15MinEntityId, context.OtherCost.Cost15Min, context.OtherCost,
"Other cost (15 min)", timestamp),
UpdateCostEntityAsync(OtherCost1HEntityId, context.OtherCost.Cost1Hour, context.OtherCost,
"Other cost (1h)", timestamp),
UpdateCostEntityAsync(OtherCostDailyEntityId, context.OtherCost.CostDaily, context.OtherCost,
"Other cost (daily)", timestamp),
UpdateCostEntityAsync(OtherCostMonthlyEntityId, context.OtherCost.CostMonthly, context.OtherCost,
"Other cost (monthly)", timestamp),
UpdateAnomalyAsync(context, timestamp),
UpdateStatusAsync(context, timestamp)
};
@@ -114,10 +119,14 @@ public sealed class PowerMonitorEntityService(
foreach (var (entityId, costSnapshot) in context.DeviceCosts)
{
var deviceName = PowerSensorDiscoveryService.ExtractDeviceName(entityId);
tasks.Add(UpdateCostEntityAsync($"sensor.powermonitor_{deviceName}_cost_15min", costSnapshot.Cost15Min, costSnapshot, $"{deviceName} cost (15 min)", timestamp));
tasks.Add(UpdateCostEntityAsync($"sensor.powermonitor_{deviceName}_cost_1h", costSnapshot.Cost1Hour, costSnapshot, $"{deviceName} cost (1h)", timestamp));
tasks.Add(UpdateCostEntityAsync($"sensor.powermonitor_{deviceName}_cost_daily", costSnapshot.CostDaily, costSnapshot, $"{deviceName} cost (daily)", timestamp));
tasks.Add(UpdateCostEntityAsync($"sensor.powermonitor_{deviceName}_cost_monthly", costSnapshot.CostMonthly, costSnapshot, $"{deviceName} cost (monthly)", timestamp));
tasks.Add(UpdateCostEntityAsync($"sensor.powermonitor_{deviceName}_cost_15min", costSnapshot.Cost15Min,
costSnapshot, $"{deviceName} cost (15 min)", timestamp));
tasks.Add(UpdateCostEntityAsync($"sensor.powermonitor_{deviceName}_cost_1h", costSnapshot.Cost1Hour,
costSnapshot, $"{deviceName} cost (1h)", timestamp));
tasks.Add(UpdateCostEntityAsync($"sensor.powermonitor_{deviceName}_cost_daily", costSnapshot.CostDaily,
costSnapshot, $"{deviceName} cost (daily)", timestamp));
tasks.Add(UpdateCostEntityAsync($"sensor.powermonitor_{deviceName}_cost_monthly", costSnapshot.CostMonthly,
costSnapshot, $"{deviceName} cost (monthly)", timestamp));
}
await Task.WhenAll(tasks).ConfigureAwait(false);
@@ -154,9 +163,9 @@ public sealed class PowerMonitorEntityService(
try
{
var tasks = SystemEntityIds.Select(entityManager.RemoveEntity);
await Task.WhenAll(tasks).ConfigureAwait(false);
logger.LogInformation("PowerMonitor system entities cleaned up");
}
catch (Exception ex)
@@ -166,22 +175,20 @@ public sealed class PowerMonitorEntityService(
}
}
public async Task RemoveDeviceEntitiesAsync(IReadOnlyList<string> removedDeviceNames, CancellationToken cancellationToken = default)
public async Task RemoveDeviceEntitiesAsync(IReadOnlyList<string> removedDeviceNames,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var entityIds = removedDeviceNames.SelectMany(GetDeviceCostEntityIds).ToList();
var tasks = entityIds.Select(async id =>
{
try
{
await entityManager.RemoveEntity(id).ConfigureAwait(false);
if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug("Entity removed: {EntityId}", id);
}
if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug("Entity removed: {EntityId}", id);
}
catch (Exception ex)
{
@@ -191,7 +198,7 @@ public sealed class PowerMonitorEntityService(
await Task.WhenAll(tasks).ConfigureAwait(false);
}
internal static ICollection<PowerMonitorEntity> GetSystemEntitiesList()
{
@@ -199,7 +206,12 @@ public sealed class PowerMonitorEntityService(
{
new("sensor", "powermonitor_total_power", "PowerMonitor total power")
{
AdditionalConfig = new { unit_of_measurement = PowerMonitorConsts.WattUnit, device_class = PowerMonitorConsts.PowerDeviceClass, state_class = PowerMonitorConsts.MeasurementStateClass }
AdditionalConfig = new
{
unit_of_measurement = PowerMonitorConsts.WattUnit,
device_class = PowerMonitorConsts.PowerDeviceClass,
state_class = PowerMonitorConsts.MeasurementStateClass
}
},
CreateCostEntity("powermonitor_total_cost_15min", "PowerMonitor total cost (15 min)"),
CreateCostEntity("powermonitor_total_cost_1h", "PowerMonitor total cost (1h)"),
@@ -207,7 +219,12 @@ public sealed class PowerMonitorEntityService(
CreateCostEntity("powermonitor_total_cost_monthly", "PowerMonitor total cost (monthly)"),
new("sensor", "powermonitor_other_power", "PowerMonitor other power")
{
AdditionalConfig = new { unit_of_measurement = PowerMonitorConsts.WattUnit, device_class = PowerMonitorConsts.PowerDeviceClass, state_class = PowerMonitorConsts.MeasurementStateClass }
AdditionalConfig = new
{
unit_of_measurement = PowerMonitorConsts.WattUnit,
device_class = PowerMonitorConsts.PowerDeviceClass,
state_class = PowerMonitorConsts.MeasurementStateClass
}
},
CreateCostEntity("powermonitor_other_cost_15min", "PowerMonitor other cost (15 min)"),
CreateCostEntity("powermonitor_other_cost_1h", "PowerMonitor other cost (1h)"),
@@ -293,7 +310,8 @@ public sealed class PowerMonitorEntityService(
}).ConfigureAwait(false);
}
private async Task UpdateCostEntityAsync(string entityId, double costValue, CostSnapshot snapshot, string friendlyName, string timestamp)
private async Task UpdateCostEntityAsync(string entityId, double costValue, CostSnapshot snapshot,
string friendlyName, string timestamp)
{
var value = costValue.ToString("F4", CultureInfo.InvariantCulture);
await SetEntityStateSafe(entityId, value).ConfigureAwait(false);
@@ -360,4 +378,4 @@ public sealed class PowerMonitorEntityService(
logger.LogError(ex, "Failed to set attributes for {EntityId}", entityId);
}
}
}
}