update
This commit is contained in:
@@ -20,6 +20,7 @@ type HADevice struct {
|
||||
// SwitchDiscovery represents Home Assistant MQTT switch discovery config
|
||||
// Reference: https://www.home-assistant.io/integrations/switch.mqtt/
|
||||
type SwitchDiscovery struct {
|
||||
Platform string `json:"platform"`
|
||||
Name string `json:"name"`
|
||||
CommandTopic string `json:"command_topic"`
|
||||
StateTopic string `json:"state_topic"`
|
||||
@@ -38,6 +39,7 @@ type SwitchDiscovery struct {
|
||||
// SensorDiscovery represents Home Assistant MQTT sensor discovery config
|
||||
// Reference: https://www.home-assistant.io/integrations/sensor.mqtt/
|
||||
type SensorDiscovery struct {
|
||||
Platform string `json:"platform"`
|
||||
Name string `json:"name"`
|
||||
StateTopic string `json:"state_topic"`
|
||||
UniqueID string `json:"unique_id"`
|
||||
@@ -79,6 +81,7 @@ func (c *Client) PublishDeviceDiscovery(deviceID int) error {
|
||||
}
|
||||
|
||||
discovery := SwitchDiscovery{
|
||||
Platform: "mqtt",
|
||||
Name: device.Name,
|
||||
CommandTopic: fmt.Sprintf("telldus/device/%d/set", deviceID),
|
||||
StateTopic: fmt.Sprintf("telldus/device/%d/state", deviceID),
|
||||
@@ -162,6 +165,7 @@ func (c *Client) publishSensorDiscoveryForSensor(sensor *datastore.Sensor, dataT
|
||||
|
||||
if dataTypes&telldus.DataTypeTemperature != 0 && sensor.TemperatureUniqueID != "" {
|
||||
discovery := SensorDiscovery{
|
||||
Platform: "mqtt",
|
||||
Name: fmt.Sprintf("%s Temperature", sensor.Name),
|
||||
StateTopic: fmt.Sprintf("telldus/sensor/%s/%s/%d/temperature", sensor.Protocol, sensor.Model, sensor.ID),
|
||||
UniqueID: sensor.TemperatureUniqueID,
|
||||
@@ -182,6 +186,7 @@ func (c *Client) publishSensorDiscoveryForSensor(sensor *datastore.Sensor, dataT
|
||||
|
||||
if dataTypes&telldus.DataTypeHumidity != 0 && sensor.HumidityUniqueID != "" {
|
||||
discovery := SensorDiscovery{
|
||||
Platform: "mqtt",
|
||||
Name: fmt.Sprintf("%s Humidity", sensor.Name),
|
||||
StateTopic: fmt.Sprintf("telldus/sensor/%s/%s/%d/humidity", sensor.Protocol, sensor.Model, sensor.ID),
|
||||
UniqueID: sensor.HumidityUniqueID,
|
||||
|
||||
65
pkg/mqtt/discovery_test.go
Normal file
65
pkg/mqtt/discovery_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package mqtt
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSensorDiscoveryJSON(t *testing.T) {
|
||||
discovery := SensorDiscovery{
|
||||
Platform: "mqtt",
|
||||
Name: "Test Sensor Temperature",
|
||||
StateTopic: "telldus/sensor/arctech/selflearning/1/temperature",
|
||||
UniqueID: "telldus_sensor_arctech_selflearning_1_temperature",
|
||||
UnitOfMeasurement: "°C",
|
||||
DeviceClass: "temperature",
|
||||
StateClass: "measurement",
|
||||
Device: HADevice{
|
||||
Identifiers: []string{"telldus_sensor_arctech_selflearning_1"},
|
||||
Name: "Test Sensor",
|
||||
Manufacturer: "Telldus",
|
||||
Model: "arctech selflearning",
|
||||
},
|
||||
}
|
||||
|
||||
got, err := json.Marshal(discovery)
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal failed: %v", err)
|
||||
}
|
||||
|
||||
want := `{"platform":"mqtt","name":"Test Sensor Temperature","state_topic":"telldus/sensor/arctech/selflearning/1/temperature","unique_id":"telldus_sensor_arctech_selflearning_1_temperature","device":{"identifiers":["telldus_sensor_arctech_selflearning_1"],"name":"Test Sensor","manufacturer":"Telldus","model":"arctech selflearning"},"unit_of_measurement":"°C","device_class":"temperature","state_class":"measurement"}`
|
||||
if strings.TrimSpace(string(got)) != want {
|
||||
t.Fatalf("unexpected payload\nwant: %s\n got: %s", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSwitchDiscoveryJSON(t *testing.T) {
|
||||
discovery := SwitchDiscovery{
|
||||
Platform: "mqtt",
|
||||
Name: "Test Switch",
|
||||
CommandTopic: "telldus/device/1/set",
|
||||
StateTopic: "telldus/device/1/state",
|
||||
UniqueID: "telldus_device_1",
|
||||
PayloadOn: "ON",
|
||||
PayloadOff: "OFF",
|
||||
StateOn: "ON",
|
||||
StateOff: "OFF",
|
||||
Device: HADevice{
|
||||
Identifiers: []string{"telldus_1"},
|
||||
Name: "Test Switch",
|
||||
Manufacturer: "Telldus",
|
||||
Model: "arctech codeswitch",
|
||||
},
|
||||
}
|
||||
|
||||
got, err := json.Marshal(discovery)
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal failed: %v", err)
|
||||
}
|
||||
|
||||
want := `{"platform":"mqtt","name":"Test Switch","command_topic":"telldus/device/1/set","state_topic":"telldus/device/1/state","unique_id":"telldus_device_1","device":{"identifiers":["telldus_1"],"name":"Test Switch","manufacturer":"Telldus","model":"arctech codeswitch"},"payload_on":"ON","payload_off":"OFF","state_on":"ON","state_off":"OFF"}`
|
||||
if strings.TrimSpace(string(got)) != want {
|
||||
t.Fatalf("unexpected payload\nwant: %s\n got: %s", want, got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user