two things:

fix bug with newer HA versions by sending int id instead of string
add warning to logs when receiving unsuccessful websocket response
This commit is contained in:
Sam Lewis
2023-06-11 14:09:13 -04:00
parent d16f646423
commit eb26a44459
2 changed files with 21 additions and 12 deletions

View File

@@ -2,7 +2,6 @@ package services
import ( import (
"context" "context"
"fmt"
"saml.dev/gome-assistant/internal" "saml.dev/gome-assistant/internal"
ws "saml.dev/gome-assistant/internal/websocket" ws "saml.dev/gome-assistant/internal/websocket"
@@ -32,7 +31,7 @@ func BuildService[
} }
type BaseServiceRequest struct { type BaseServiceRequest struct {
Id string `json:"id"` Id int64 `json:"id"`
RequestType string `json:"type"` // hardcoded "call_service" RequestType string `json:"type"` // hardcoded "call_service"
Domain string `json:"domain"` Domain string `json:"domain"`
Service string `json:"service"` Service string `json:"service"`
@@ -45,7 +44,7 @@ type BaseServiceRequest struct {
func NewBaseServiceRequest(entityId string) BaseServiceRequest { func NewBaseServiceRequest(entityId string) BaseServiceRequest {
id := internal.GetId() id := internal.GetId()
bsr := BaseServiceRequest{ bsr := BaseServiceRequest{
Id: fmt.Sprint(id), Id: id,
RequestType: "call_service", RequestType: "call_service",
} }
if entityId != "" { if entityId != "" {

View File

@@ -3,20 +3,23 @@ package websocket
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
type BaseMessage struct { type BaseMessage struct {
Type string `json:"type"` Type string `json:"type"`
Id int64 `json:"id"` Id int64 `json:"id"`
Success bool `json:"success"`
} }
type ChanMsg struct { type ChanMsg struct {
Id int64 Id int64
Type string Type string
Raw []byte Success bool
Raw []byte
} }
func ListenWebsocket(conn *websocket.Conn, ctx context.Context, c chan ChanMsg) { 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)
break break
} }
base := BaseMessage{} base := BaseMessage{
// default to true for messages that don't include "success" at all
Success: true,
}
json.Unmarshal(bytes, &base) json.Unmarshal(bytes, &base)
if !base.Success {
fmt.Println("WARNING: received unsuccessful response:", string(bytes))
}
chanMsg := ChanMsg{ chanMsg := ChanMsg{
Type: base.Type, Type: base.Type,
Id: base.Id, Id: base.Id,
Raw: bytes, Success: base.Success,
Raw: bytes,
} }
c <- chanMsg c <- chanMsg