Skip to content

Commit eb26a44

Browse files
committed
two things:
fix bug with newer HA versions by sending int id instead of string add warning to logs when receiving unsuccessful websocket response
1 parent d16f646 commit eb26a44

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

internal/services/services.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package services
22

33
import (
44
"context"
5-
"fmt"
65

76
"saml.dev/gome-assistant/internal"
87
ws "saml.dev/gome-assistant/internal/websocket"
@@ -32,7 +31,7 @@ func BuildService[
3231
}
3332

3433
type BaseServiceRequest struct {
35-
Id string `json:"id"`
34+
Id int64 `json:"id"`
3635
RequestType string `json:"type"` // hardcoded "call_service"
3736
Domain string `json:"domain"`
3837
Service string `json:"service"`
@@ -45,7 +44,7 @@ type BaseServiceRequest struct {
4544
func NewBaseServiceRequest(entityId string) BaseServiceRequest {
4645
id := internal.GetId()
4746
bsr := BaseServiceRequest{
48-
Id: fmt.Sprint(id),
47+
Id: id,
4948
RequestType: "call_service",
5049
}
5150
if entityId != "" {

internal/websocket/reader.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ package websocket
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"log"
78

89
"github.com/gorilla/websocket"
910
)
1011

1112
type BaseMessage struct {
12-
Type string `json:"type"`
13-
Id int64 `json:"id"`
13+
Type string `json:"type"`
14+
Id int64 `json:"id"`
15+
Success bool `json:"success"`
1416
}
1517

1618
type ChanMsg struct {
17-
Id int64
18-
Type string
19-
Raw []byte
19+
Id int64
20+
Type string
21+
Success bool
22+
Raw []byte
2023
}
2124

2225
func ListenWebsocket(conn *websocket.Conn, ctx context.Context, c chan ChanMsg) {
@@ -29,12 +32,19 @@ func ListenWebsocket(conn *websocket.Conn, ctx context.Context, c chan ChanMsg)
2932
break
3033
}
3134

32-
base := BaseMessage{}
35+
base := BaseMessage{
36+
// default to true for messages that don't include "success" at all
37+
Success: true,
38+
}
3339
json.Unmarshal(bytes, &base)
40+
if !base.Success {
41+
fmt.Println("WARNING: received unsuccessful response:", string(bytes))
42+
}
3443
chanMsg := ChanMsg{
35-
Type: base.Type,
36-
Id: base.Id,
37-
Raw: bytes,
44+
Type: base.Type,
45+
Id: base.Id,
46+
Success: base.Success,
47+
Raw: bytes,
3848
}
3949

4050
c <- chanMsg

0 commit comments

Comments
 (0)