-
Notifications
You must be signed in to change notification settings - Fork 0
/
homeassistant.go
149 lines (121 loc) · 3.48 KB
/
homeassistant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"slices"
"strings"
"time"
)
type HAItem struct {
DevID string `json:"dev_id"`
Gps [2]float64 `json:"gps"`
GpsAccuracy float64 `json:"gps_accuracy"`
HostName string `json:"host_name"`
Battery float64 `json:"battery"`
// LocationName string `json:"location_name"`
}
func sendItemsToHomeAssistant(items *Items) {
for _, item := range *items {
if config.Update.Ignore != nil && slices.Contains(config.Update.Ignore, item.Identifier) {
continue
}
if !item.Location.LocationFinished {
continue
}
oldValue, ok := lruCache.Get(item.Identifier)
if ok {
if oldValue[0] == item.Location.Latitude && oldValue[1] == item.Location.Longitude {
continue
}
}
lruCache.Set(item.Identifier, [2]float64{item.Location.Latitude, item.Location.Longitude})
if menuInfo != nil {
menuInfo.SetTitle(fmt.Sprintf("last update: %s", time.Now().Format("15:04:05")))
}
batteryLevel := 0.00
if item.BatteryStatus > 0 {
batteryLevel = 100 / float64(item.BatteryStatus)
}
haItem := HAItem{
DevID: fmt.Sprintf("findmy_%s", strings.Replace(item.Identifier, "-", "", -1)),
Gps: [2]float64{
item.Location.Latitude,
item.Location.Longitude,
},
GpsAccuracy: item.Location.HorizontalAccuracy,
// LocationName: item.Address.MapItemFullAddress,
HostName: item.Name,
Battery: batteryLevel,
}
_ = processHomeassistant(haItem)
}
}
func sendDevicesToHomeAssistant(devices *Devices) {
for _, item := range *devices {
if config.Update.Ignore != nil && slices.Contains(config.Update.Ignore, item.Identifier) {
continue
}
if !item.Location.LocationFinished {
continue
}
if item.Location.HorizontalAccuracy > float64(config.Update.MinimalAccuracy) {
continue
}
oldValue, ok := lruCache.Get(item.Identifier)
if ok {
if oldValue[0] == item.Location.Latitude && oldValue[1] == item.Location.Longitude {
continue
}
}
lruCache.Set(item.Identifier, [2]float64{item.Location.Latitude, item.Location.Longitude})
if menuInfo != nil {
menuInfo.SetTitle(fmt.Sprintf("last update: %s", time.Now().Format("15:04:05")))
}
batteryLevel := 0.00
if item.BatteryStatus > 0 {
batteryLevel = float64(item.BatteryStatus) * 100
}
haItem := HAItem{
DevID: fmt.Sprintf("findmy_%s", strings.Replace(item.Identifier, "-", "", -1)),
Gps: [2]float64{
item.Location.Latitude,
item.Location.Longitude,
},
GpsAccuracy: item.Location.HorizontalAccuracy,
// LocationName: item.Address.MapItemFullAddress,
HostName: item.Name,
Battery: batteryLevel,
}
_ = processHomeassistant(haItem)
}
}
func processHomeassistant(haItem HAItem) error {
jsonStr, errMarshal := json.Marshal(haItem)
if errMarshal != nil {
return errMarshal
}
url := config.Homeassistant.URL
req, errNewRequest := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if errNewRequest != nil {
return errNewRequest
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.Homeassistant.Token))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, errDo := client.Do(req)
if errDo != nil {
return errDo
}
defer resp.Body.Close()
fmt.Println("request body:", string(jsonStr))
if resp.StatusCode != 200 {
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := io.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
}
return nil
}