From 20e7e830a625842be17a69b62fba7d7b42466485 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sat, 19 Nov 2022 03:55:09 +0100 Subject: [PATCH] Renamed output binary to avoid conflict --- .gitignore | 2 +- Dockerfile | 6 ++-- automation/automation.go | 14 ++++++++++ automation/mixer.go | 56 ++++++++++++++++++++++++++++++++++++++ automation/presence.go | 59 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 automation/automation.go create mode 100644 automation/mixer.go create mode 100644 automation/presence.go diff --git a/.gitignore b/.gitignore index b3cfb1a..1b7bd00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ storage/ -automation +app .env tmp/ diff --git a/Dockerfile b/Dockerfile index ca0e7f9..f748405 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,13 +7,13 @@ RUN go mod download COPY . . -RUN go build +RUN go build -o app FROM golang:alpine WORKDIR /app -COPY --from=build-automation /src/automation /app/automation +COPY --from=build-automation /src/app /app/app COPY --from=build-automation /src/config.yml /app/config.yml -CMD ["/app/automation"] +CMD ["/app/app"] diff --git a/automation/automation.go b/automation/automation.go new file mode 100644 index 0000000..518c88e --- /dev/null +++ b/automation/automation.go @@ -0,0 +1,14 @@ +package automation + +import ( + "automation/home" + "automation/integration/hue" + "automation/integration/ntfy" + + paho "github.com/eclipse/paho.mqtt.golang" +) + +func RegisterAutomations(client paho.Client, hue *hue.Hue, notify *ntfy.Notify, home *home.Home) { + presenceAutomation(client, hue, notify, home) + mixerAutomation(client, home) +} diff --git a/automation/mixer.go b/automation/mixer.go new file mode 100644 index 0000000..367a4c0 --- /dev/null +++ b/automation/mixer.go @@ -0,0 +1,56 @@ +package automation + +import ( + "automation/device" + "automation/home" + "automation/integration/kasa" + "encoding/json" + "log" + + paho "github.com/eclipse/paho.mqtt.golang" +) + +func mixerAutomation(client paho.Client, home *home.Home) { + var handler paho.MessageHandler = func(client paho.Client, msg paho.Message) { + mixer, err := device.GetDevice[*kasa.Outlet](&home.Devices, "living_room/mixer") + if err != nil { + log.Println(err) + return + } + speakers, err := device.GetDevice[*kasa.Outlet](&home.Devices, "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.GetOnOff() { + mixer.SetOnOff(false) + speakers.SetOnOff(false) + } else { + mixer.SetOnOff(true) + } + } else if message.Action == "brightness_move_up" { + if speakers.GetOnOff() { + speakers.SetOnOff(false) + } else { + speakers.SetOnOff(true) + mixer.SetOnOff(true) + } + } + } + + if token := client.Subscribe("test/remote", 1, handler); token.Wait() && token.Error() != nil { + log.Println(token.Error()) + } +} + diff --git a/automation/presence.go b/automation/presence.go new file mode 100644 index 0000000..27a7421 --- /dev/null +++ b/automation/presence.go @@ -0,0 +1,59 @@ +package automation + +import ( + "automation/device" + "automation/home" + "automation/integration/hue" + "automation/integration/ntfy" + "automation/presence" + "encoding/json" + "fmt" + "log" + + paho "github.com/eclipse/paho.mqtt.golang" +) + +func presenceAutomation(client paho.Client, hue *hue.Hue, notify *ntfy.Notify, home *home.Home) { + var handler paho.MessageHandler = func(client paho.Client, msg paho.Message) { + if len(msg.Payload()) == 0 { + // In this case we clear the persistent message + return + } + var message presence.Message + err := json.Unmarshal(msg.Payload(), &message) + if err != nil { + log.Println(err) + return + } + + fmt.Printf("Presence: %t\n", message.State) + + // Set presence on the hue bridge + hue.SetFlag(41, message.State) + + if !message.State { + log.Println("Turn off all the devices") + + // Turn off all devices + // @TODO Maybe allow for exceptions, could be a list in the config that we check against? + for _, dev := range home.Devices { + switch d := dev.(type) { + case device.OnOff: + d.SetOnOff(false) + } + + } + + // @TODO Turn off nest thermostat + } else { + // @TODO Turn on the nest thermostat again + } + + // Notify users of presence update + notify.Presence(message.State) + } + + if token := client.Subscribe("test/remote", 1, handler); token.Wait() && token.Error() != nil { + log.Println(token.Error()) + } +}