@@ -5,20 +5,19 @@ import (
55 "errors"
66 "fmt"
77 "log/slog"
8+ "net/url"
89 "time"
910
1011 "github.com/golang-module/carbon"
1112 "github.com/gorilla/websocket"
1213 sunriseLib "github.com/nathan-osman/go-sunrise"
14+
1315 "saml.dev/gome-assistant/internal"
1416 "saml.dev/gome-assistant/internal/http"
1517 pq "saml.dev/gome-assistant/internal/priorityqueue"
1618 ws "saml.dev/gome-assistant/internal/websocket"
1719)
1820
19- // Returned by NewApp() if authentication fails
20- var ErrInvalidToken = ws .ErrInvalidToken
21-
2221var ErrInvalidArgs = errors .New ("invalid arguments provided" )
2322
2423type App struct {
@@ -41,15 +40,11 @@ type App struct {
4140 eventListeners map [string ][]* EventListener
4241}
4342
44- /*
45- DurationString represents a duration, such as "2s" or "24h".
46- See https://pkg.go.dev/time#ParseDuration for all valid time units.
47- */
43+ // DurationString represents a duration, such as "2s" or "24h".
44+ // See https://pkg.go.dev/time#ParseDuration for all valid time units.
4845type DurationString string
4946
50- /*
51- TimeString is a 24-hr format time "HH:MM" such as "07:30".
52- */
47+ // TimeString is a 24-hr format time "HH:MM" such as "07:30".
5348type TimeString string
5449
5550type timeRange struct {
@@ -59,11 +54,16 @@ type timeRange struct {
5954
6055type NewAppRequest struct {
6156 // Required
57+ URL string
58+
59+ // Optional
60+ // Deprecated: use URL instead
6261 // IpAddress of your Home Assistant instance i.e. "localhost"
6362 // or "192.168.86.59" etc.
6463 IpAddress string
6564
6665 // Optional
66+ // Deprecated: use URL instead
6767 // Port number Home Assistant is running on. Defaults to 8123.
6868 Port string
6969
@@ -90,39 +90,37 @@ NewApp establishes the websocket connection and returns an object
9090you can use to register schedules and listeners.
9191*/
9292func NewApp (request NewAppRequest ) (* App , error ) {
93- if request .IpAddress == "" || request .HAAuthToken == "" || request .HomeZoneEntityId == "" {
94- slog .Error ("IpAddress , HAAuthToken, and HomeZoneEntityId are all required arguments in NewAppRequest" )
93+ if ( request .URL == "" && request . IpAddress == "" ) || request .HAAuthToken == "" || request .HomeZoneEntityId == "" {
94+ slog .Error ("URL , HAAuthToken, and HomeZoneEntityId are all required arguments in NewAppRequest" )
9595 return nil , ErrInvalidArgs
9696 }
97- port := request .Port
98- if port == "" {
99- port = "8123"
100- }
10197
102- var (
103- conn * websocket.Conn
104- ctx context.Context
105- ctxCancel context.CancelFunc
106- err error
107- )
98+ baseURL := & url.URL {}
10899
109- if request .Secure {
110- conn , ctx , ctxCancel , err = ws .SetupSecureConnection (request .IpAddress , port , request .HAAuthToken )
100+ if request .URL != "" {
101+ var err error
102+ baseURL , err = url .Parse (request .URL )
103+ if err != nil {
104+ return nil , ErrInvalidArgs
105+ }
111106 } else {
112- conn , ctx , ctxCancel , err = ws .SetupConnection (request .IpAddress , port , request .HAAuthToken )
107+ // This is deprecated and will be removed in a future release
108+ port := request .Port
109+ if port == "" {
110+ port = "8123"
111+ }
112+ baseURL .Host = request .IpAddress + ":" + port
113113 }
114114
115+ conn , ctx , ctxCancel , err := ws .ConnectionFromUri (baseURL , request .HAAuthToken )
116+ if err != nil {
117+ return nil , err
118+ }
115119 if conn == nil {
116120 return nil , err
117121 }
118122
119- var httpClient * http.HttpClient
120-
121- if request .Secure {
122- httpClient = http .NewHttpsClient (request .IpAddress , port , request .HAAuthToken )
123- } else {
124- httpClient = http .NewHttpClient (request .IpAddress , port , request .HAAuthToken )
125- }
123+ httpClient := http .NewHttpClient (baseURL , request .HAAuthToken )
126124
127125 wsWriter := & ws.WebsocketWriter {Conn : conn }
128126 service := newService (wsWriter , ctx , httpClient )
0 commit comments