Clean state for use in subject string
parent
5a23359294
commit
e86694c57b
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"log/slog"
|
||||
|
@ -142,11 +143,13 @@ func handleMessages() {
|
|||
logger.Error("Error marshalling event data", "error", marshallErr)
|
||||
}
|
||||
go nats.SetKeyValueString(fmt.Sprintf("homeassistant.states.%s", message.Event.Data.EntityId), message.Event.Data.NewState.State)
|
||||
stateReplacer := strings.NewReplacer(" ", "_", ".", ",", "*", "_", ">", "_")
|
||||
cleanedState := stateReplacer.Replace(message.Event.Data.NewState.State)
|
||||
if message.Event.Data.NewState.State == message.Event.Data.OldState.State {
|
||||
logger.Debug("State unchanged, publishing to attributes topic")
|
||||
nats.Publish(fmt.Sprintf("homeassistant.attributues.%s.%s", message.Event.Data.EntityId, message.Event.Data.NewState.State), data)
|
||||
nats.Publish(fmt.Sprintf("homeassistant.attributues.%s.%s", message.Event.Data.EntityId, cleanedState), data)
|
||||
} else {
|
||||
nats.PublishRequest(fmt.Sprintf("homeassistant.states.%s.%s", message.Event.Data.EntityId, message.Event.Data.NewState.State), data, defaultTimeout, 2)
|
||||
nats.PublishRequest(fmt.Sprintf("homeassistant.states.%s.%s", message.Event.Data.EntityId, cleanedState), data, defaultTimeout, 2)
|
||||
}
|
||||
case zhaEventId:
|
||||
data, _ := json.Marshal(message.Event.Data)
|
||||
|
|
Loading…
Reference in New Issue