Skip to content

Commit

Permalink
Dependency upgrade and MQTT breaking fix (#869)
Browse files Browse the repository at this point in the history
* Update dependecies and fix MQTT breaking changes

* fixed nullable possible return
  • Loading branch information
helto4real authored May 9, 2023
1 parent 75eb52b commit ea4237e
Show file tree
Hide file tree
Showing 21 changed files with 37 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/AppModel/NetDaemon.AppModel/NetDaemon.AppModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="7.0.0" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="7.0.0" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task TopicAndPayloadAreSet()
await mqttSetup.MessageSender.SendMessageAsync("topic", "payload", true, MqttQualityOfServiceLevel.AtMostOnce);
var publishedMessage = mqttSetup.LastPublishedMessage;

var payloadAsText = System.Text.Encoding.Default.GetString(publishedMessage.Payload);
var payloadAsText = System.Text.Encoding.Default.GetString(publishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

publishedMessage.Topic.Should().Be("topic");
payloadAsText.Should().Be("payload");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NetDaemon.Extensions.MqttEntityManager;
using NetDaemon.Extensions.MqttEntityManager.Models;
using NetDaemon.HassClient.Tests.ExtensionsTest.MqttEntityManagerTests.TestHelpers;

namespace NetDaemon.HassClient.Tests.ExtensionsTest.MqttEntityManagerTests;
Expand All @@ -23,7 +22,7 @@ public async Task CreateWithNoOptionsSetsBaseConfig()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.CreateAsync("domain.sensor");
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>() );

payload?.Count.Should().Be(6);
payload?["name"].ToString().Should().Be("sensor");
Expand All @@ -41,7 +40,7 @@ public async Task CreateWithDefaultOptionsSetsBaseConfig()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.CreateAsync("domain.sensor", new EntityCreationOptions());
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?.Count.Should().Be(6);
payload?["name"].ToString().Should().Be("sensor");
Expand All @@ -59,7 +58,7 @@ public async Task CreateCanSetUniqueId()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.CreateAsync("domain.sensor", new EntityCreationOptions(UniqueId: "my_id"));
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?["unique_id"].ToString().Should().Be("my_id");
}
Expand All @@ -71,7 +70,7 @@ public async Task CreateSetsObjectId()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.CreateAsync("domain.the_id");
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?["object_id"].ToString().Should().Be("the_id");
}
Expand All @@ -83,7 +82,7 @@ public async Task CreateCanSetDeviceClass()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.CreateAsync("domain.sensor", new EntityCreationOptions(DeviceClass: "classy"));
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?["device_class"].ToString().Should().Be("classy");
}
Expand All @@ -95,7 +94,7 @@ public async Task CreateCanSetName()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.CreateAsync("domain.sensor", new EntityCreationOptions(Name: "george"));
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?["name"].ToString().Should().Be("george");
}
Expand Down Expand Up @@ -131,7 +130,7 @@ public async Task CreateCanSetAdditionalOptions()
var otherOptions = new { sub_class = "lights", up_state = "live" };

await entityManager.CreateAsync("domain.sensor", additionalConfig: otherOptions);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?["sub_class"].ToString().Should().Be("lights");
payload?["up_state"].ToString().Should().Be("live");
Expand All @@ -146,7 +145,7 @@ public async Task CreateCanOverrideBaseConfig()
var otherOptions = new { command_topic = "my/topic" };

await entityManager.CreateAsync("domain.sensor", additionalConfig: otherOptions);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?["command_topic"].ToString().Should().Be("my/topic");
}
Expand All @@ -158,7 +157,7 @@ public async Task CreateAvailabilityTopicOffByDefault()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.CreateAsync("domain.sensor");
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?.ContainsKey("availability_topic").Should().BeFalse();
}
Expand All @@ -170,7 +169,7 @@ public async Task CreateAvailabilityTopicSetForAvailUp()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.CreateAsync("domain.sensor", new EntityCreationOptions(PayloadAvailable: "up"));
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?.ContainsKey("availability_topic").Should().BeTrue();
payload?["availability_topic"].ToString().Should().Be("homeassistant/domain/sensor/availability");
Expand All @@ -184,7 +183,7 @@ public async Task CreateAvailabilityTopicSetForAvailDown()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.CreateAsync("domain.sensor", new EntityCreationOptions(PayloadNotAvailable: "down"));
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

payload?.ContainsKey("availability_topic").Should().BeTrue();
payload?["availability_topic"].ToString().Should().Be("homeassistant/domain/sensor/availability");
Expand All @@ -199,7 +198,7 @@ public async Task CanRemove()

await entityManager.RemoveAsync("domain.sensor");

mqttSetup.LastPublishedMessage.Payload.Should().BeNull();
mqttSetup.LastPublishedMessage.PayloadSegment.Should().BeEmpty();
}

[Fact]
Expand All @@ -209,7 +208,7 @@ public async Task CanSetState()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.SetStateAsync("domain.sensor", "NewState");
var payload = System.Text.Encoding.Default.GetString(mqttSetup.LastPublishedMessage.Payload);
var payload = Encoding.Default.GetString(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

mqttSetup.LastPublishedMessage.Topic.Should().Be("homeassistant/domain/sensor/state");
payload.Should().Be("NewState");
Expand All @@ -224,7 +223,7 @@ public async Task CanSetStateToBlank()
await entityManager.SetStateAsync("domain.sensor", "");

mqttSetup.LastPublishedMessage.Topic.Should().Be("homeassistant/domain/sensor/state");
mqttSetup.LastPublishedMessage.Payload.Should().BeNull();
mqttSetup.LastPublishedMessage.PayloadSegment.Should().BeEmpty();
}

[Fact]
Expand All @@ -235,7 +234,7 @@ public async Task CanSetAttributes()

var attributes = new { colour = "purple", ziggy = "stardust" };
await entityManager.SetAttributesAsync("domain.sensor", attributes);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.Payload);
var payload = PayloadToDictionary(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

mqttSetup.LastPublishedMessage.Topic.Should().Be("homeassistant/domain/sensor/attributes");
payload?["colour"].ToString().Should().Be("purple");
Expand All @@ -249,7 +248,7 @@ public async Task CanSetAvailability()
var entityManager = new MqttEntityManager(mqttSetup.MessageSender, null!, GetOptions());

await entityManager.SetAvailabilityAsync("domain.sensor", "up");
var payload = System.Text.Encoding.Default.GetString(mqttSetup.LastPublishedMessage.Payload);
var payload = Encoding.Default.GetString(mqttSetup.LastPublishedMessage.PayloadSegment.Array ?? Array.Empty<byte>());

mqttSetup.LastPublishedMessage.Topic.Should().Be("homeassistant/domain/sensor/availability");
payload.Should().Be("up");
Expand All @@ -259,7 +258,7 @@ public async Task CanSetAvailability()
private Dictionary<string, object>? PayloadToDictionary(byte[] payload)
{
return JsonSerializer.Deserialize<Dictionary<string, object>>(
System.Text.Encoding.Default.GetString(payload)
Encoding.Default.GetString(payload)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public MockMqttMessageSenderSetup()
// ReSharper disable once MemberCanBePrivate.Global
public void SetupMessageReceiver()
{
var publishResult = new MqttClientPublishResult() { ReasonCode = MqttClientPublishReasonCode.Success };

// Ensure that when the MQTT Client is called its published message is saved
MqttClient.Setup(m => m.EnqueueAsync(It.IsAny<MqttApplicationMessage>()))
.Callback<MqttApplicationMessage>((message) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/Client/NetDaemon.HassClient/NetDaemon.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="7.0.0" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private Task OnMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs msg)
{
try
{
var payload = ByteArrayHelper.SafeToString(msg.ApplicationMessage.Payload);
var payload = ByteArrayHelper.SafeToString(msg.ApplicationMessage.PayloadSegment.Array ?? Array.Empty<byte>());
var topic = msg.ApplicationMessage.Topic;
_logger.LogTrace("Subscription received {Payload} from {Topic}", payload, topic);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="MQTTnet" Version="4.1.4.563" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.1.4.563" />
<PackageReference Include="MQTTnet" Version="4.2.0.706" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.2.0.706" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageReference Include="Cronos" Version="0.7.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/NetDaemon.Runtime/NetDaemon.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion src/debug/DebugHost/DebugHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
Expand Down

0 comments on commit ea4237e

Please sign in to comment.