feat: support secure connections

This commit is contained in:
Dominik Siebel
2024-02-22 11:28:05 +01:00
parent a9aad16cf3
commit 8b436ce4ee
3 changed files with 56 additions and 6 deletions

28
app.go
View File

@@ -77,6 +77,12 @@ type NewAppRequest struct {
// Used to pull latitude/longitude from Home Assistant
// to calculate sunset/sunrise times.
HomeZoneEntityId string
// Optional
// Whether to use secure connections for http and websockets.
// Setting this to `true` will use `https://` instead of `https://`
// and `wss://` instead of `ws://`.
Secure bool
}
/*
@@ -92,13 +98,31 @@ func NewApp(request NewAppRequest) (*App, error) {
if port == "" {
port = "8123"
}
conn, ctx, ctxCancel, err := ws.SetupConnection(request.IpAddress, port, request.HAAuthToken)
var (
conn *websocket.Conn
ctx context.Context
ctxCancel context.CancelFunc
err error
)
if request.Secure {
conn, ctx, ctxCancel, err = ws.SetupSecureConnection(request.IpAddress, port, request.HAAuthToken)
} else {
conn, ctx, ctxCancel, err = ws.SetupConnection(request.IpAddress, port, request.HAAuthToken)
}
if conn == nil {
return nil, err
}
httpClient := http.NewHttpClient(request.IpAddress, port, request.HAAuthToken)
var httpClient *http.HttpClient
if request.Secure {
httpClient = http.NewHttpsClient(request.IpAddress, port, request.HAAuthToken)
} else {
httpClient = http.NewHttpClient(request.IpAddress, port, request.HAAuthToken)
}
wsWriter := &ws.WebsocketWriter{Conn: conn}
service := newService(wsWriter, ctx, httpClient)