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

60
integration/hue/events.go Normal file
View File

@@ -0,0 +1,60 @@
package hue
import (
"time"
)
type EventType string
const (
Update EventType = "update"
)
type DeviceType string
const (
Light DeviceType = "light"
GroupedLight = "grouped_light"
Button = "button"
)
type LastEvent string
const (
InitialPress LastEvent = "initial_press"
ShortPress = "short_press"
)
type device struct {
ID string `json:"id"`
IDv1 string `json:"id_v1"`
Owner struct {
Rid string `json:"rid"`
Rtype string `json:"rtype"`
} `json:"owner"`
Type DeviceType `json:"type"`
On *struct {
On bool `json:"on"`
} `json:"on"`
Dimming *struct {
Brightness float32 `json:"brightness"`
} `json:"dimming"`
ColorTemperature *struct {
Mirek int `json:"mirek"`
MirekValid bool `json:"mirek_valid"`
} `json:"color_temperature"`
Button *struct {
LastEvent LastEvent `json:"last_event"`
}
}
type Event struct {
CreationTime time.Time `json:"creationtime"`
Data []device `json:"data"`
ID string `json:"id"`
Type EventType `json:"type"`
}

60
integration/hue/hue.go Normal file
View File

@@ -0,0 +1,60 @@
package hue
import (
"bytes"
"crypto/tls"
"fmt"
"net/http"
"os"
"github.com/r3labs/sse/v2"
)
type Hue struct {
ip string
login string
Events chan *sse.Event
}
func (hue *Hue) SetFlag(id int, value bool) {
url := fmt.Sprintf("http://%s/api/%s/sensors/%d/state", hue.ip, hue.login, id)
var data []byte
if value {
data = []byte(`{ "flag": true }`)
} else {
data = []byte(`{ "flag": false }`)
}
client := &http.Client{}
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data))
if err != nil {
panic(err)
}
_, err = client.Do(req)
if err != nil {
panic(err)
}
}
func Connect() Hue {
login, _ := os.LookupEnv("HUE_BRIDGE")
ip, _ := os.LookupEnv("HUE_IP")
hue := Hue{ip: ip, login: login, Events: make(chan *sse.Event)}
// Subscribe to eventstream
client := sse.NewClient(fmt.Sprintf("https://%s/eventstream/clip/v2", hue.ip))
client.Headers["hue-application-key"] = hue.login
client.Connection.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
err := client.SubscribeChanRaw(hue.Events)
if err != nil {
panic(err)
}
return hue
}