1
0
Fork 0

Clean state for use in subject string

main
Jordan Hotmann 2023-12-18 12:40:45 -07:00
parent 5a23359294
commit e86694c57b
No known key found for this signature in database
GPG Key ID: 01B504170C2A2EA3
1 changed files with 5 additions and 2 deletions

View File

@ -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)