Skip to content

Commit

Permalink
fix: added config example
Browse files Browse the repository at this point in the history
  • Loading branch information
pvainio committed May 15, 2024
1 parent ee4d6a8 commit 5cba64a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 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.5
github.com/pvainio/vallox-rs485 v0.0.7
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/pvainio/vallox-rs485 v0.0.4 h1:nvlCa9arfJMlrxcUsgev1NnabekHgk6C8Jft+WQ6qjY=
github.com/pvainio/vallox-rs485 v0.0.4/go.mod h1:gGg7X93MjVm/hZr1igmHm1QOHp3epgX95mCaWt7/p04=
github.com/pvainio/vallox-rs485 v0.0.5 h1:tZFFRM0rp2/fsE0gqJJfI2kfh4W+uw2fG9erAcoxVMI=
github.com/pvainio/vallox-rs485 v0.0.5/go.mod h1:xJ4a2TAYmOO7qkl3WhWA2Il85h2bQVeR5Z7WubdjA2U=
github.com/pvainio/vallox-rs485 v0.0.6 h1:dSFqqoPdMtEf0euSymRH0XNnUOJ9/ECyNP5lpy6CWyA=
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/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
59 changes: 37 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const (
topicTempIncomingOutside = "temp/incoming/outside"
topicTempOutgoingInside = "temp/outgoing/inside"
topicTempOutgoingOutside = "temp/outgoing/outside"
topicRhHighest = "rh/highest"
topicRh1 = "rh/sensor1"
topicRh2 = "rh/sensor2"
topicCo2Highest = "co2/highest"
topicRaw = "raw/%x"
)

Expand All @@ -38,6 +42,11 @@ var topicMapOld = map[byte]string{
vallox.TempIncomingOutside: topicTempIncomingOutside,
vallox.TempOutgoingInside: topicTempOutgoingInside,
vallox.TempOutgoingOutside: topicTempOutgoingOutside,
// vallox.RhHighest: topicRhHighest,
// vallox.Rh1: topicRh1,
// vallox.Rh2: topicRh2,
// vallox.Co2HighestHighByte: topicCo2Highest,
// vallox.Co2HighestLowByte: topicCo2Highest,
}

// newer protocol?
Expand All @@ -47,10 +56,17 @@ var topicMapNew = map[byte]string{
vallox.TempIncomingOutsideNew: topicTempIncomingOutside,
vallox.TempOutgoingInsideNew: topicTempOutgoingInside,
vallox.TempOutgoingOutsideNew: topicTempOutgoingOutside,
// vallox.RhHighest: topicRhHighest,
// vallox.Rh1: topicRh1,
// vallox.Rh2: topicRh2,
// vallox.Co2HighestHighByte: topicCo2Highest,
// vallox.Co2HighestLowByte: topicCo2Highest,
}

var topicMap map[byte]string

var announced map[string]any

type Config struct {
SerialDevice string `envconfig:"serial_device" required:"true"`
MqttUrl string `envconfig:"mqtt_url" required:"true"`
Expand Down Expand Up @@ -116,10 +132,6 @@ func main() {
cache := make(map[byte]cacheEntry)

announceMeToMqttDiscovery(mqtt, cache)
// query some initial values
queryValues(valloxDevice)

ticker := time.NewTicker(300 * time.Second)

for {
select {
Expand All @@ -138,24 +150,18 @@ func main() {
if status == "online" {
// HA became online, send discovery so it knows about entities
go announceMeToMqttDiscovery(mqtt, cache)
// resend some data
queryValues(valloxDevice)
} else if status != "offline" {
logInfo.Printf("unknown HA status message %s", status)
}
case <-ticker.C:
queryValues(valloxDevice)
case <-time.Tick(15 * time.Minute):
queryValues(valloxDevice, cache)
case <-time.After(time.Second):
// query initial values
queryValues(valloxDevice, cache)
}
}
}

func queryValues(valloxDevice *vallox.Vallox) {
logDebug.Printf("scheduled query values")
for register := range topicMap {
valloxDevice.Query(register)
}
}

func handleValloxEvent(valloxDev *vallox.Vallox, e vallox.Event, cache map[byte]cacheEntry, mqtt mqttClient.Client) {
if !valloxDev.ForMe(e) {
return // Ignore values not addressed for me
Expand All @@ -166,10 +172,8 @@ func handleValloxEvent(valloxDev *vallox.Vallox, e vallox.Event, cache map[byte]
if val, ok := cache[e.Register]; !ok {
// 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(15)*time.Minute {
// Some values are not published by the device, so manually republish to keep the device online
resendOldValues(valloxDev, cache)
// we already have that value and have recently published it, no need to publish to mqtt
} 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
return
}

Expand Down Expand Up @@ -272,12 +276,16 @@ func subscribe(mqtt mqttClient.Client) {
mqtt.Subscribe(topic(topicFanSpeedSet), 0, changeSpeedMessage)
}

func resendOldValues(device *vallox.Vallox, cache map[byte]cacheEntry) {
func queryValues(device *vallox.Vallox, cache map[byte]cacheEntry) {
// Speed is not automatically published by Vallox, so manually refresh the value
logDebug.Printf("scheduled register query")
now := time.Now()
validTime := now.Add(time.Duration(-15) * time.Minute)
if cached, ok := cache[vallox.FanSpeed]; (ok && cached.time.Before(validTime)) || !ok {
device.Query(vallox.FanSpeed)
for register, _ := range topicMap {
if cached, ok := cache[register]; !ok || cached.time.Before(validTime) {
// more than 15min old, query it
device.Query(register)
}
}
}

Expand Down Expand Up @@ -353,6 +361,8 @@ func discoveryMsg(uid string, name string, stateTopic string, commandTopic strin
}

func announceMeToMqttDiscovery(mqtt mqttClient.Client, cache map[byte]cacheEntry) {
announced = make(map[string]any)

publishSensor(mqtt, "fan_speed", "speed", topicFanSpeed)
publishSelect(mqtt, "fan_select", "speed select", topicFanSpeed, topicFanSpeedSet)
publishSensor(mqtt, "temp_incoming_outside", "outdoor temperature", topicTempIncomingOutside)
Expand Down Expand Up @@ -385,6 +395,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 {
// already announced
return
}
announced[stateTopic] = true
msg := discoveryMsg(uid, name, stateTopic, cmdTopic)
publish(mqtt, discoveryTopic, msg)
}
Expand Down

0 comments on commit 5cba64a

Please sign in to comment.