From a9a9453811edcc3724a82dad62cc2d50fec23338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Tue, 1 Feb 2022 21:50:37 +0100 Subject: [PATCH 1/3] fixed snake case naming of app input boolean --- .../Internal/AppStateManagerTests.cs | 16 +++++++++++++++- .../Internal/AppStateManager.cs | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Runtime/NetDaemon.Runtime.Tests/Internal/AppStateManagerTests.cs b/src/Runtime/NetDaemon.Runtime.Tests/Internal/AppStateManagerTests.cs index db49431f9..9c0b93a5d 100644 --- a/src/Runtime/NetDaemon.Runtime.Tests/Internal/AppStateManagerTests.cs +++ b/src/Runtime/NetDaemon.Runtime.Tests/Internal/AppStateManagerTests.cs @@ -377,4 +377,18 @@ public void TestAppOneStateIsNullShouldNotCallSetStateAsync() // ASSERT appMock.Verify(n => n.SetStateAsync(ApplicationState.Disabled), Times.Never); } -} \ No newline at end of file + + [Theory] + [InlineData("lowercase", "lowercase")] + [InlineData("lower.namespace.lowercase", "lower_namespace_lowercase")] + [InlineData("Namespace.Class", "namespace_class")] + [InlineData("Namespace.ClassNameWithUpperAndLower", "namespace_class_name_with_upper_and_lower")] + [InlineData("ALLUPPERCASE", "alluppercase")] + [InlineData("DIClass", "diclass")] + [InlineData("DiClass", "di_class")] + public void TestToSafeHomeAssistantEntityIdFromApplicationIdShouldGiveCorrectName(string fromId, string toId) + { + var expected = $"input_boolean.netdaemon_{toId}"; + AppStateManager.ToSafeHomeAssistantEntityIdFromApplicationId(fromId).Should().Be(expected); + } +} diff --git a/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs b/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs index 4d0739e23..cf16cf002 100644 --- a/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs +++ b/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs @@ -104,21 +104,34 @@ public static string ToSafeHomeAssistantEntityIdFromApplicationId(string applica var normalizedString = applicationId.Normalize(NormalizationForm.FormD); StringBuilder stringBuilder = new(applicationId.Length); + char lastChar = '\0'; + foreach (var c in normalizedString) + { switch (CharUnicodeInfo.GetUnicodeCategory(c)) { case UnicodeCategory.LowercaseLetter: + stringBuilder.Append(c); + break; case UnicodeCategory.UppercaseLetter: + if (CharUnicodeInfo.GetUnicodeCategory(lastChar) == UnicodeCategory.LowercaseLetter) + { + stringBuilder.Append('_'); + } + stringBuilder.Append(char.ToLowerInvariant(c)); + break; case UnicodeCategory.DecimalDigitNumber: stringBuilder.Append(c); break; - case UnicodeCategory.SpaceSeparator: case UnicodeCategory.ConnectorPunctuation: case UnicodeCategory.DashPunctuation: + case UnicodeCategory.OtherPunctuation: stringBuilder.Append('_'); break; } + lastChar = c; + } return $"input_boolean.netdaemon_{stringBuilder.ToString().ToLowerInvariant()}"; } From 03599b634d4a77f8fa8fd32c423cc8f15bb3d0cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Tue, 1 Feb 2022 21:55:54 +0100 Subject: [PATCH 2/3] fix --- src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs b/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs index cf16cf002..8317ece08 100644 --- a/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs +++ b/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs @@ -143,8 +143,8 @@ private void ClearExistingCacheOnNewConnection() private async Task GetOrCreateStateForApp(string entityId) { - var haConnection = _provider.GetRequiredService() ?? - throw new InvalidOperationException(); + var haConnection = _provider.GetRequiredService(); + try { var state = await haConnection.GetEntityStateAsync(entityId, _cancelTokenSource.Token) From cbe81008e7f3c6d815b0abe55315e350ba264663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Wed, 2 Feb 2022 18:19:15 +0100 Subject: [PATCH 3/3] Make sure we do not have double underscore --- .../Internal/AppStateManagerTests.cs | 3 +++ src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs | 8 +++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Runtime/NetDaemon.Runtime.Tests/Internal/AppStateManagerTests.cs b/src/Runtime/NetDaemon.Runtime.Tests/Internal/AppStateManagerTests.cs index 9c0b93a5d..0c08c5883 100644 --- a/src/Runtime/NetDaemon.Runtime.Tests/Internal/AppStateManagerTests.cs +++ b/src/Runtime/NetDaemon.Runtime.Tests/Internal/AppStateManagerTests.cs @@ -386,6 +386,9 @@ public void TestAppOneStateIsNullShouldNotCallSetStateAsync() [InlineData("ALLUPPERCASE", "alluppercase")] [InlineData("DIClass", "diclass")] [InlineData("DiClass", "di_class")] + [InlineData("Di_Class", "di_class")] + [InlineData("di_class", "di_class")] + [InlineData("di__class", "di_class")] public void TestToSafeHomeAssistantEntityIdFromApplicationIdShouldGiveCorrectName(string fromId, string toId) { var expected = $"input_boolean.netdaemon_{toId}"; diff --git a/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs b/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs index 8317ece08..ffcc0cee0 100644 --- a/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs +++ b/src/Runtime/NetDaemon.Runtime/Internal/AppStateManager.cs @@ -116,7 +116,8 @@ public static string ToSafeHomeAssistantEntityIdFromApplicationId(string applica case UnicodeCategory.UppercaseLetter: if (CharUnicodeInfo.GetUnicodeCategory(lastChar) == UnicodeCategory.LowercaseLetter) { - stringBuilder.Append('_'); + if (lastChar != '_') + stringBuilder.Append('_'); } stringBuilder.Append(char.ToLowerInvariant(c)); break; @@ -127,7 +128,8 @@ public static string ToSafeHomeAssistantEntityIdFromApplicationId(string applica case UnicodeCategory.ConnectorPunctuation: case UnicodeCategory.DashPunctuation: case UnicodeCategory.OtherPunctuation: - stringBuilder.Append('_'); + if (lastChar != '_') + stringBuilder.Append('_'); break; } lastChar = c; @@ -144,7 +146,7 @@ private void ClearExistingCacheOnNewConnection() private async Task GetOrCreateStateForApp(string entityId) { var haConnection = _provider.GetRequiredService(); - + try { var state = await haConnection.GetEntityStateAsync(entityId, _cancelTokenSource.Token)