Skip to content

Commit

Permalink
fix(hass): 🐛 correct JSON marshaling
Browse files Browse the repository at this point in the history
- fix marshaling event request type
- simplify setting request body for all request types
  • Loading branch information
joshuar committed Nov 19, 2024
1 parent 0183281 commit bd31214
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
15 changes: 2 additions & 13 deletions internal/hass/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/go-resty/resty/v2"

"github.com/joshuar/go-hass-agent/internal/hass/sensor"
"github.com/joshuar/go-hass-agent/internal/logging"
)

Expand Down Expand Up @@ -78,7 +77,7 @@ func (e *ResponseError) Error() string {
return strings.Join(msg, ": ")
}

func Send[T any](ctx context.Context, url string, details any) (T, error) {
func Send[T any](ctx context.Context, url string, details Request) (T, error) {
var response T

requestClient := client.R().SetContext(ctx)
Expand All @@ -96,17 +95,7 @@ func Send[T any](ctx context.Context, url string, details any) (T, error) {
}
}

// Explicitly set the request body based on the type. Not doing this with
// composable types may result in an unexpected body.
switch request := details.(type) {
case sensor.Entity:
requestClient.SetBody(request.RequestBody())
case sensor.State:
requestClient.SetBody(request.RequestBody())
case Request:
requestClient.SetBody(request.RequestBody())
}

requestClient.SetBody(details.RequestBody())
responseObj, err := requestClient.Post(url)

logging.FromContext(ctx).
Expand Down
4 changes: 2 additions & 2 deletions internal/hass/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *Client) HassVersion(ctx context.Context) string {
}

func (c *Client) ProcessEvent(ctx context.Context, details event.Event) error {
resp, err := api.Send[response](ctx, c.url, details)
resp, err := api.Send[response](ctx, c.url, &details)
if err != nil {
return fmt.Errorf("failed to send event request: %w", err)
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func (c *Client) ProcessSensor(ctx context.Context, details sensor.Entity) error
}

// Sensor registration.
resp, err := api.Send[registrationResponse](ctx, c.url, details)
resp, err := api.Send[registrationResponse](ctx, c.url, &details)
if err != nil {
return fmt.Errorf("failed to send sensor registration: %w", err)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/hass/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func (c *Config) IsEntityDisabled(entity string) (bool, error) {

type configRequest struct{}

func (c *configRequest) RequestType() string {
return "get_config"
}

func (c *configRequest) RequestData() any {
return nil
func (c *configRequest) RequestBody() any {
return struct {
Type string `json:"type"`
}{
Type: "get_config",
}
}
14 changes: 8 additions & 6 deletions internal/hass/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ func (e *Event) Validate() error {
return nil
}

func (e *Event) RequestType() string {
return requestTypeEvent
}

func (e *Event) RequestData() any {
return e
func (e *Event) RequestBody() any {
return struct {
Data any `json:"data"`
RequestType string `json:"type"`
}{
RequestType: requestTypeEvent,
Data: e,
}
}
2 changes: 1 addition & 1 deletion internal/hass/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type response struct {
}

func (r *response) Status() (responseStatus, error) {
if r.IsSuccess {
if r.IsSuccess || r.ErrorDetails == nil {
return Updated, nil
}

Expand Down

0 comments on commit bd31214

Please sign in to comment.