evcc-io/messenger/pushover.go
Maschga 7f772bad73
Config UI: make messengers configurable (#26946)
Co-authored-by: andig <cpuidle@gmail.com>
Co-authored-by: andig <andi@evcc.io>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: Michael Geers <michael@geers.tv>
2026-02-17 12:26:03 +01:00

67 lines
1.4 KiB
Go

package messenger
import (
"errors"
"strings"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/gregdel/pushover"
)
func init() {
registry.Add("pushover", NewPushOverFromConfig)
}
// PushOver implements the pushover messenger
type PushOver struct {
log *util.Logger
app *pushover.Pushover
device string
recipients []string
}
// NewPushOverFromConfig creates new pushover messenger
func NewPushOverFromConfig(other map[string]any) (api.Messenger, error) {
var cc struct {
App string
Recipients []string
Devices []string
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.App == "" {
return nil, errors.New("missing app name")
}
m := &PushOver{
log: util.NewLogger("pushover").Redact(cc.App),
app: pushover.New(cc.App),
device: strings.Join(cc.Devices, ","),
recipients: cc.Recipients,
}
m.log.Redact(cc.Recipients...)
return m, nil
}
// Send sends to all receivers
func (m *PushOver) Send(title, msg string) {
message := pushover.NewMessageWithTitle(msg, title)
message.DeviceName = m.device
for _, id := range m.recipients {
go func(id string) {
m.log.DEBUG.Printf("sending to %s", id)
recipient := pushover.NewRecipient(id)
if _, err := m.app.SendMessage(message, recipient); err != nil {
m.log.ERROR.Print(err)
}
}(id)
}
}