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:
2026-12-01 12:33:31 +02:00
parent 77458383d1
commit f919d6b25b
115 changed files with 3115 additions and 1959 deletions
+10 -6
View File
@@ -1,8 +1,12 @@
#region
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
#endregion
namespace HomeAssistantServer;
public static class CustomLoggingProvider
@@ -10,14 +14,14 @@ public static class CustomLoggingProvider
public static IHostBuilder UseCustomLogging(this IHostBuilder builder)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json",
true)
.Build();
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json",
true)
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
.ReadFrom.Configuration(configuration)
.CreateLogger();
return builder.UseSerilog(logger);
}
+38 -25
View File
@@ -1,3 +1,5 @@
#region
using System;
using System.Collections.Generic;
using System.Text.Json;
@@ -13,6 +15,8 @@ using NordPoolManager.Interface;
using PowerMonitorManager.Interface;
using ShowerMonitorManager.Interface;
#endregion
namespace HomeAssistantServer;
[NetDaemonApp]
@@ -50,23 +54,24 @@ public sealed class MqttRecreateApp
await Task.Delay(TimeSpan.FromSeconds(5));
await _mqttEntityManager.CreateAsync(
RecreateButtonEntityId,
new EntityCreationOptions
{
UniqueId = RecreateButtonEntityId,
Name = "Recreate all MQTT entities"
},
new
{
icon = "mdi:refresh",
device = new
{
identifiers = AppDaemonIdentifiers,
name = "AppDaemon",
model = "AppDaemon",
manufacturer = "HomeAssistant App Daemon"
}
}).ConfigureAwait(false);
RecreateButtonEntityId,
new EntityCreationOptions
{
UniqueId = RecreateButtonEntityId,
Name = "Recreate all MQTT entities"
},
new
{
icon = "mdi:refresh",
device = new
{
identifiers = AppDaemonIdentifiers,
name = "AppDaemon",
model = "AppDaemon",
manufacturer = "HomeAssistant App Daemon"
}
})
.ConfigureAwait(false);
if (_logger.IsEnabled(LogLevel.Information))
{
@@ -74,12 +79,13 @@ public sealed class MqttRecreateApp
}
var button = new Entity(_haContext, RecreateButtonEntityId);
button.StateChanges()
.Subscribe(_ =>
{
_logger.LogInformation("Recreate-all button pressed, recreating all MQTT entities");
Task.Run(HandleRecreateAsync);
});
.Subscribe(_ =>
{
_logger.LogInformation("Recreate-all button pressed, recreating all MQTT entities");
Task.Run(HandleRecreateAsync);
});
}
catch (Exception ex)
{
@@ -107,9 +113,11 @@ public sealed class MqttRecreateApp
private async Task RecreateNordPoolAsync()
{
var entityService = _serviceProvider.GetService<INordPoolEntityService>();
if (entityService is null)
{
_logger.LogDebug("NordPool entity service not registered (disabled), skipping");
return;
}
@@ -132,9 +140,11 @@ public sealed class MqttRecreateApp
private async Task RecreateBoilerMonitorAsync()
{
var entityService = _serviceProvider.GetService<IBoilerMonitorEntityService>();
if (entityService is null)
{
_logger.LogDebug("BoilerMonitor entity service not registered (disabled), skipping");
return;
}
@@ -155,9 +165,11 @@ public sealed class MqttRecreateApp
private async Task RecreateShowerMonitorAsync()
{
var entityService = _serviceProvider.GetService<IShowerMonitorEntityService>();
if (entityService is null)
{
_logger.LogDebug("ShowerMonitor entity service not registered (disabled), skipping");
return;
}
@@ -177,9 +189,11 @@ public sealed class MqttRecreateApp
private async Task RecreatePowerMonitorAsync()
{
var entityService = _serviceProvider.GetService<IPowerMonitorEntityService>();
if (entityService is null)
{
_logger.LogDebug("PowerMonitor entity service not registered (disabled), skipping");
return;
}
@@ -202,14 +216,13 @@ public sealed class MqttRecreateApp
try
{
var state = _haContext.GetState(NordPoolPriceEntityId);
if (state?.Attributes is IDictionary<string, object> attributes &&
attributes.TryGetValue("unit_of_measurement", out var value))
{
return value switch
{
string s => s,
JsonElement { ValueKind: JsonValueKind.String } je => je.GetString(),
_ => null
string s => s, JsonElement { ValueKind: JsonValueKind.String } je => je.GetString(), _ => null
};
}
}
+43 -38
View File
@@ -1,3 +1,5 @@
#region
using System;
using System.Reflection;
using BatteryMonitorManager;
@@ -28,47 +30,50 @@ using PowerMonitorManager.Models;
using ShowerMonitorManager;
using ShowerMonitorManager.Models;
#endregion
try
{
var app = Host.CreateDefaultBuilder(args)
.UseNetDaemonAppSettings()
.UseCustomLogging()
.UseNetDaemonRuntime()
.UseNetDaemonTextToSpeech()
.ConfigureServices((context, services) =>
{
services
.AddNetDaemonMqttEntityManagement()
.AddAppsFromAssembly(Assembly.GetExecutingAssembly())
.AddNetDaemonStateManager()
.AddNetDaemonScheduler()
.AddHomeAssistantGenerated()
.AddAutoMapper(config =>
{
config.LicenseKey =
"eyJhbGciOiJSUzI1NiIsImtpZCI6Ikx1Y2t5UGVubnlTb2Z0d2FyZUxpY2Vuc2VLZXkvYmJiMTNhY2I1OTkwNGQ4OWI0Y2IxYzg1ZjA4OGNjZjkiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2x1Y2t5cGVubnlzb2Z0d2FyZS5jb20iLCJhdWQiOiJMdWNreVBlbm55U29mdHdhcmUiLCJleHAiOiIxODMwMDM4NDAwIiwiaWF0IjoiMTc2NDAwNTE3OSIsImFjY291bnRfaWQiOiIwMTlhYjZlNjFjZWM3ZjhhYWQzMjE4ZmQ0ODdkYTgwNiIsImN1c3RvbWVyX2lkIjoiMDE5YWI2ZTYxY2VjN2Y4YWFkMzIxOGZkNDg3ZGE4MDYiLCJzdWJfaWQiOiItIiwiZWRpdGlvbiI6IjAiLCJ0eXBlIjoiMiIsInRlcm1fZW5kIjoiMTgyNzQ0NjQwMCJ9.VgEgdxh9xBudubLZr90mUiCORAhLx00Fz7lw1bUMhLWICjBposFrBWrgccKb_irFis_6qUyi567VbsmbjTDxAp9-f0QI0VW4_Zr9wcNeCrDzE0U-DKdcWoMpHoJYUf62tMscGcgVJEKgMX27KLWWqAQzcJm8X-O072A7pW1_x_xkAorFOIfahKXdECnUUdHpPUdrClUbxfWS-wkkKBP0AnrnYY1N2FGlqsJj5_ro1Mmcpijx0sxUKSl1FKaHmx6pOHq-rvJEF7Vvi35vO4HQYFnyP-hN2fQSkgK-Eg8LG6BqBLDXK33VlI6WMO0nOUG62KWAI7jXPSZhrSjBZfxy0g";
config.AddProfile<NordPoolMappingProfile>();
config.AddProfile<GlobalMappingProfile>();
})
.AddTransient<IEntityManager, EntityManager>()
.AddSingleton(TimeProvider.System)
.AddScoped<IBoilerConfigReader, BoilerConfigReader>()
.Configure<NordPoolOptions>(context.Configuration.GetSection(NordPoolOptions.SectionName))
.AddNordPool()
.Configure<PowerMonitorOptions>(context.Configuration.GetSection(PowerMonitorOptions.SectionName))
.AddPowerMonitor()
.Configure<BoilerMonitorOptions>(context.Configuration.GetSection(BoilerMonitorOptions.SectionName))
.AddBoilerMonitor(context.Configuration)
.Configure<ShowerMonitorOptions>(context.Configuration.GetSection(ShowerMonitorOptions.SectionName))
.AddShowerMonitor(context.Configuration)
.Configure<LightMonitorOptions>(context.Configuration.GetSection(LightMonitorOptions.SectionName))
.AddLightMonitor(context.Configuration)
.Configure<BatteryMonitorOptions>(context.Configuration.GetSection(BatteryMonitorOptions.SectionName))
.AddBatteryMonitor(context.Configuration)
.Configure<LeakMonitorOptions>(context.Configuration.GetSection(LeakMonitorOptions.SectionName))
.AddLeakMonitor(context.Configuration);
})
.Build();
.UseNetDaemonAppSettings()
.UseCustomLogging()
.UseNetDaemonRuntime()
.UseNetDaemonTextToSpeech()
.ConfigureServices((context, services) =>
{
services
.AddNetDaemonMqttEntityManagement()
.AddAppsFromAssembly(Assembly.GetExecutingAssembly())
.AddNetDaemonStateManager()
.AddNetDaemonScheduler()
.AddHomeAssistantGenerated()
.AddAutoMapper(config =>
{
config.LicenseKey =
"eyJhbGciOiJSUzI1NiIsImtpZCI6Ikx1Y2t5UGVubnlTb2Z0d2FyZUxpY2Vuc2VLZXkvYmJiMTNhY2I1OTkwNGQ4OWI0Y2IxYzg1ZjA4OGNjZjkiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2x1Y2t5cGVubnlzb2Z0d2FyZS5jb20iLCJhdWQiOiJMdWNreVBlbm55U29mdHdhcmUiLCJleHAiOiIxODMwMDM4NDAwIiwiaWF0IjoiMTc2NDAwNTE3OSIsImFjY291bnRfaWQiOiIwMTlhYjZlNjFjZWM3ZjhhYWQzMjE4ZmQ0ODdkYTgwNiIsImN1c3RvbWVyX2lkIjoiMDE5YWI2ZTYxY2VjN2Y4YWFkMzIxOGZkNDg3ZGE4MDYiLCJzdWJfaWQiOiItIiwiZWRpdGlvbiI6IjAiLCJ0eXBlIjoiMiIsInRlcm1fZW5kIjoiMTgyNzQ0NjQwMCJ9.VgEgdxh9xBudubLZr90mUiCORAhLx00Fz7lw1bUMhLWICjBposFrBWrgccKb_irFis_6qUyi567VbsmbjTDxAp9-f0QI0VW4_Zr9wcNeCrDzE0U-DKdcWoMpHoJYUf62tMscGcgVJEKgMX27KLWWqAQzcJm8X-O072A7pW1_x_xkAorFOIfahKXdECnUUdHpPUdrClUbxfWS-wkkKBP0AnrnYY1N2FGlqsJj5_ro1Mmcpijx0sxUKSl1FKaHmx6pOHq-rvJEF7Vvi35vO4HQYFnyP-hN2fQSkgK-Eg8LG6BqBLDXK33VlI6WMO0nOUG62KWAI7jXPSZhrSjBZfxy0g";
config.AddProfile<NordPoolMappingProfile>();
config.AddProfile<GlobalMappingProfile>();
})
.AddTransient<IEntityManager, EntityManager>()
.AddSingleton(TimeProvider.System)
.AddScoped<IBoilerConfigReader, BoilerConfigReader>()
.Configure<NordPoolOptions>(context.Configuration.GetSection(NordPoolOptions.SectionName))
.AddNordPool()
.Configure<PowerMonitorOptions>(context.Configuration.GetSection(PowerMonitorOptions.SectionName))
.AddPowerMonitor()
.Configure<BoilerMonitorOptions>(context.Configuration.GetSection(BoilerMonitorOptions.SectionName))
.AddBoilerMonitor(context.Configuration)
.Configure<ShowerMonitorOptions>(context.Configuration.GetSection(ShowerMonitorOptions.SectionName))
.AddShowerMonitor(context.Configuration)
.Configure<LightMonitorOptions>(context.Configuration.GetSection(LightMonitorOptions.SectionName))
.AddLightMonitor(context.Configuration)
.Configure<BatteryMonitorOptions>(context.Configuration.GetSection(BatteryMonitorOptions.SectionName))
.AddBatteryMonitor(context.Configuration)
.Configure<LeakMonitorOptions>(context.Configuration.GetSection(LeakMonitorOptions.SectionName))
.AddLeakMonitor(context.Configuration);
})
.Build();
await app.RunAsync().ConfigureAwait(false);
}