Added notifcation when presence state changes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dreaded_X 2022-09-16 22:24:32 +02:00
parent e3edee3e09
commit e5a6b75f62
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
2 changed files with 43 additions and 13 deletions

View File

@ -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:

54
main.go
View File

@ -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