1
0
Fork 0
hats/pkg/homeassistant/rest.go

54 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-10-12 17:23:35 +00:00
package homeassistant
import (
"fmt"
"strings"
"github.com/go-resty/resty/v2"
)
type RestClient struct {
client *resty.Client
}
func NewRestClient(baseUrl, token string) *RestClient {
client := resty.New().SetBaseURL(baseUrl)
client.SetHeaders(map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", token),
"Accept": "application/json",
})
return &RestClient{
client: client,
}
}
func (c *RestClient) GetState(entityId string) (StateData, error) {
var data StateData
resp, err := c.client.R().SetResult(&data).Get(fmt.Sprintf("api/states/%s", entityId))
if err == nil && !resp.IsSuccess() {
err = fmt.Errorf("%d status code received: %s", resp.StatusCode(), resp.String())
}
return data, err
}
2023-10-18 18:35:46 +00:00
func (c *RestClient) CallService(entityId string, service string, extras ...map[string]any) error {
2023-10-12 17:23:35 +00:00
domain := strings.Split(entityId, ".")[0]
2023-11-07 22:55:04 +00:00
return c.CallServiceManual(domain, entityId, service, extras...)
}
func (c *RestClient) CallServiceManual(domain string, entityId string, service string, extras ...map[string]any) error {
2023-10-18 18:35:46 +00:00
data := map[string]any{
2023-10-12 17:23:35 +00:00
"entity_id": entityId,
}
for _, extra := range extras {
for k, v := range extra {
data[k] = v
}
}
resp, err := c.client.R().SetBody(data).Post(fmt.Sprintf("api/services/%s/%s", domain, service))
if err == nil && !resp.IsSuccess() {
err = fmt.Errorf("%d status code received: %s", resp.StatusCode(), resp.String())
}
return err
}