This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues or pull requests.
automation/integration/zigbee/devices.go
Dreaded_X c091ad0782
Some checks failed
continuous-integration/drone/push Build is failing
Finished major refactor
2022-11-19 03:50:12 +01:00

40 lines
916 B
Go

package zigbee
import (
"automation/device"
"automation/home"
"context"
"encoding/json"
"log"
paho "github.com/eclipse/paho.mqtt.golang"
)
func DevicesHandler(client paho.Client, home *home.Home) {
var handler paho.MessageHandler = func(client paho.Client, msg paho.Message) {
var devices []Info
json.Unmarshal(msg.Payload(), &devices)
for name, d := range device.GetDevices[Device](&home.Devices) {
d.Delete()
// Delete all zigbee devices from the device list
delete(home.Devices, name)
}
for _, d := range devices {
switch d.Description {
case "Kettle":
kettle := NewKettle(d, client, home.Service)
home.AddDevice(kettle)
}
}
// Send sync request
home.Service.RequestSync(context.Background(), home.Username)
}
if token := client.Subscribe("zigbee2mqtt/bridge/devices", 1, handler); token.Wait() && token.Error() != nil {
log.Println(token.Error())
}
}