From e5a6b75f62424cbfeb6e1baaf4c1b7b2114a3e82 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 16 Sep 2022 22:24:32 +0200 Subject: [PATCH] Added notifcation when presence state changes --- .drone.yml | 2 +- main.go | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/.drone.yml b/.drone.yml index 8978c60..9f07443 100644 --- a/.drone.yml +++ b/.drone.yml @@ -32,7 +32,7 @@ steps: - docker rm automation || true - - docker run -e MQTT_HOST=$MQTT_HOST -e MQTT_PORT=$MQTT_PORT -e MQTT_USER=$MQTT_USER -e MQTT_PASS=$MQTT_PASS -e MQTT_CLIENT_ID=$MQTT_CLIENT_ID -e HUE_BRIDGE=$HUE_BRIDGE --network mqtt --name automation -d automation + - docker run -e MQTT_HOST=$MQTT_HOST -e MQTT_PORT=$MQTT_PORT -e MQTT_USER=$MQTT_USER -e MQTT_PASS=$MQTT_PASS -e MQTT_CLIENT_ID=$MQTT_CLIENT_ID -e HUE_BRIDGE=$HUE_BRIDGE -e NTFY_TOPIC=%NTFY_TOPIC --network mqtt --name automation -d automation when: branch: diff --git a/main.go b/main.go index dd3caeb..4b2a9fc 100644 --- a/main.go +++ b/main.go @@ -83,6 +83,32 @@ func (hue *Hue) updateFlag(id int, value bool) { } } +type ntfy struct{ + topic string +} + +func (ntfy *ntfy) notifyPresence(home bool) { + var description string + var actions string + if home { + description = "Home" + actions = "broadcast, Set as away, extras.cmd=presence, extras.state=0, clear=true" + } else { + description = "Away" + actions = "broadcast, Set as home, extras.cmd=presence, extras.state=1, clear=true" + } + + req, _ := http.NewRequest("POST", fmt.Sprintf("https://ntfy.sh/%s", ntfy.topic), + strings.NewReader(description)) + + req.Header.Set("Title", "Presence") + req.Header.Set("Tags", "house") + req.Header.Set("Actions", actions) + req.Header.Set("Priority", "1") + + http.DefaultClient.Do(req) +} + func connectToHue() Hue { login, _ := os.LookupEnv("HUE_BRIDGE") @@ -130,6 +156,15 @@ func connectMQTT() MQTT.Client { return client } +func connectNtfy() ntfy { + topic, _ := os.LookupEnv("NTFY_TOPIC") + ntfy := ntfy{topic} + + // @TODO Make sure the topic is valid? + + return ntfy +} + func main() { _ = godotenv.Load() @@ -138,32 +173,27 @@ func main() { signal.Notify(halt, os.Interrupt, syscall.SIGTERM) // MQTT - presence := make(chan bool, 1) client := connectMQTT() + presence := make(chan bool, 1) if token := client.Subscribe("automation/presence/+", 0, presenceHandler(presence)); token.Wait() && token.Error() != nil { fmt.Println(token.Error()) os.Exit(1) } - // Hue hue := connectToHue() - fmt.Println("Starting event loop") + // ntfy.sh + ntfy := connectNtfy() // Event loop + fmt.Println("Starting event loop") events: for { select { case present := <-presence: - if present { - fmt.Println("Coming home") - hue.updateFlag(41, true) - } else { - fmt.Println("Leaving home") - hue.updateFlag(41, false) - } - - fmt.Println("Done") + fmt.Printf("Present: %t", present) + hue.updateFlag(41, present) + ntfy.notifyPresence(present) case <-halt: break events