This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues or pull requests.
automation/ntfy/ntfy.go
Dreaded_X dace0eba29
All checks were successful
continuous-integration/drone/push Build is passing
Refactored code and added support for kasa smart plugs
2022-09-24 00:42:42 +02:00

47 lines
948 B
Go

package ntfy
import (
"fmt"
"net/http"
"os"
"strings"
)
type ntfy struct {
topic string
}
func (ntfy *ntfy) Presence(home bool) {
// @TODO Maybe add list the devices that are home currently?
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, err := http.NewRequest("POST", fmt.Sprintf("https://ntfy.sh/%s", ntfy.topic), strings.NewReader(description))
if err != nil {
panic(err)
}
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 Connect() ntfy {
topic, _ := os.LookupEnv("NTFY_TOPIC")
ntfy := ntfy{topic}
// @TODO Make sure the topic is valid?
return ntfy
}