Skip to content

Commit 3a031c7

Browse files
committed
Simplified
1 parent 2769ab3 commit 3a031c7

File tree

5 files changed

+59
-71
lines changed

5 files changed

+59
-71
lines changed

src/Client/NetDaemon.HassClient/Common/HomeAssistantClientConnector.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,21 @@ public static async Task<IHomeAssistantConnection> ConnectClientAsync(string hos
5151

5252
return await client.ConnectAsync(host, port, ssl, token, websocketPath, cancelToken).ConfigureAwait(false);
5353
}
54+
55+
/// <summary>
56+
/// Connect to Home Assistant
57+
/// </summary>
58+
public static async Task<IHomeAssistantConnection> ConnectClientAsync(string websocketUrl, string token, CancellationToken cancelToken)
59+
{
60+
return await ConnectClientAsync(new Uri(websocketUrl), token, cancelToken);
61+
}
62+
63+
64+
/// <summary>
65+
/// Connect to Home Assistant
66+
/// </summary>
67+
public static async Task<IHomeAssistantConnection> ConnectClientAsync(Uri websocketUrl, string token, CancellationToken cancelToken)
68+
{
69+
return await ConnectClientAsync(websocketUrl.Host, websocketUrl.Port, websocketUrl.Scheme is "https" or "wss", token, websocketUrl.PathAndQuery, cancelToken);
70+
}
5471
}

src/Client/NetDaemon.HassClient/Internal/HomeAssistantClient.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ await transportPipeline.SendMessageAsync(
7777

7878
private static Uri GetHomeAssistantWebSocketUri(string host, int port, bool ssl, string websocketPath)
7979
{
80-
return new Uri($"{(ssl ? "wss" : "ws")}://{host}:{port}/{websocketPath}");
80+
return new UriBuilder
81+
{
82+
Host = host,
83+
Port = port,
84+
Scheme = ssl ? "wss" : "ws",
85+
Path = websocketPath,
86+
Query = string.Empty,
87+
}.Uri;
8188
}
8289

8390
private static async Task<bool> CheckIfRunning(IHomeAssistantConnection connection, CancellationToken cancelToken)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace NetDaemon.HassModel;
2+
3+
/// <summary>
4+
/// Factory class to create an instance of <see cref="IHaContext"/> for use in console applications or scripts.
5+
/// </summary>
6+
public static class HaContextFactory
7+
{
8+
/// <summary>
9+
/// Creates a new instance of <see cref="IHaContext"/> using the provided WebSocket URL and token.
10+
/// </summary>
11+
/// <remarks>Do not use this in NetDaemon apps. Apps should use UseNetDaemonRuntime and resolve IHaContext via dependency injection</remarks>
12+
/// <param name="homeAssistantWebsocketUrl">The Websocket Url to HomeAssistant, eg: ws://localhost:8123/api/websocket</param>
13+
/// <param name="token">The long lived accestoken for Home Assitant</param>
14+
/// <returns>An instance of IHaContext</returns>
15+
public static async Task<IHaContext> CreateAsync(string homeAssistantWebsocketUrl, string token)
16+
{
17+
var connection = await HomeAssistantClientConnector.ConnectClientAsync(homeAssistantWebsocketUrl, token, CancellationToken.None);
18+
19+
var collection = new ServiceCollection();
20+
collection.AddLogging(builder => builder.AddConsole());
21+
collection.AddSingleton(connection);
22+
collection.AddScopedHaContext();
23+
var serviceProvider = collection.BuildServiceProvider().CreateScope().ServiceProvider;
24+
25+
var cacheManager = serviceProvider.GetRequiredService<ICacheManager>();
26+
await cacheManager.InitializeAsync(connection, CancellationToken.None);
27+
28+
return serviceProvider.GetRequiredService<IHaContext>();
29+
}
30+
}

src/debug/ConsoleClient/HaContextFactory.cs

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/debug/ConsoleClient/Program.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
var haContext = await HaContextFactory.CreateHaContextAsync();
1+
//#:package [email protected]
22

3-
Console.WriteLine(haContext.Entity("sun.sun").State);
3+
var ha = await NetDaemon.HassModel.HaContextFactory.CreateAsync("ws://localhost:8123/api/websocket", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI4MDhlZjQ3NWRlOTU0YWJmYTYwNTRkZDc2YzRkZmJjNiIsImlhdCI6MTc0NjkxMTkxOSwiZXhwIjoyMDYyMjcxOTE5fQ.KSwYw1IER965EUcN2_7XPPgVikeIli-mTH8XreveWvA");
44

5-
haContext.Entity("input_button.test_button").StateAllChanges().Subscribe(s => Console.WriteLine($"Pressed {s.New?.State}"));
6-
7-
await new StreamReader(Console.OpenStandardInput()).ReadLineAsync();
5+
ha.Entity("input_boolean.dummy_switch").CallService("toggle");
6+
Console.WriteLine("done");

0 commit comments

Comments
 (0)