Poo recorder almost ported to go, needs to have perodically backup
This commit is contained in:
96
src/util/homeassistantutil/homeassistantutil.go
Normal file
96
src/util/homeassistantutil/homeassistantutil.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package homeassistantutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
ipField string = "homeassistant.ip"
|
||||
portField string = "homeassistant.port"
|
||||
authTokenField string = "homeassistant.authToken"
|
||||
webhookPath string = "/api/webhook/"
|
||||
sensorPath string = "/api/states/"
|
||||
)
|
||||
|
||||
type HttpSensor struct {
|
||||
EntityId string `json:"entity_id"`
|
||||
State string `json:"state"`
|
||||
Attributes interface{} `json:"attributes"`
|
||||
}
|
||||
|
||||
type WebhookBody interface{}
|
||||
|
||||
func TriggerWebhook(webhookId string, body WebhookBody) {
|
||||
if viper.InConfig(ipField) &&
|
||||
viper.InConfig(portField) &&
|
||||
viper.InConfig(authTokenField) {
|
||||
url := fmt.Sprintf("http://%s:%s%s%s", viper.GetString(ipField), viper.GetString(portField), webhookPath, webhookId)
|
||||
payload, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("TriggerWebhook Error marshalling", err))
|
||||
return
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("TriggerWebhook Error creating request", err))
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+viper.GetString(authTokenField))
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 1,
|
||||
}
|
||||
go func() {
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("TriggerWebhook Error sending request", err))
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
slog.Warn(fmt.Sprintln("TriggerWebhook Unexpected response status", resp.StatusCode))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
}()
|
||||
} else {
|
||||
slog.Warn("TriggerWebhook Home Assistant IP, port, or token not found in config file")
|
||||
}
|
||||
}
|
||||
|
||||
func PublishSensor(sensor HttpSensor) {
|
||||
if viper.InConfig(ipField) &&
|
||||
viper.InConfig(portField) &&
|
||||
viper.InConfig(authTokenField) {
|
||||
url := fmt.Sprintf("http://%s:%s%s%s", viper.GetString(ipField), viper.GetString(portField), sensorPath, sensor.EntityId)
|
||||
payload, err := json.Marshal(sensor)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("PublishSensor Error marshalling", err))
|
||||
return
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("PublishSensor Error creating request", err))
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+viper.GetString(authTokenField))
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 1,
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("PublishSensor Error sending request", err))
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
slog.Warn(fmt.Sprintln("PublishSensor Unexpected response status", resp.StatusCode))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
} else {
|
||||
slog.Warn("PublishSensor Home Assistant IP, port, or token not found in config file")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user