Skip to content

Commit

Permalink
Fix beta HA versions (#888)
Browse files Browse the repository at this point in the history
* Handle beta versions of ha

* Update HomeAssistantClient.cs

* Update GlobalUsings.cs

* add test and helper
  • Loading branch information
mcinnes01 authored Jun 29, 2023
1 parent e843d37 commit 6038eb1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace NetDaemon.HassClient.Tests.HelperTest;

public class VersionHelperTests
{
[Theory]
[InlineData("2022.8.12", "2022.8.12")]
[InlineData("2022.8.0b7", "2022.8.0")]
[InlineData("2022.9.0", "2022.9.0")]
[InlineData("2022.9.0b1", "2022.9.0")]
public void WithoutBeta_ValidInput_ReturnsExpectedVersion(string input, string expectedOutput)
{
// Arrange
var expectedVersion = new Version(expectedOutput);

// Act
var parsedVersion = VersionHelper.ReplaceBeta(input);

// Assert
Assert.Equal(expectedVersion, parsedVersion);
}
}
3 changes: 2 additions & 1 deletion src/Client/NetDaemon.HassClient/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
global using System.Text;
global using System.Text.Json;
global using System.Text.Json.Serialization;
global using System.Text.RegularExpressions;
global using System.Threading.Tasks;
global using System.Reactive.Subjects;
global using System.Reactive.Linq;
Expand All @@ -33,4 +34,4 @@
[assembly: InternalsVisibleTo("NetDaemon.HassClient.Tests")]
[assembly: InternalsVisibleTo("NetDaemon.HassModel.Tests")]
[assembly: InternalsVisibleTo("NetDaemon.Runtime.Tests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
11 changes: 11 additions & 0 deletions src/Client/NetDaemon.HassClient/Internal/Helpers/VersionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace NetDaemon.Client.Internal.Helpers;

internal static partial class VersionHelper
{
public static Version ReplaceBeta(string version)
=> Version.Parse(BetaVersion().Replace(version, ".0"));


[GeneratedRegex("\\.0b\\d+$")]
private static partial Regex BetaVersion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public async Task<IHomeAssistantConnection> ConnectAsync(string host, int port,

var transportPipeline = _transportPipelineFactory.New(ws);

var hassVersionInfo = await HandleAuthorizationSequenceAndReturnHassVersionInfo(token, transportPipeline, cancelToken).ConfigureAwait(false);
var hassVersionInfo = await HandleAuthorizationSequenceAndReturnHassVersionInfo(token, transportPipeline, cancelToken).ConfigureAwait(false);

if (Version.Parse(hassVersionInfo) >= new Version(2022, 9))
if (VersionHelper.ReplaceBeta(hassVersionInfo) >= new Version(2022, 9))
{
await AddCoalesceSupport(transportPipeline, cancelToken).ConfigureAwait(false);
}
Expand Down Expand Up @@ -140,4 +140,4 @@ await transportPipeline.SendMessageAsync(
throw new ApplicationException($"Unexpected response ({authResultMessage.Single().Type})");
}
}
}
}

0 comments on commit 6038eb1

Please sign in to comment.