-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for Zigbee temperature sensors
- Loading branch information
1 parent
972bf3e
commit f9fc89e
Showing
3 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters