diff --git a/go.mod b/go.mod index 12a4241..216b49e 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22 require ( github.com/eclipse/paho.mqtt.golang v1.4.3 github.com/kelseyhightower/envconfig v1.4.0 - github.com/pvainio/vallox-rs485 v0.0.7 + github.com/pvainio/vallox-rs485 v0.0.8 ) require ( diff --git a/go.sum b/go.sum index b14214c..6e03906 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/pvainio/vallox-rs485 v0.0.6 h1:dSFqqoPdMtEf0euSymRH0XNnUOJ9/ECyNP5lpy github.com/pvainio/vallox-rs485 v0.0.6/go.mod h1:xJ4a2TAYmOO7qkl3WhWA2Il85h2bQVeR5Z7WubdjA2U= github.com/pvainio/vallox-rs485 v0.0.7 h1:22lO5i9nePHQyvHzAbKAyxPUdElFF+fMZPY9QOb0+bM= github.com/pvainio/vallox-rs485 v0.0.7/go.mod h1:xJ4a2TAYmOO7qkl3WhWA2Il85h2bQVeR5Z7WubdjA2U= +github.com/pvainio/vallox-rs485 v0.0.8 h1:X/swdTPF0rgZK5MfDsqVEC+pTGdg8vJMnPrbp3/t98c= +github.com/pvainio/vallox-rs485 v0.0.8/go.mod h1:xJ4a2TAYmOO7qkl3WhWA2Il85h2bQVeR5Z7WubdjA2U= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= diff --git a/main.go b/main.go index e0aa7a9..fb1ad7f 100644 --- a/main.go +++ b/main.go @@ -133,6 +133,9 @@ func main() { announceMeToMqttDiscovery(mqtt, cache) + init := time.After(5 * time.Second) + schedule := time.Tick(5 * time.Minute) + for { select { case event := <-valloxDevice.Events(): @@ -153,9 +156,9 @@ func main() { } else if status != "offline" { logInfo.Printf("unknown HA status message %s", status) } - case <-time.Tick(15 * time.Minute): + case <-schedule: queryValues(valloxDevice, cache) - case <-time.After(time.Second): + case <-init: // query initial values queryValues(valloxDevice, cache) } @@ -167,17 +170,20 @@ func handleValloxEvent(valloxDev *vallox.Vallox, e vallox.Event, cache map[byte] return // Ignore values not addressed for me } - logDebug.Printf("received register %d value %d matching %s", e.Register, e.Value, topicMap[e.Register]) + logDebug.Printf("received register %x value %x matching %s", e.Register, e.Value, topicMap[e.Register]) - if val, ok := cache[e.Register]; !ok { + cached, hit := cache[e.Register] + if !hit { // First time we receive this value, send Home Assistant discovery announceRawData(mqtt, e.Register) - } else if val.value.RawValue == e.RawValue && time.Since(val.time) < time.Duration(1)*time.Minute { - // we already have the value and have recently published it, no need to publish to mqtt + } + + if !hit || cached.value.RawValue != e.RawValue || time.Since(cached.time) > time.Duration(1)*time.Minute { + go publishValue(mqtt, e) return } - cached := cacheEntry{time: time.Now(), value: e} + cached = cacheEntry{time: time.Now(), value: e} cache[e.Register] = cached if e.Register == vallox.FanSpeed { @@ -185,7 +191,7 @@ func handleValloxEvent(valloxDev *vallox.Vallox, e vallox.Event, cache map[byte] currentSpeedUpdated = cached.time } - go publishValue(mqtt, cached.value) + queryValues(valloxDev, cache) } func sendSpeed(valloxDevice *vallox.Vallox) { @@ -395,11 +401,11 @@ func publishSelect(mqtt mqttClient.Client, uid string, name string, stateTopic s func publishDiscovery(mqtt mqttClient.Client, etype string, uid string, name string, stateTopic string, cmdTopic string) { discoveryTopic := fmt.Sprintf("homeassistant/%s/%s/config", etype, toUid(uid)) - if _, ok := announced[stateTopic]; ok { + if _, ok := announced[discoveryTopic]; ok { // already announced return } - announced[stateTopic] = true + announced[discoveryTopic] = true msg := discoveryMsg(uid, name, stateTopic, cmdTopic) publish(mqtt, discoveryTopic, msg) }