Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pvainio committed Jun 17, 2024
1 parent 5cba64a commit 9ebf987
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
26 changes: 16 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)
}
Expand All @@ -167,25 +170,28 @@ 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 {
currentSpeed = byte(e.Value)
currentSpeedUpdated = cached.time
}

go publishValue(mqtt, cached.value)
queryValues(valloxDev, cache)
}

func sendSpeed(valloxDevice *vallox.Vallox) {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 9ebf987

Please sign in to comment.