Did some groundwork for setting up more complex interactions
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dreaded_X 2022-11-18 04:27:43 +01:00
parent 2aff4d937a
commit 4c023ad933
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
2 changed files with 70 additions and 7 deletions

View File

@ -9,8 +9,8 @@ mqtt:
kasa: kasa:
outlets: outlets:
mixer: 10.0.0.49 living_room/mixer: 10.0.0.49
speakers: 10.0.0.182 living_room/speakers: 10.0.0.182
computers: computers:
zeus: zeus:

73
main.go
View File

@ -61,11 +61,70 @@ func GetConfig() config {
return cfg return cfg
} }
var devices map[string]interface{}
// // @TODO Implement this for the other devices as well
// func GetDeviceKasa(name string) (*kasa.Kasa, error) {
// deviceGeneric, ok := devices[name]
// if !ok {
// return nil, fmt.Errorf("Device does not exist")
// }
// device, ok := deviceGeneric.(kasa.Kasa)
// if !ok {
// return nil, fmt.Errorf("Device is not a Kasa device")
// }
// return &device, nil
// }
// func SetupBindings(m *mqtt.MQTT) {
// m.AddHandler("zigbee2mqtt/living_room/audio_remote", func(_ paho.Client, msg paho.Message) {
// mixer, err := GetDeviceKasa("living_room/mixer")
// if err != nil {
// log.Println(err)
// return
// }
// speakers, err := GetDeviceKasa("living_room/speakers")
// if err != nil {
// log.Println(err)
// return
// }
// var message struct {
// Action string `json:"action"`
// }
// err = json.Unmarshal(msg.Payload(), &message)
// if err != nil {
// log.Println(err)
// return
// }
// if message.Action == "on" {
// if mixer.GetState() {
// mixer.SetState(false)
// speakers.SetState(false)
// } else {
// mixer.SetState(true)
// }
// } else if message.Action == "brightness_move_up" {
// if speakers.GetState() {
// speakers.SetState(false)
// } else {
// speakers.SetState(true)
// mixer.SetState(true)
// }
// }
// })
// }
func main() { func main() {
_ = godotenv.Load() _ = godotenv.Load()
config := GetConfig() config := GetConfig()
devices = make(map[string]interface{})
// MQTT // MQTT
m := mqtt.Connect(config.MQTT) m := mqtt.Connect(config.MQTT)
defer m.Disconnect() defer m.Disconnect()
@ -74,9 +133,8 @@ func main() {
h := hue.Connect(config.Hue) h := hue.Connect(config.Hue)
// Kasa // Kasa
kasaDevices := make(map[string]kasa.Kasa)
for name, ip := range config.Kasa.Outlets { for name, ip := range config.Kasa.Outlets {
kasaDevices[name] = kasa.New(ip) devices[name] = kasa.New(ip)
} }
// ntfy.sh // ntfy.sh
@ -113,9 +171,14 @@ func main() {
// Turn off all the devices that we manage ourselves // Turn off all the devices that we manage ourselves
provider.TurnAllOff() provider.TurnAllOff()
// Turn off kasa devices // Turn off all devices
for _, device := range kasaDevices { // @TODO Maybe allow for exceptions, could be a list in the config that we check against?
device.SetState(false) for _, device := range devices {
switch d := device.(type) {
case kasa.Kasa:
d.SetState(false)
}
} }
// @TODO Turn off nest thermostat // @TODO Turn off nest thermostat