-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#12 - Added ability to override ID and Name generation for Outlets, a…
…nd measurements. Added ability to also completely disable certain Outlets and Measurements.
- Loading branch information
1 parent
aa502ff
commit 52b4f6b
Showing
10 changed files
with
206 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using rPDU2MQTT.Models.Converters; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace rPDU2MQTT.Models.Config; | ||
|
||
/// <summary> | ||
/// This defines the schema for overrides for a specific section. | ||
/// </summary> | ||
/// <typeparam name="string">This is the type of key used.</typeparam> | ||
public class TypeOverride | ||
{ | ||
[JsonConverter(typeof(CaseInsensitiveDictionaryConverter<string>))] | ||
[JsonPropertyName("ID")] | ||
/// <summary> | ||
/// Allows overriding the generated EntityName. | ||
/// </summary> | ||
/// <remarks> | ||
/// This maps to <see cref="Models.HomeAssistant.baseClasses.baseEntity.Name"/>, ie, "object_id" | ||
/// </remarks> | ||
public Dictionary<string, string> ID { get; set; } = new(); | ||
|
||
[JsonConverter(typeof(CaseInsensitiveDictionaryConverter<string>))] | ||
[JsonPropertyName("Name")] | ||
/// <summary> | ||
/// Allows overriding the Name / Display Name. | ||
/// </summary> | ||
/// <remarks> | ||
/// This maps to <see cref="Models.HomeAssistant.baseClasses.baseEntity.DisplayName"/>, ie, "name" | ||
/// </remarks> | ||
public Dictionary<string, string> Name { get; set; } = new(); | ||
|
||
[JsonConverter(typeof(CaseInsensitiveDictionaryConverter<bool>))] | ||
[JsonPropertyName("Enabled")] | ||
/// <summary> | ||
/// Allows enabling, or disabling specific entities. | ||
/// </summary> | ||
public Dictionary<string, bool> Enabled { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// Remove any entites which are marked as disabled. | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
/// <param name="Entities"></param> | ||
public void RemoveDisabledRecords<TEntity>(Dictionary<string, TEntity> Entities, Func<TEntity, string>? KeyFunc = null) | ||
{ | ||
//Remove any disabled entities. | ||
foreach (var item in Entities.ToList()) | ||
{ | ||
string Key = KeyFunc is null ? item.Key : KeyFunc.Invoke(item.Value); | ||
if (Enabled.TryGetValue(Key, out bool IsEnabled) && IsEnabled == false) | ||
{ | ||
Entities.Remove(item.Key); | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
rPDU2MQTT/Models/Converters/CaseInsensitiveDictionaryConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace rPDU2MQTT.Models.Converters; | ||
|
||
public sealed class CaseInsensitiveDictionaryConverter<TValue> : JsonConverter<Dictionary<string, TValue>> | ||
{ | ||
public override Dictionary<string, TValue> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var newDictionary = (Dictionary<string, TValue>)JsonSerializer.Deserialize(ref reader, typeToConvert, options); | ||
return new Dictionary<string, TValue>(newDictionary, StringComparer.OrdinalIgnoreCase); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, Dictionary<string, TValue> value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value, value.GetType(), options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.