Reorganized code, added google home intergrations and added zigbee2mqtt kettle
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-11-15 01:03:30 +01:00
parent dace0eba29
commit dd03ae56ee
23 changed files with 2050 additions and 214 deletions

88
integration/kasa/kasa.go Normal file
View File

@@ -0,0 +1,88 @@
package kasa
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"net"
)
func encrypt(data []byte) []byte {
var key byte = 171
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(len(data)))
for _, c := range []byte(data) {
a := key ^ c
key = a
buf.WriteByte(a)
}
return buf.Bytes()
}
func decrypt(data []byte) string {
var key byte = 171
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(len(data)))
for _, c := range data {
a := key ^ c
key = c
buf.WriteByte(a)
}
return string(buf.Bytes())
}
type Kasa struct {
ip string
}
func New(ip string) Kasa {
return Kasa{ip}
}
func (kasa *Kasa) sendCmd(cmd cmd) {
con, err := net.Dial("tcp", fmt.Sprintf("%s:9999", kasa.ip))
if err != nil {
panic(err)
}
defer con.Close()
b, err := json.Marshal(cmd)
if err != nil {
panic(err)
}
_, err = con.Write(encrypt(b))
if err != nil {
panic(err)
}
resp := make([]byte, 2048)
_, err = con.Read(resp)
if err != nil {
panic(err)
}
var reply reply
json.Unmarshal(resp, &reply)
if reply.System.SetRelayState.ErrCode != 0 {
fmt.Println(reply)
fmt.Println(resp)
}
}
func (kasa *Kasa) SetState(on bool) {
var cmd cmd
if on {
cmd.System.SetRelayState.State = 1
}
kasa.sendCmd(cmd)
}

19
integration/kasa/reply.go Normal file
View File

@@ -0,0 +1,19 @@
package kasa
type errCode struct {
ErrCode int
}
type reply struct {
System struct {
SetRelayState errCode `json:"set_relay_state"`
} `json:"system"`
}
type cmd struct {
System struct {
SetRelayState struct {
State int `json:"state"`
} `json:"set_relay_state"`
} `json:"system"`
}