run homeassistant webhook trigger
This commit is contained in:
1 parent
120d34ec37
commit
1248b36a80
2 files changed
+41
-6
No files matched your search
@@ -27,12 +27,13 @@ type ConfigLogging struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ConfigRing struct {
|
type ConfigRing struct {
|
||||||
Device *string `arg:"--input-device,env:INPUT_DEVICE"`
|
Device *string `arg:"--input-device,env:INPUT_DEVICE"`
|
||||||
Key string `arg:"--ring-key" default:"KEY_F1"`
|
Key string `arg:"--ring-key" default:"KEY_F1"`
|
||||||
JingleBaseUri *string `arg:"--jingle-base-uri,env:JINGLE_BASE_URI"`
|
JingleBaseUri *string `arg:"--jingle-base-uri,env:JINGLE_BASE_URI"`
|
||||||
JinglePath string `arg:"--jingle-path,env:JINGLE_PATH" default:"audio/ding-dong.wav"`
|
JinglePath string `arg:"--jingle-path,env:JINGLE_PATH" default:"audio/ding-dong.wav"`
|
||||||
SonosTarget string `arg:"--sonos-target,env:SONOS_TARGET" default:"-"`
|
SonosTarget string `arg:"--sonos-target,env:SONOS_TARGET" default:"-"`
|
||||||
SonosVolume int `arg:"--sonos-volume,env:SONOS_VOLUME" default:"50"`
|
SonosVolume int `arg:"--sonos-volume,env:SONOS_VOLUME" default:"50"`
|
||||||
|
HomeassistantWebhook *string `arg:"--ha-webhook,env:HA_WEBHOOK"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigHTTP struct {
|
type ConfigHTTP struct {
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package ring
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/holoplot/go-evdev"
|
"github.com/holoplot/go-evdev"
|
||||||
"github.com/kaedwen/webrtc/pkg/common"
|
"github.com/kaedwen/webrtc/pkg/common"
|
||||||
@@ -72,12 +74,26 @@ func (h *RingHandler) Watch(ctx context.Context) error {
|
|||||||
for {
|
for {
|
||||||
e, err := d.ReadOne()
|
e, err := d.ReadOne()
|
||||||
if err == nil && e.Code == key && e.Value == 0 {
|
if err == nil && e.Code == key && e.Value == 0 {
|
||||||
|
// run all players
|
||||||
tu := bu.JoinPath(h.cfg.JinglePath)
|
tu := bu.JoinPath(h.cfg.JinglePath)
|
||||||
for _, p := range h.playHandlers {
|
for _, p := range h.playHandlers {
|
||||||
if err := p.Play(tu); err != nil {
|
if err := p.Play(tu); err != nil {
|
||||||
h.lg.Error("failed to play", zap.Error(err))
|
h.lg.Error("failed to play", zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// run webhooks when configured
|
||||||
|
if h.cfg.HomeassistantWebhook != nil {
|
||||||
|
h.lg.Info("Triggering Homeassistant Webhook", zap.String("hook", *h.cfg.HomeassistantWebhook))
|
||||||
|
ctxt, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||||
|
|
||||||
|
err := h.TriggerWebhook(ctxt, *h.cfg.HomeassistantWebhook)
|
||||||
|
if err != nil {
|
||||||
|
h.lg.Error("failed to trigger webhook", zap.Error(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -89,3 +105,21 @@ func (h *RingHandler) Watch(ctx context.Context) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *RingHandler) TriggerWebhook(ctx context.Context, hook string) error {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, *h.cfg.HomeassistantWebhook, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.StatusCode != 200 {
|
||||||
|
return fmt.Errorf("received wrong status code - %d", res.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in new issue
Block a user