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.Reactive.Concurrency;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager.Interface;
|
||||
@@ -7,6 +9,8 @@ using Microsoft.Extensions.Options;
|
||||
using NetDaemon.AppModel;
|
||||
using NetDaemon.Extensions.Scheduler;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
[NetDaemonApp]
|
||||
@@ -58,12 +62,13 @@ public sealed class BatteryMonitorInit
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("BatteryMonitor initialized with threshold {Threshold}%",
|
||||
_options.BatteryThresholdPercent);
|
||||
_options.BatteryThresholdPercent);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error initializing BatteryMonitor");
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -71,17 +76,17 @@ public sealed class BatteryMonitorInit
|
||||
private void ScheduleDailyCheck()
|
||||
{
|
||||
_scheduler.ScheduleCron(BatteryMonitorConsts.DailyCheckCron, () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Running scheduled daily battery check");
|
||||
CheckAndNotify();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during scheduled battery check");
|
||||
}
|
||||
});
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Running scheduled daily battery check");
|
||||
CheckAndNotify();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during scheduled battery check");
|
||||
}
|
||||
});
|
||||
|
||||
_logger.LogInformation("Scheduled daily battery check at 08:00");
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#region
|
||||
|
||||
using BatteryMonitorManager.Interface;
|
||||
using BatteryMonitorManager.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NetDaemon.AppModel;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
public static class BatteryMonitorServiceRegistration
|
||||
@@ -11,15 +15,19 @@ public static class BatteryMonitorServiceRegistration
|
||||
public static IServiceCollection AddBatteryMonitor(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var options = configuration
|
||||
.GetSection(BatteryMonitorOptions.SectionName)
|
||||
.Get<BatteryMonitorOptions>() ?? new BatteryMonitorOptions();
|
||||
.GetSection(BatteryMonitorOptions.SectionName)
|
||||
.Get<BatteryMonitorOptions>() ??
|
||||
new BatteryMonitorOptions();
|
||||
|
||||
if (!options.Enabled) return services;
|
||||
if (!options.Enabled)
|
||||
{
|
||||
return services;
|
||||
}
|
||||
|
||||
services
|
||||
.AddNetDaemonApp<BatteryMonitorInit>()
|
||||
.AddScoped<IBatterySensorDiscoveryService, BatterySensorDiscoveryService>()
|
||||
.AddScoped<IBatteryNotificationService, BatteryNotificationService>();
|
||||
.AddNetDaemonApp<BatteryMonitorInit>()
|
||||
.AddScoped<IBatterySensorDiscoveryService, BatterySensorDiscoveryService>()
|
||||
.AddScoped<IBatteryNotificationService, BatteryNotificationService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#region
|
||||
|
||||
using System.Text;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager.Interface;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetDaemon.HassModel;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
public sealed class BatteryNotificationService(
|
||||
@@ -12,37 +16,40 @@ public sealed class BatteryNotificationService(
|
||||
{
|
||||
public void NotifyLowBattery(IReadOnlyList<BatterySensorInfo> lowBatteryDevices)
|
||||
{
|
||||
if (lowBatteryDevices.Count == 0) return;
|
||||
if (lowBatteryDevices.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var message = BuildNotificationMessage(lowBatteryDevices);
|
||||
|
||||
haContext.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
data: new
|
||||
{
|
||||
title = BatteryMonitorConsts.NotificationTitle,
|
||||
message,
|
||||
notification_id = BatteryMonitorConsts.NotificationId
|
||||
});
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
data: new
|
||||
{
|
||||
title = BatteryMonitorConsts.NotificationTitle,
|
||||
message,
|
||||
notification_id = BatteryMonitorConsts.NotificationId
|
||||
});
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Sent low battery notification for {Count} device(s)",
|
||||
lowBatteryDevices.Count);
|
||||
"Sent low battery notification for {Count} device(s)",
|
||||
lowBatteryDevices.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public void DismissNotification()
|
||||
{
|
||||
haContext.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationDismissService,
|
||||
data: new
|
||||
{
|
||||
notification_id = BatteryMonitorConsts.NotificationId
|
||||
});
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationDismissService,
|
||||
data: new
|
||||
{
|
||||
notification_id = BatteryMonitorConsts.NotificationId
|
||||
});
|
||||
|
||||
logger.LogDebug("Dismissed low battery notification");
|
||||
}
|
||||
@@ -54,7 +61,9 @@ public sealed class BatteryNotificationService(
|
||||
sb.AppendLine();
|
||||
|
||||
foreach (var device in devices.OrderBy(d => d.BatteryLevel))
|
||||
{
|
||||
sb.AppendLine($"- **{device.FriendlyName}**: {device.BatteryLevel:F0}%");
|
||||
}
|
||||
|
||||
return sb.ToString().TrimEnd();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#region
|
||||
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using BatteryMonitor.Models;
|
||||
@@ -6,6 +8,8 @@ using Microsoft.Extensions.Logging;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
public sealed class BatterySensorDiscoveryService(
|
||||
@@ -17,17 +21,17 @@ public sealed class BatterySensorDiscoveryService(
|
||||
var allEntities = haContext.GetAllEntities();
|
||||
|
||||
var lowBatterySensors = allEntities
|
||||
.Where(IsBatterySensor)
|
||||
.Where(e => !IsExcludedDevice(e.EntityId))
|
||||
.Select(e => TryCreateSensorInfo(e, thresholdPercent))
|
||||
.Where(info => info is not null)
|
||||
.ToList();
|
||||
.Where(IsBatterySensor)
|
||||
.Where(e => !IsExcludedDevice(e.EntityId))
|
||||
.Select(e => TryCreateSensorInfo(e, thresholdPercent))
|
||||
.Where(info => info is not null)
|
||||
.ToList();
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Battery check: found {Count} sensors below {Threshold}%",
|
||||
lowBatterySensors.Count, thresholdPercent);
|
||||
"Battery check: found {Count} sensors below {Threshold}%",
|
||||
lowBatterySensors.Count, thresholdPercent);
|
||||
}
|
||||
|
||||
return lowBatterySensors!;
|
||||
@@ -50,7 +54,7 @@ public sealed class BatterySensorDiscoveryService(
|
||||
var lowerEntityId = entityId.ToLowerInvariant();
|
||||
|
||||
return BatteryMonitorConsts.ExcludedKeywords
|
||||
.Any(keyword => lowerEntityId.Contains(keyword, StringComparison.OrdinalIgnoreCase));
|
||||
.Any(keyword => lowerEntityId.Contains(keyword, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private BatterySensorInfo? TryCreateSensorInfo(Entity entity, int thresholdPercent)
|
||||
@@ -62,7 +66,7 @@ public sealed class BatterySensorDiscoveryService(
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug("Skipping battery sensor {EntityId}: state is {State}", entity.EntityId,
|
||||
state ?? "null");
|
||||
state ?? "null");
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -73,16 +77,18 @@ public sealed class BatterySensorDiscoveryService(
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug("Skipping battery sensor {EntityId}: unable to parse state '{State}'", entity.EntityId,
|
||||
state);
|
||||
state);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (batteryLevel >= thresholdPercent) return null;
|
||||
if (batteryLevel >= thresholdPercent)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var friendlyName = GetJsonStringProperty(entity.EntityState?.AttributesJson, "friendly_name")
|
||||
?? entity.EntityId;
|
||||
var friendlyName = GetJsonStringProperty(entity.EntityState?.AttributesJson, "friendly_name") ?? entity.EntityId;
|
||||
|
||||
return new BatterySensorInfo
|
||||
{
|
||||
@@ -94,10 +100,13 @@ public sealed class BatterySensorDiscoveryService(
|
||||
|
||||
private static string? GetJsonStringProperty(JsonElement? element, string propertyName)
|
||||
{
|
||||
if (element is null) return null;
|
||||
if (element is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return element.Value.TryGetProperty(propertyName, out var value)
|
||||
? value.GetString()
|
||||
: null;
|
||||
? value.GetString()
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
#region
|
||||
|
||||
using BatteryMonitor.Models;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BatteryMonitorManager.Interface;
|
||||
|
||||
public interface IBatteryNotificationService
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#region
|
||||
|
||||
using BatteryMonitor.Models;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BatteryMonitorManager.Interface;
|
||||
|
||||
public interface IBatterySensorDiscoveryService
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#region
|
||||
|
||||
using BatteryMonitor.Models;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BatteryMonitorManager.Models;
|
||||
|
||||
public sealed class BatteryMonitorOptions
|
||||
|
||||
Reference in New Issue
Block a user