Skip to content

Commit

Permalink
Allow changing default MQTT topic prefix via config (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2142 committed May 4, 2024
1 parent cf9b8d4 commit f7b0c1a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
13 changes: 7 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 5 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -68,6 +71,7 @@ frigate:
clientid: frigate-notify
username: mqtt-user
password: mqtt-pass
topic_prefix: frigate
```

### Cameras
Expand Down Expand Up @@ -345,6 +349,7 @@ frigate:
clientid:
username:
password:
topic_prefix:
cameras:
exclude:
Expand Down
8 changes: 5 additions & 3 deletions events/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f7b0c1a

Please sign in to comment.