Skip to content

Commit

Permalink
feat: fatal on mqtt disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
mewejo committed Mar 2, 2023
1 parent d9bb436 commit 7712dc3
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/hass/hass_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hass
import (
"encoding/json"
"fmt"
"log"
"os"
"time"

Expand Down Expand Up @@ -67,6 +68,13 @@ func (c *HassClient) Disconnect() {
c.client.Disconnect(500)
}

func (c *HassClient) handleLostConnection(client mqtt.Client, err error) {
log.Fatal(fmt.Sprintf(
"MQTT connection lost: %s",
err,
))
}

func (c *HassClient) Connect(lwt MqttMessage) error {
connectionString := fmt.Sprintf(
"tcp://%v:%v",
Expand All @@ -82,6 +90,7 @@ func (c *HassClient) Connect(lwt MqttMessage) error {
opts.SetPassword(os.Getenv("MQTT_PASSWORD"))
opts.SetKeepAlive(2 * time.Second)
opts.SetPingTimeout(1 * time.Second)
opts.OnConnectionLost = c.handleLostConnection

mqttClient := mqtt.NewClient(opts)

Expand Down

0 comments on commit 7712dc3

Please sign in to comment.