Add read-only mode support and enhance logging throughout the application
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2026-02-19 12:36:52 +11:00
parent bdcb8e6f73
commit 1c15ff5911
8 changed files with 281 additions and 33 deletions

View File

@@ -99,6 +99,17 @@ type panelStateCache struct {
// New creates an MQTT client that publishes MK2 updates and optionally handles setting write commands.
func New(mk2 mk2driver.Mk2, writer mk2driver.SettingsWriter, config Config) error {
log.WithFields(logrus.Fields{
"broker": config.Broker,
"client_id": config.ClientID,
"topic": config.Topic,
"command_topic": config.CommandTopic,
"status_topic": config.StatusTopic,
"ha_enabled": config.HomeAssistant.Enabled,
"ha_node_id": config.HomeAssistant.NodeID,
"ha_device_name": config.HomeAssistant.DeviceName,
}).Info("Initializing MQTT client")
c := mqtt.NewClient(getOpts(config))
if token := c.Connect(); token.Wait() && token.Error() != nil {
return token.Error()
@@ -135,14 +146,18 @@ func New(mk2 mk2driver.Mk2, writer mk2driver.SettingsWriter, config Config) erro
go func() {
for e := range mk2.C() {
if e == nil || !e.Valid {
log.Debug("Skipping invalid/nil MK2 event for MQTT publish")
continue
}
if err := publishJSON(c, config.Topic, e, 0, false); err != nil {
log.Errorf("Could not publish update to MQTT topic %q: %v", config.Topic, err)
} else {
log.WithField("topic", config.Topic).Debug("Published MK2 update to MQTT")
}
}
}()
log.Info("MQTT client setup complete")
return nil
}
@@ -152,9 +167,14 @@ func subscribeHAPanelModeState(client mqtt.Client, config Config, cache *panelSt
}
stateTopic := haPanelSwitchStateTopic(config)
log.WithField("topic", stateTopic).Info("Subscribing to Home Assistant mode state topic for panel cache")
t := client.Subscribe(stateTopic, 1, func(_ mqtt.Client, msg mqtt.Message) {
switchState, switchName, err := normalizePanelSwitch(string(msg.Payload()))
if err != nil {
log.WithFields(logrus.Fields{
"topic": msg.Topic(),
"payload": string(msg.Payload()),
}).WithError(err).Warn("Ignoring invalid Home Assistant mode state payload")
return
}
cache.remember(writeCommand{
@@ -163,6 +183,10 @@ func subscribeHAPanelModeState(client mqtt.Client, config Config, cache *panelSt
SwitchState: switchState,
SwitchName: switchName,
})
log.WithFields(logrus.Fields{
"topic": msg.Topic(),
"switch_name": switchName,
}).Debug("Updated panel mode cache from Home Assistant state topic")
})
t.Wait()
return t.Error()
@@ -174,6 +198,11 @@ func commandHandler(client mqtt.Client, writer mk2driver.SettingsWriter, config
}
return func(_ mqtt.Client, msg mqtt.Message) {
log.WithFields(logrus.Fields{
"topic": msg.Topic(),
"payload": string(msg.Payload()),
}).Debug("Received MQTT command message")
cmd, err := decodeWriteCommand(msg.Payload())
if err != nil {
log.Errorf("Invalid MQTT write command payload from topic %q: %v", msg.Topic(), err)
@@ -247,6 +276,7 @@ func (c *panelStateCache) resolvePanelCommand(cmd writeCommand) (writeCommand, e
cmd.HasSwitch = true
cmd.SwitchName = c.switchName
cmd.SwitchState = c.switchState
log.WithField("switch", cmd.SwitchName).Debug("Resolved panel command switch from cache")
return cmd, nil
}
@@ -260,6 +290,7 @@ func (c *panelStateCache) remember(cmd writeCommand) {
c.switchName = cmd.SwitchName
c.switchState = cmd.SwitchState
c.mu.Unlock()
log.WithField("switch", cmd.SwitchName).Debug("Remembered panel switch state in cache")
}
func decodeWriteCommand(payload []byte) (writeCommand, error) {
@@ -465,6 +496,7 @@ func decodeStandbyValue(msg writeCommandPayload) (bool, error) {
func publishWriteStatus(client mqtt.Client, topic string, status writeStatus) {
if topic == "" {
log.Debug("Skipping write status publish; status topic is empty")
return
}
if err := publishJSON(client, topic, status, 1, false); err != nil {
@@ -479,6 +511,11 @@ func publishHADiscovery(client mqtt.Client, config Config) error {
for _, def := range definitions {
topic := fmt.Sprintf("%s/%s/%s/%s/config", prefix, def.Component, nodeID, def.ObjectID)
log.WithFields(logrus.Fields{
"topic": topic,
"component": def.Component,
"object_id": def.ObjectID,
}).Debug("Publishing Home Assistant discovery definition")
if err := publishJSON(client, topic, def.Config, 1, true); err != nil {
return fmt.Errorf("could not publish discovery for %s/%s: %w", def.Component, def.ObjectID, err)
}
@@ -665,6 +702,12 @@ func publishJSON(client mqtt.Client, topic string, payload any, qos byte, retain
if t.Error() != nil {
return t.Error()
}
log.WithFields(logrus.Fields{
"topic": topic,
"qos": qos,
"retained": retained,
"bytes": len(data),
}).Debug("Published JSON message")
return nil
}
@@ -679,6 +722,12 @@ func publishString(client mqtt.Client, topic, payload string, qos byte, retained
if t.Error() != nil {
return t.Error()
}
log.WithFields(logrus.Fields{
"topic": topic,
"qos": qos,
"retained": retained,
"payload": payload,
}).Debug("Published string message")
return nil
}