Skip to content

Commit

Permalink
add support for Zigbee temperature sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilGruber committed Jun 28, 2024
1 parent 972bf3e commit f9fc89e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
11 changes: 5 additions & 6 deletions devices/temperature.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func MakeTemperature(config core.DeviceConfig) Temperature {
return t
}

func NewTemperature(config core.DeviceConfig) *Temperature {
s := MakeTemperature(config)
return &s
}

func (t *Temperature) GetMin() int {
return 0
}
Expand All @@ -28,11 +33,6 @@ func (t *Temperature) GetMax() int {
return 99
}

func NewTemperature(config core.DeviceConfig) *Temperature {
s := MakeTemperature(config)
return &s
}

func (t *Temperature) PublishValue(mqtt mqtt.Client) {
}

Expand All @@ -55,7 +55,6 @@ func (t *Temperature) GetMessageHandler(channel chan core.SwitchRequest, tempera
if err != nil {
log.Println("Received invalid temperature " + payload[:] + ": " + err.Error())
return

}
t.setCurrent(temperature)
}
Expand Down
51 changes: 51 additions & 0 deletions devices/ztemperature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package devices

import (
"encoding/json"
"github.com/PhilGruber/dimmy/core"
mqtt "github.com/eclipse/paho.mqtt.golang"
"log"
)

func MakeZTemperature(config core.DeviceConfig) ZTemperature {
t := ZTemperature{}
t.Name = config.Name
t.MqttTopic = config.Topic

t.Current = 0
t.Humidity = 0
t.Type = "temperature"
return t
}

func NewZTemperature(config core.DeviceConfig) *ZTemperature {
t := MakeZTemperature(config)
return &t
}

type ZTemperature struct {
Temperature
Humidity float64
}

type ZTemperatureMessage struct {
core.Zigbee2MqttMessage
Temperature float64 `json:"temperature"`
Humidity float64 `json:"humidity"`
}

func (t *ZTemperature) GetMessageHandler(channel chan core.SwitchRequest, temperature DeviceInterface) mqtt.MessageHandler {
log.Printf("Subscribing to temperature sensor on %s\n", t.MqttTopic)
return func(client mqtt.Client, mqttMessage mqtt.Message) {
payload := mqttMessage.Payload()
var data ZTemperatureMessage
err := json.Unmarshal(payload, &data)
if err != nil {
log.Printf("Received invalid json payload from %s: %v\n\tError: %s ", t.MqttTopic, payload, err.Error())
return
}
log.Printf("Received new temperature: %.2f Humidity: %.2f", data.Temperature, data.Humidity)
t.setCurrent(data.Temperature)
t.Humidity = data.Humidity
}
}
5 changes: 5 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ func main() {
devices[deviceConfig.Name] = dimmyDevices.NewPlug(deviceConfig)
case "temperature":
devices[deviceConfig.Name] = dimmyDevices.NewTemperature(deviceConfig)
<<<<<<< Updated upstream
case "thermostat":
devices[deviceConfig.Name] = dimmyDevices.NewThermostat(deviceConfig)
=======
case "ztemperature":
devices[deviceConfig.Name] = dimmyDevices.NewZTemperature(deviceConfig)
>>>>>>> Stashed changes
case "group":
default:
log.Println("Skipping deviceConfig of unknown type '" + deviceConfig.Type + "'")
Expand Down

0 comments on commit f9fc89e

Please sign in to comment.