Clean state for use in subject string
parent
5a23359294
commit
e86694c57b
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
@ -142,11 +143,13 @@ func handleMessages() {
|
||||||
logger.Error("Error marshalling event data", "error", marshallErr)
|
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)
|
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 {
|
if message.Event.Data.NewState.State == message.Event.Data.OldState.State {
|
||||||
logger.Debug("State unchanged, publishing to attributes topic")
|
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 {
|
} 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:
|
case zhaEventId:
|
||||||
data, _ := json.Marshal(message.Event.Data)
|
data, _ := json.Marshal(message.Event.Data)
|
||||||
|
|
Loading…
Reference in New Issue