From 7712dc39aa98e53aaac196f55ae0df39b3989eec Mon Sep 17 00:00:00 2001 From: Josh Bonfield Date: Thu, 2 Mar 2023 19:24:47 +0000 Subject: [PATCH] feat: fatal on mqtt disconnection --- pkg/hass/hass_client.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/hass/hass_client.go b/pkg/hass/hass_client.go index 18eb137..122f6d1 100644 --- a/pkg/hass/hass_client.go +++ b/pkg/hass/hass_client.go @@ -3,6 +3,7 @@ package hass import ( "encoding/json" "fmt" + "log" "os" "time" @@ -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", @@ -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)