From f7b0c1ac9c6b81c44f69b5d9e8980ab7ebb0d302 Mon Sep 17 00:00:00 2001 From: Matt Schmitz Date: Fri, 3 May 2024 20:10:53 -0400 Subject: [PATCH] Allow changing default MQTT topic prefix via config (#41) --- config/config.go | 13 +++++++------ docs/changelog.md | 4 ++++ docs/config.md | 5 +++++ events/mqtt.go | 8 +++++--- example-config.yml | 2 ++ 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index c2d8dd3..1f98ec9 100644 --- a/config/config.go +++ b/config/config.go @@ -31,12 +31,13 @@ type WebAPI struct { } type MQTT struct { - Enabled bool `fig:"enabled" default:false` - Server string `fig:"server" default:""` - Port int `fig:"port" default:1883` - ClientID string `fig:"clientid" default:"frigate-notify"` - Username string `fig:"username" default:""` - Password string `fig:"password" default:""` + Enabled bool `fig:"enabled" default:false` + Server string `fig:"server" default:""` + Port int `fig:"port" default:1883` + ClientID string `fig:"clientid" default:"frigate-notify"` + Username string `fig:"username" default:""` + Password string `fig:"password" default:""` + TopicPrefix string `fig:"topic_prefix" default:"frigate"` } type Cameras struct { diff --git a/docs/changelog.md b/docs/changelog.md index 6362fd3..b6caf31 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,9 @@ # Changelog +## [v0.2.7](https://github.com/0x2142/frigate-notify/releases/tag/v0.2.7) - TBD + +- Allow changing default MQTT topic prefix via config + ## [v0.2.6](https://github.com/0x2142/frigate-notify/releases/tag/v0.2.6) - Apr 01 2024 - Fixed issue with setting `unzoned: drop` under zone config, where alerts wouldn't be sent if event began outside a zone. diff --git a/docs/config.md b/docs/config.md index 3b38541..a461690 100644 --- a/docs/config.md +++ b/docs/config.md @@ -58,6 +58,9 @@ frigate: - **password** (Optional) - MQTT password - Required if `username` is set +- **topic_prefix** (Optional - Default: `frigate`) + - Optionally change MQTT topic prefix + - This should match the topic prefix used by Frigate ```yaml title="Config File Snippet" frigate: @@ -68,6 +71,7 @@ frigate: clientid: frigate-notify username: mqtt-user password: mqtt-pass + topic_prefix: frigate ``` ### Cameras @@ -345,6 +349,7 @@ frigate: clientid: username: password: + topic_prefix: cameras: exclude: diff --git a/events/mqtt.go b/events/mqtt.go index 7d37837..2a256ed 100644 --- a/events/mqtt.go +++ b/events/mqtt.go @@ -60,6 +60,7 @@ func SubscribeMQTT() { // processEvent handles incoming MQTT messages & pulls out relevant info for alerting func processEvent(client mqtt.Client, msg mqtt.Message) { + fmt.Println("GOT MQTT") // Parse incoming MQTT message var event MQTTEvent json.Unmarshal(msg.Payload(), &event) @@ -124,9 +125,10 @@ func connectionLostHandler(c mqtt.Client, err error) { // connectHandler logs message on MQTT connection func connectHandler(client mqtt.Client) { log.Println("Connected to MQTT.") - if subscription := client.Subscribe("frigate/events", 0, processEvent); subscription.Wait() && subscription.Error() != nil { - log.Printf("Failed to subscribe to topic frigate/events") + topic := fmt.Sprintf(config.ConfigData.Frigate.MQTT.TopicPrefix + "/events") + if subscription := client.Subscribe(topic, 0, processEvent); subscription.Wait() && subscription.Error() != nil { + log.Printf("Failed to subscribe to topic: %s", topic) time.Sleep(10 * time.Second) } - log.Printf("Subscribed to MQTT topic frigate/events") + log.Printf("Subscribed to MQTT topic: %s", topic) } diff --git a/example-config.yml b/example-config.yml index c283a26..3a116ce 100644 --- a/example-config.yml +++ b/example-config.yml @@ -28,6 +28,8 @@ frigate: # MQTT Authentication. Leave both blank for anonymous username: password: + # Optionally set custom topic prefix (Default: frigate) + topic_prefix: cameras: # List of cameras to exclude from being monitored