Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions NetDaemon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetDaemon.AppModel.SourceDe
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DebugMinimalHost", "src\debug\DebugMinimalHost\DebugMinimalHost.csproj", "{24BF4C65-4F48-4349-9649-E152A4E1496E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleClient", "src\debug\ConsoleClient\ConsoleClient.csproj", "{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -355,6 +357,18 @@ Global
{24BF4C65-4F48-4349-9649-E152A4E1496E}.Release|x64.Build.0 = Release|Any CPU
{24BF4C65-4F48-4349-9649-E152A4E1496E}.Release|x86.ActiveCfg = Release|Any CPU
{24BF4C65-4F48-4349-9649-E152A4E1496E}.Release|x86.Build.0 = Release|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Debug|x64.ActiveCfg = Debug|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Debug|x64.Build.0 = Debug|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Debug|x86.ActiveCfg = Debug|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Debug|x86.Build.0 = Debug|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Release|Any CPU.Build.0 = Release|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Release|x64.ActiveCfg = Release|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Release|x64.Build.0 = Release|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Release|x86.ActiveCfg = Release|Any CPU
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -382,6 +396,7 @@ Global
{CF7273C1-A4FE-4598-9C99-D746CE279A32} = {E15D4280-7FFC-4F8B-9B8C-CF9AF2BF838C}
{AE48B790-C3D7-440E-945E-EE5C82AE2A86} = {A62F78F8-2EF5-49C8-B437-E5FC6321E866}
{24BF4C65-4F48-4349-9649-E152A4E1496E} = {E15D4280-7FFC-4F8B-9B8C-CF9AF2BF838C}
{E966FBC9-D7AF-44F1-BE0F-C344B01A5323} = {E15D4280-7FFC-4F8B-9B8C-CF9AF2BF838C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7C5FBB7F-654C-4CAC-964F-6D71AF3D62F8}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public static IServiceCollection AddHomeAssistantClient(this IServiceCollection
.AddSingleton<IHomeAssistantRunner>(s => s.GetRequiredService<HomeAssistantRunner>())
.AddSingleton<HomeAssistantApiManager>()
.AddSingleton<IHomeAssistantApiManager>(s => s.GetRequiredService<HomeAssistantApiManager>())
.AddTransient(s => s.GetRequiredService<IHomeAssistantRunner>().CurrentConnection!)
.AddSingleton<IHomeAssistantConnectionProvider>(s => s.GetRequiredService<HomeAssistantRunner>())
.AddTransient(s => s.GetRequiredService<IHomeAssistantConnectionProvider>().CurrentConnection!)
.AddWebSocketFactory()
.AddPipelineFactory()
.AddConnectionFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public static async Task<IHomeAssistantConnection> ConnectClientAsync(string hos
var loggerFactory = LoggerFactory.Create(b => b.AddConsole());
var loggerConnect = loggerFactory.CreateLogger<IHomeAssistantConnection>();
var loggerClient = loggerFactory.CreateLogger<IHomeAssistantClient>();
var loggerResultMessageHandler = loggerFactory.CreateLogger<ResultMessageHandler>();
var settings = new HomeAssistantSettings
{
Host = host,
Expand All @@ -52,4 +51,20 @@ public static async Task<IHomeAssistantConnection> ConnectClientAsync(string hos

return await client.ConnectAsync(host, port, ssl, token, websocketPath, cancelToken).ConfigureAwait(false);
}

/// <summary>
/// Connect to Home Assistant
/// </summary>
public static async Task<IHomeAssistantConnection> ConnectClientAsync(string websocketUrl, string token, CancellationToken cancelToken)
{
return await ConnectClientAsync(new Uri(websocketUrl), token, cancelToken);
}

/// <summary>
/// Connect to Home Assistant
/// </summary>
public static async Task<IHomeAssistantConnection> ConnectClientAsync(Uri websocketUrl, string token, CancellationToken cancelToken)
{
return await ConnectClientAsync(websocketUrl.Host, websocketUrl.Port, websocketUrl.Scheme is "https" or "wss", token, websocketUrl.PathAndQuery, cancelToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NetDaemon.Client;

/// <summary>
/// Interface to retrieve the current connection to HomeAsstant
/// </summary>
public interface IHomeAssistantConnectionProvider
{
/// <summary>
/// The current connection to Home Assistant. Null if disconnected.
/// </summary>
IHomeAssistantConnection? CurrentConnection { get; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace NetDaemon.Client;

public interface IHomeAssistantRunner : IAsyncDisposable
public interface IHomeAssistantRunner : IHomeAssistantConnectionProvider, IAsyncDisposable
{
/// <summary>
/// Observable that emits when a (new) connection is established.
Expand All @@ -12,11 +12,6 @@ public interface IHomeAssistantRunner : IAsyncDisposable
/// </summary>
IObservable<DisconnectReason> OnDisconnect { get; }

/// <summary>
/// The current connection to Home Assistant. Null if disconnected.
/// </summary>
IHomeAssistantConnection? CurrentConnection { get; }

/// <summary>
/// Maintains a connection to the Home Assistant server
/// </summary>
Expand All @@ -27,7 +22,7 @@ public interface IHomeAssistantRunner : IAsyncDisposable
/// <param name="timeout">Wait time between connects</param>
/// <param name="cancelToken">Cancel token</param>
Task RunAsync(string host, int port, bool ssl, string token, TimeSpan timeout, CancellationToken cancelToken);

/// <summary>
/// Maintains a connection to the Home Assistant server
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ await transportPipeline.SendMessageAsync(

private static Uri GetHomeAssistantWebSocketUri(string host, int port, bool ssl, string websocketPath)
{
return new Uri($"{(ssl ? "wss" : "ws")}://{host}:{port}/{websocketPath}");
return new UriBuilder
{
Host = host,
Port = port,
Scheme = ssl ? "wss" : "ws",
Path = websocketPath,
Query = string.Empty,
}.Uri;
}

private static async Task<bool> CheckIfRunning(IHomeAssistantConnection connection, CancellationToken cancelToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ private static void AddMockServices(ServiceCollection serviceCollection)

var hassConnectionMock = new Mock<IHomeAssistantConnection>();
serviceCollection.AddSingleton(hassConnectionMock.Object);
var haRunnerMock = new Mock<IHomeAssistantRunner>();
haRunnerMock.SetupGet(n => n.CurrentConnection).Returns(hassConnectionMock.Object);
var connectionProviderMock = new Mock<IHomeAssistantConnectionProvider>();
connectionProviderMock.SetupGet(n => n.CurrentConnection).Returns(hassConnectionMock.Object);

serviceCollection.AddSingleton(_ => haRunnerMock.Object);
serviceCollection.AddSingleton(_ => connectionProviderMock.Object);
var apiManagerMock = new Mock<IHomeAssistantApiManager>();

serviceCollection.AddSingleton(_ => apiManagerMock.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,10 @@ private async Task<ServiceProvider> CreateServiceProvider()
);
serviceCollection.AddSingleton<IObservable<HassEvent>>(_hassEventSubjectMock);

var haRunnerMock = new Mock<IHomeAssistantRunner>();
var connectionProviderMock = new Mock<IHomeAssistantConnectionProvider>();

haRunnerMock.SetupGet(n => n.CurrentConnection).Returns(_hassConnectionMock.Object);
serviceCollection.AddSingleton(_ => haRunnerMock.Object);
connectionProviderMock.SetupGet(n => n.CurrentConnection).Returns(_hassConnectionMock.Object);
serviceCollection.AddSingleton(_ => connectionProviderMock.Object);

var apiManagerMock = new Mock<IHomeAssistantApiManager>();

Expand All @@ -379,7 +379,7 @@ private async Task<ServiceProvider> CreateServiceProvider()

var provider = serviceCollection.BuildServiceProvider();

await provider.GetRequiredService<ICacheManager>().InitializeAsync(CancellationToken.None);
await provider.GetRequiredService<ICacheManager>().InitializeAsync(_hassConnectionMock.Object, CancellationToken.None);

return provider;
}
Expand Down
Loading
Loading