forked from whs/go-aqaramqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aqaramqtt.go
321 lines (271 loc) · 7.5 KB
/
aqaramqtt.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"encoding/json"
"fmt"
"log"
"math"
"strconv"
"strings"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/whs/go-aqaramqtt/aqara"
)
const MidVol = 50
// AqaraMqtt bridges between Xiaomi Aqara and MQTT
type AqaraMqtt struct {
mqtt mqtt.Client
gateway *aqara.Gateway
gatewayListener aqara.GatewayListener
Prefix string
aqaraChan chan aqara.ListenResponse
lastRgb *HassColorMessage
}
// NewAqaraMqtt return new instance of AqaraMqtt
func NewAqaraMqtt(mqttOptions *mqtt.ClientOptions, gateway *aqara.Gateway, prefix string) *AqaraMqtt {
out := &AqaraMqtt{
gateway: gateway,
gatewayListener: aqara.NewGatewayListener([]*aqara.Gateway{gateway}),
aqaraChan: make(chan aqara.ListenResponse),
Prefix: prefix,
}
out.initMqtt(mqttOptions)
return out
}
func (a *AqaraMqtt) initMqtt(mqttOptions *mqtt.ClientOptions) {
mqttOptions.SetWill(fmt.Sprintf("%s/status", a.Prefix), "0", 1, true)
mqttOptions.SetOnConnectHandler(func(client mqtt.Client) {
log.Print("connected to mqtt")
if token := client.Publish(fmt.Sprintf("%s/status", a.Prefix), 1, true, "1"); token.Wait() && token.Error() != nil {
log.Printf("LWT publish fail: %v", token.Error())
}
if err := a.registerDevices(); err != nil {
log.Print(err)
}
})
a.mqtt = mqtt.NewClient(mqttOptions)
// mqtt.DEBUG = log.New(os.Stdout, "[mqtt] ", log.LstdFlags)
// mqtt.ERROR = mqtt.DEBUG
// mqtt.WARN = mqtt.DEBUG
// mqtt.CRITICAL = mqtt.DEBUG
}
// Start AqaraMqtt server
func (a *AqaraMqtt) Start() error {
go a.listenAqara()
a.connectMqtt()
a.streamAqara()
return nil
}
func (a *AqaraMqtt) listenAqara() {
err := a.gatewayListener.Listen(a.aqaraChan)
if err != nil {
panic(err)
}
}
func (a *AqaraMqtt) connectMqtt() {
log.Print("Connecting to MQTT")
if token := a.mqtt.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
log.Print("Connected to MQTT")
}
func (a *AqaraMqtt) registerDevices() error {
if token := a.mqtt.Subscribe(fmt.Sprintf("%s/gateway/%s/set_rgb", a.Prefix, a.gateway.Sid), 0, a.setRgbHandler); token.Wait() && token.Error() != nil {
return token.Error()
}
if token := a.mqtt.Subscribe(fmt.Sprintf("%s/gateway/%s/set_mid", a.Prefix, a.gateway.Sid), 0, a.setMidHandler); token.Wait() && token.Error() != nil {
return token.Error()
}
sids, err := a.gateway.GetDevices()
if err != nil {
return err
}
for _, sid := range sids {
status, err := a.gateway.GetDeviceStatus(sid)
log.Printf("registering %s", sid)
if err != nil {
log.Printf("cannot get device %s status: %v", sid, err)
continue
}
switch status.Model {
case "plug":
if token := a.mqtt.Subscribe(fmt.Sprintf("%s/plug/%s/set_status", a.Prefix, sid), 0, a.setStatusHandler); token.Wait() && token.Error() != nil {
return token.Error()
}
}
if err := a.publishReport(status); err != nil {
return err
}
}
return nil
}
func (a *AqaraMqtt) streamAqara() {
for {
message := <-a.aqaraChan
if err := a.publishReport(&message.Message); err != nil {
log.Println(err)
}
}
}
func (a *AqaraMqtt) publishReport(report *aqara.ReportMessage) error {
for k, v := range report.Data {
var value string
if report.Model == "gateway" && k == "rgb" {
value = formatHassRgb(int64(v.(float64)))
} else if report.Model == "switch" && k == "status" {
value = v.(string)
if value == "long_click_release" {
value = "release"
}
} else if report.Model == "motion" && k == "no_motion" {
k = "status"
value = "no_motion"
} else if k == "voltage" && report.Model != "plug" {
value = convertVoltage(v.(float64))
} else {
value = fmt.Sprintf("%v", v)
}
if token := a.mqtt.Publish(fmt.Sprintf("%s/%s/%s/%s", a.Prefix, report.Model, report.Sid, k), 0, true, value); token.Wait() && token.Error() != nil {
return token.Error()
}
// switch click event must be followed immediately by release event
if report.Model == "switch" && k == "status" && value != "long_click_press" && value != "release" {
if token := a.mqtt.Publish(fmt.Sprintf("%s/switch/%s/status", a.Prefix, report.Sid), 0, true, "release"); token.Wait() && token.Error() != nil {
return token.Error()
}
}
}
return nil
}
func (a *AqaraMqtt) setRgbHandler(_ mqtt.Client, message mqtt.Message) {
log.Print(message)
sid := getSidFromMqttTopic(message.Topic())
if a.gateway.Sid != sid {
log.Printf("sid %s not of the gateway %s. skipping", sid, a.gateway.Sid)
return
}
rgb, hassMessage, err := parseHassRgbMessage(message.Payload(), a.lastRgb)
if err != nil {
log.Print(err)
return
}
a.lastRgb = hassMessage
out, err := a.gateway.SetRGB(rgb)
if err != nil {
log.Print(err)
} else {
log.Print(out)
}
}
func (a *AqaraMqtt) setMidHandler(_ mqtt.Client, message mqtt.Message) {
log.Print(message)
sid := getSidFromMqttTopic(message.Topic())
if a.gateway.Sid != sid {
log.Printf("sid %s not of the gateway %s. skipping", sid, a.gateway.Sid)
return
}
mid, err := strconv.ParseUint(string(message.Payload()), 10, 32)
if err != nil {
log.Print(err)
return
}
out, err := a.gateway.SetMID(uint(mid), MidVol)
if err != nil {
log.Print(err)
} else {
log.Print(out)
}
}
func (a *AqaraMqtt) setStatusHandler(_ mqtt.Client, message mqtt.Message) {
log.Print(message)
sid := getSidFromMqttTopic(message.Topic())
out, err := a.gateway.SetStatus(sid, string(message.Payload()))
if err != nil {
log.Print(err)
} else {
log.Print(out)
}
}
func getSidFromMqttTopic(topic string) string {
parts := strings.Split(topic, "/")
return parts[2]
}
// HassColorMessage are used by Home Assistant
// https://www.home-assistant.io/components/light.mqtt_json/
type HassColorMessage struct {
Brightness *uint8 `json:"brightness"`
Color *colorMessage `json:"color"`
State string `json:"state"`
}
type colorMessage struct {
R uint8 `json:"r"`
G uint8 `json:"g"`
B uint8 `json:"b"`
}
func formatHassRgb(rgb int64) string {
a := uint8((rgb & 0xFF000000) >> 24)
r := uint8((rgb & 0x00FF0000) >> 16)
g := uint8((rgb & 0x0000FF00) >> 8)
b := uint8(rgb & 0x000000FF)
var state string
if a > 0 {
state = "ON"
} else {
state = "OFF"
}
message := HassColorMessage{
Brightness: &a,
State: state,
Color: &colorMessage{
R: r,
G: g,
B: b,
},
}
out, err := json.Marshal(message)
if err != nil {
log.Print(err)
return ""
}
return string(out)
}
func parseHassRgbMessage(message []byte, lastRgb *HassColorMessage) (uint64, *HassColorMessage, error) {
var parsed HassColorMessage
var zero uint8
var max uint8 = 100
if err := json.Unmarshal(message, &parsed); err != nil {
return 0, nil, err
}
if parsed.State == "OFF" {
parsed.Brightness = &zero
}
if lastRgb != nil {
if parsed.Brightness == nil {
parsed.Brightness = lastRgb.Brightness
}
if parsed.Color == nil {
parsed.Color = lastRgb.Color
}
} else {
if parsed.Brightness == nil {
parsed.Brightness = &zero
}
if parsed.Color == nil {
parsed.Color = &colorMessage{R: 0, G: 0, B: 0}
}
}
if *parsed.Brightness == 0 && parsed.State == "ON" {
parsed.Brightness = &max
}
var out uint64
out |= uint64(*parsed.Brightness) << 24
out |= uint64(parsed.Color.R) << 16
out |= uint64(parsed.Color.G) << 8
out |= uint64(parsed.Color.B)
return out, &parsed, nil
}
var maxVolt float64 = 3300
var minVolt float64 = 2800
func convertVoltage(voltage float64) string {
value := math.Max(math.Min(voltage, maxVolt), minVolt)
out := ((value - minVolt) / (maxVolt - minVolt)) * 100
return fmt.Sprintf("%.0f", (math.Round(out*100) / 100))
}