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:
@@ -8,20 +8,20 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>BatteryMonitor.Manager.Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>BatteryMonitor.Manager.Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HomeAssistant.Entities\HomeAssistant.Entities.csproj" />
|
||||
<ProjectReference Include="..\BatteryMonitor.Models\BatteryMonitor.Models.csproj" />
|
||||
<ProjectReference Include="..\HomeAssistant.Entities\HomeAssistant.Entities.csproj"/>
|
||||
<ProjectReference Include="..\BatteryMonitor.Models\BatteryMonitor.Models.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="11.0.0" />
|
||||
<PackageReference Include="NetDaemon.Extensions.Scheduling" Version="26.43.1" />
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.43.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="11.0.0"/>
|
||||
<PackageReference Include="NetDaemon.Extensions.Scheduling" Version="26.43.1"/>
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.43.1"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
using System.Reactive.Concurrency;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager.Interface;
|
||||
using BatteryMonitorManager.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NetDaemon.AppModel;
|
||||
using NetDaemon.Extensions.Scheduler;
|
||||
using BatteryMonitorManager.Interface;
|
||||
using BatteryMonitorManager.Models;
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
[NetDaemonApp]
|
||||
public sealed class BatteryMonitorInit
|
||||
{
|
||||
private readonly ILogger<BatteryMonitorInit> _logger;
|
||||
private readonly IScheduler _scheduler;
|
||||
private readonly IBatterySensorDiscoveryService _discoveryService;
|
||||
private readonly ILogger<BatteryMonitorInit> _logger;
|
||||
private readonly IBatteryNotificationService _notificationService;
|
||||
private readonly BatteryMonitorOptions _options;
|
||||
private readonly IScheduler _scheduler;
|
||||
|
||||
public BatteryMonitorInit(
|
||||
ILogger<BatteryMonitorInit> logger,
|
||||
@@ -55,9 +56,8 @@ public sealed class BatteryMonitorInit
|
||||
CheckAndNotify();
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("BatteryMonitor initialized with threshold {Threshold}%", _options.BatteryThresholdPercent);
|
||||
}
|
||||
_logger.LogInformation("BatteryMonitor initialized with threshold {Threshold}%",
|
||||
_options.BatteryThresholdPercent);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -68,7 +68,7 @@ public sealed class BatteryMonitorInit
|
||||
|
||||
private void ScheduleDailyCheck()
|
||||
{
|
||||
_scheduler.ScheduleCron(BatteryMonitor.Models.BatteryMonitorConsts.DailyCheckCron, () =>
|
||||
_scheduler.ScheduleCron(BatteryMonitorConsts.DailyCheckCron, () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -98,4 +98,4 @@ public sealed class BatteryMonitorInit
|
||||
_logger.LogDebug("No low battery devices found");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
using BatteryMonitorManager.Interface;
|
||||
using BatteryMonitorManager.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NetDaemon.AppModel;
|
||||
using BatteryMonitorManager.Interface;
|
||||
using BatteryMonitorManager.Models;
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
@@ -14,10 +14,7 @@ public static class BatteryMonitorServiceRegistration
|
||||
.GetSection(BatteryMonitorOptions.SectionName)
|
||||
.Get<BatteryMonitorOptions>() ?? new BatteryMonitorOptions();
|
||||
|
||||
if (!options.Enabled)
|
||||
{
|
||||
return services;
|
||||
}
|
||||
if (!options.Enabled) return services;
|
||||
|
||||
services
|
||||
.AddNetDaemonApp<BatteryMonitorInit>()
|
||||
@@ -26,4 +23,4 @@ public static class BatteryMonitorServiceRegistration
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetDaemon.HassModel;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager.Interface;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetDaemon.HassModel;
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
@@ -12,10 +12,7 @@ public sealed class BatteryNotificationService(
|
||||
{
|
||||
public void NotifyLowBattery(IReadOnlyList<BatterySensorInfo> lowBatteryDevices)
|
||||
{
|
||||
if (lowBatteryDevices.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (lowBatteryDevices.Count == 0) return;
|
||||
|
||||
var message = BuildNotificationMessage(lowBatteryDevices);
|
||||
|
||||
@@ -30,11 +27,9 @@ public sealed class BatteryNotificationService(
|
||||
});
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Sent low battery notification for {Count} device(s)",
|
||||
lowBatteryDevices.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public void DismissNotification()
|
||||
@@ -57,10 +52,8 @@ 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,10 +1,10 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager.Interface;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager.Interface;
|
||||
|
||||
namespace BatteryMonitorManager;
|
||||
|
||||
@@ -24,11 +24,9 @@ public sealed class BatterySensorDiscoveryService(
|
||||
.ToList();
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Battery check: found {Count} sensors below {Threshold}%",
|
||||
lowBatterySensors.Count, thresholdPercent);
|
||||
}
|
||||
|
||||
return lowBatterySensors!;
|
||||
}
|
||||
@@ -36,9 +34,7 @@ public sealed class BatterySensorDiscoveryService(
|
||||
internal static bool IsBatterySensor(Entity entity)
|
||||
{
|
||||
if (!entity.EntityId.StartsWith(BatteryMonitorConsts.SensorDomainPrefix, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var deviceClass = GetJsonStringProperty(entity.EntityState?.AttributesJson, "device_class");
|
||||
|
||||
@@ -60,9 +56,8 @@ public sealed class BatterySensorDiscoveryService(
|
||||
if (state is null or "unavailable" or "unknown")
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug("Skipping battery sensor {EntityId}: state is {State}", entity.EntityId, state ?? "null");
|
||||
}
|
||||
logger.LogDebug("Skipping battery sensor {EntityId}: state is {State}", entity.EntityId,
|
||||
state ?? "null");
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -70,17 +65,13 @@ public sealed class BatterySensorDiscoveryService(
|
||||
if (!double.TryParse(state, CultureInfo.InvariantCulture, out var batteryLevel))
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug("Skipping battery sensor {EntityId}: unable to parse state '{State}'", entity.EntityId, state);
|
||||
}
|
||||
logger.LogDebug("Skipping battery sensor {EntityId}: unable to parse state '{State}'", entity.EntityId,
|
||||
state);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (batteryLevel >= thresholdPercent)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (batteryLevel >= thresholdPercent) return null;
|
||||
|
||||
var friendlyName = GetJsonStringProperty(entity.EntityState?.AttributesJson, "friendly_name")
|
||||
?? entity.EntityId;
|
||||
@@ -95,13 +86,10 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,4 @@ public interface IBatteryNotificationService
|
||||
void NotifyLowBattery(IReadOnlyList<BatterySensorInfo> lowBatteryDevices);
|
||||
|
||||
void DismissNotification();
|
||||
}
|
||||
}
|
||||
@@ -5,4 +5,4 @@ namespace BatteryMonitorManager.Interface;
|
||||
public interface IBatterySensorDiscoveryService
|
||||
{
|
||||
IReadOnlyList<BatterySensorInfo> DiscoverLowBatterySensors(int thresholdPercent);
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,4 @@ public sealed class BatteryMonitorOptions
|
||||
public bool Enabled { get; init; }
|
||||
|
||||
public int BatteryThresholdPercent { get; init; } = BatteryMonitorConsts.DefaultBatteryThresholdPercent;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user