-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional MQTT options & support for external brokers/TLS (#1005)
* Additional MQTT options & support for external brokers/TLS * Run fix * Remove comment * Fewer options, some with defaults * Test fixes * Re-add DiscoveryPrefix
- Loading branch information
Showing
12 changed files
with
309 additions
and
51 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
146 changes: 146 additions & 0 deletions
146
...n.HassClient.Tests/ExtensionsTest/MqttEntityManagerTests/MqttClientOptionsFactoryTests.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,146 @@ | ||
using MQTTnet.Client; | ||
using NetDaemon.Extensions.MqttEntityManager; | ||
|
||
namespace NetDaemon.HassClient.Tests.ExtensionsTest.MqttEntityManagerTests; | ||
|
||
public class MqttClientOptionsFactoryTests | ||
{ | ||
private MqttClientOptionsFactory MqttClientOptionsFactory { get; } = new(); | ||
|
||
[Fact] | ||
public void CreatesDefaultConfiguration() | ||
{ | ||
// This is the bare minimum necessary to establish a connection to an MQTT broker that doesn't use TLS | ||
// or require authentication. The default port is 1883 and a TCP connection is used. | ||
var mqttConfiguration = new MqttConfiguration | ||
{ | ||
Host = "broker", | ||
}; | ||
|
||
var mqttClientOptions = MqttClientOptionsFactory.CreateClientOptions(mqttConfiguration); | ||
|
||
mqttClientOptions.Should().NotBeNull(); | ||
|
||
mqttClientOptions.ClientOptions.ChannelOptions.Should().NotBeNull(); | ||
mqttClientOptions.ClientOptions.ChannelOptions.Should().BeOfType<MqttClientTcpOptions>(); | ||
|
||
var mqttClientChannelOptions = (MqttClientTcpOptions)mqttClientOptions.ClientOptions.ChannelOptions; | ||
mqttClientChannelOptions.Server.Should().Be("broker"); | ||
mqttClientChannelOptions.Port.Should().Be(1883); | ||
|
||
mqttClientOptions.ClientOptions.Credentials.Should().BeNull(); | ||
|
||
mqttClientOptions.ClientOptions.ChannelOptions.TlsOptions.UseTls.Should().BeFalse(); | ||
mqttClientOptions.ClientOptions.ChannelOptions.TlsOptions.AllowUntrustedCertificates.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void CreatesDefaultConfigurationWithTls() | ||
{ | ||
var mqttConfiguration = new MqttConfiguration | ||
{ | ||
Host = "broker", | ||
UseTls = true | ||
}; | ||
|
||
var mqttClientOptions = MqttClientOptionsFactory.CreateClientOptions(mqttConfiguration); | ||
|
||
mqttClientOptions.Should().NotBeNull(); | ||
|
||
mqttClientOptions.ClientOptions.ChannelOptions.Should().NotBeNull(); | ||
mqttClientOptions.ClientOptions.ChannelOptions.Should().BeOfType<MqttClientTcpOptions>(); | ||
|
||
var mqttClientChannelOptions = (MqttClientTcpOptions)mqttClientOptions.ClientOptions.ChannelOptions; | ||
mqttClientChannelOptions.Server.Should().Be("broker"); | ||
mqttClientChannelOptions.Port.Should().Be(1883); | ||
|
||
mqttClientOptions.ClientOptions.Credentials.Should().BeNull(); | ||
|
||
mqttClientOptions.ClientOptions.ChannelOptions.TlsOptions.UseTls.Should().BeTrue(); | ||
|
||
// This would only get set to true if it and UseTls are both true | ||
mqttClientOptions.ClientOptions.ChannelOptions.TlsOptions.AllowUntrustedCertificates.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void IgnoresTlsCustomizationIfTlsIsntEnabled() | ||
{ | ||
var mqttConfiguration = new MqttConfiguration | ||
{ | ||
Host = "broker", | ||
UseTls = false, | ||
AllowUntrustedCertificates = true | ||
}; | ||
|
||
var mqttClientOptions = MqttClientOptionsFactory.CreateClientOptions(mqttConfiguration); | ||
|
||
mqttClientOptions.Should().NotBeNull(); | ||
|
||
mqttClientOptions.ClientOptions.ChannelOptions.Should().NotBeNull(); | ||
mqttClientOptions.ClientOptions.ChannelOptions.Should().BeOfType<MqttClientTcpOptions>(); | ||
|
||
var mqttClientChannelOptions = (MqttClientTcpOptions)mqttClientOptions.ClientOptions.ChannelOptions; | ||
mqttClientChannelOptions.Server.Should().Be("broker"); | ||
mqttClientChannelOptions.Port.Should().Be(1883); | ||
|
||
mqttClientOptions.ClientOptions.Credentials.Should().BeNull(); | ||
|
||
mqttClientOptions.ClientOptions.ChannelOptions.TlsOptions.UseTls.Should().BeFalse(); | ||
|
||
// This would only get set to true if it and UseTls are both true | ||
mqttClientOptions.ClientOptions.ChannelOptions.TlsOptions.AllowUntrustedCertificates.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void CreatesFullyCustomizedConfiguration() | ||
{ | ||
var mqttConfiguration = new MqttConfiguration | ||
{ | ||
Host = "broker", | ||
Port = 1234, | ||
UserName = "testuser", | ||
Password = "testpassword", | ||
UseTls = true, | ||
AllowUntrustedCertificates = true | ||
}; | ||
|
||
var mqttClientOptions = MqttClientOptionsFactory.CreateClientOptions(mqttConfiguration); | ||
|
||
mqttClientOptions.Should().NotBeNull(); | ||
|
||
mqttClientOptions.ClientOptions.ChannelOptions.Should().NotBeNull(); | ||
mqttClientOptions.ClientOptions.ChannelOptions.Should().BeOfType<MqttClientTcpOptions>(); | ||
|
||
var mqttClientChannelOptions = (MqttClientTcpOptions)mqttClientOptions.ClientOptions.ChannelOptions; | ||
mqttClientChannelOptions.Server.Should().Be("broker"); | ||
mqttClientChannelOptions.Port.Should().Be(1234); | ||
|
||
mqttClientOptions.ClientOptions.Credentials.Should().NotBeNull(); | ||
mqttClientOptions.ClientOptions.Credentials.Should().BeOfType<MqttClientCredentials>(); | ||
|
||
mqttClientOptions.ClientOptions.Credentials.GetUserName(mqttClientOptions.ClientOptions).Should().Be("testuser"); | ||
mqttClientOptions.ClientOptions.Credentials.GetPassword(mqttClientOptions.ClientOptions).Should().BeEquivalentTo(Encoding.UTF8.GetBytes("testpassword")); | ||
|
||
mqttClientOptions.ClientOptions.ChannelOptions.TlsOptions.UseTls.Should().BeTrue(); | ||
mqttClientOptions.ClientOptions.ChannelOptions.TlsOptions.AllowUntrustedCertificates.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
void ThrowsArgumentNullExceptionIfMqttConfigIsNull() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => MqttClientOptionsFactory.CreateClientOptions(null!)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
void ThrowsArgumentExceptionIfMqttConfigHasNullOrEmptyHost(string? host) | ||
{ | ||
var mqttConfiguration = new MqttConfiguration | ||
{ | ||
Host = host!, | ||
}; | ||
|
||
Assert.Throws<ArgumentException>(() => MqttClientOptionsFactory.CreateClientOptions(mqttConfiguration)); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/Extensions/NetDaemon.Extensions.MqttEntityManager/IMqttClientOptionsFactory.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,16 @@ | ||
using MQTTnet.Extensions.ManagedClient; | ||
|
||
namespace NetDaemon.Extensions.MqttEntityManager; | ||
|
||
/// <summary> | ||
/// Represents a factory for creating MQTT client options. | ||
/// </summary> | ||
public interface IMqttClientOptionsFactory | ||
{ | ||
/// <summary> | ||
/// Creates the client options for MQTT connection from the supplied configuration. | ||
/// /// </summary> | ||
/// <param name="mqttConfig">The MQTT configuration.</param> | ||
/// <returns>The managed MQTT client options.</returns> | ||
ManagedMqttClientOptions CreateClientOptions(MqttConfiguration mqttConfig); | ||
} |
Oops, something went wrong.