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.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -9,6 +11,8 @@ using PowerMonitorManager.Interface;
|
||||
using PowerMonitorManager.Models;
|
||||
using PowerMonitorManager.Models.Consts;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace PowerMonitorManager;
|
||||
|
||||
public sealed class PowerSensorDiscoveryService : IPowerSensorDiscoveryService
|
||||
@@ -38,25 +42,25 @@ public sealed class PowerSensorDiscoveryService : IPowerSensorDiscoveryService
|
||||
var allEntities = _haContext.GetAllEntities();
|
||||
|
||||
var powerSensors = allEntities
|
||||
.Where(IsPowerSensor)
|
||||
.Where(e => !IsMainMeter(e.EntityId))
|
||||
.Where(e => !IsExcluded(e.EntityId))
|
||||
.Select(e => new PowerSensorInfo
|
||||
{
|
||||
EntityId = e.EntityId,
|
||||
FriendlyName = GetFriendlyName(e),
|
||||
DeviceName = ExtractDeviceName(e.EntityId),
|
||||
UnitOfMeasurement = GetUnitOfMeasurement(e)
|
||||
})
|
||||
.ToList();
|
||||
.Where(IsPowerSensor)
|
||||
.Where(e => !IsMainMeter(e.EntityId))
|
||||
.Where(e => !IsExcluded(e.EntityId))
|
||||
.Select(e => new PowerSensorInfo
|
||||
{
|
||||
EntityId = e.EntityId,
|
||||
FriendlyName = GetFriendlyName(e),
|
||||
DeviceName = ExtractDeviceName(e.EntityId),
|
||||
UnitOfMeasurement = GetUnitOfMeasurement(e)
|
||||
})
|
||||
.ToList();
|
||||
|
||||
_currentDeviceSensors = powerSensors;
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Discovered {Count} power sensors (excluded main meter: {MainMeter}, exclusion patterns: {PatternCount})",
|
||||
powerSensors.Count, _options.MainMeterEntityId, _exclusionPatterns.Length);
|
||||
"Discovered {Count} power sensors (excluded main meter: {MainMeter}, exclusion patterns: {PatternCount})",
|
||||
powerSensors.Count, _options.MainMeterEntityId, _exclusionPatterns.Length);
|
||||
}
|
||||
|
||||
return powerSensors;
|
||||
@@ -64,10 +68,17 @@ public sealed class PowerSensorDiscoveryService : IPowerSensorDiscoveryService
|
||||
|
||||
internal static bool IsPowerSensor(Entity entity)
|
||||
{
|
||||
if (!entity.EntityId.StartsWith("sensor.", StringComparison.OrdinalIgnoreCase)) return false;
|
||||
if (!entity.EntityId.StartsWith("sensor.", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var attributes = entity.EntityState?.AttributesJson;
|
||||
if (attributes is null) return false;
|
||||
|
||||
if (attributes is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var deviceClass = GetJsonStringProperty(attributes.Value, "device_class");
|
||||
var unit = GetJsonStringProperty(attributes.Value, "unit_of_measurement");
|
||||
@@ -90,38 +101,44 @@ public sealed class PowerSensorDiscoveryService : IPowerSensorDiscoveryService
|
||||
internal static string ExtractDeviceName(string entityId)
|
||||
{
|
||||
var withoutDomain = entityId.Contains('.')
|
||||
? entityId[(entityId.IndexOf('.') + 1)..]
|
||||
: entityId;
|
||||
? entityId[(entityId.IndexOf('.') + 1)..]
|
||||
: entityId;
|
||||
|
||||
return withoutDomain.EndsWith("_power", StringComparison.OrdinalIgnoreCase)
|
||||
? withoutDomain[..^"_power".Length]
|
||||
: withoutDomain;
|
||||
? withoutDomain[..^"_power".Length]
|
||||
: withoutDomain;
|
||||
}
|
||||
|
||||
private static string GetFriendlyName(Entity entity)
|
||||
{
|
||||
var name = GetJsonStringProperty(entity.EntityState?.AttributesJson, "friendly_name");
|
||||
|
||||
return name ?? entity.EntityId;
|
||||
}
|
||||
|
||||
private static string GetUnitOfMeasurement(Entity entity)
|
||||
{
|
||||
var unit = GetJsonStringProperty(entity.EntityState?.AttributesJson, "unit_of_measurement");
|
||||
|
||||
return unit ?? PowerMonitorConsts.WattUnit;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
internal static Regex GlobToRegex(string globPattern)
|
||||
{
|
||||
var regexPattern = "^" + Regex.Escape(globPattern).Replace("\\*", ".*") + "$";
|
||||
|
||||
return new Regex(regexPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user