Files
go-telldus-matter/pkg/mqtt/discovery_test.go
Mats Tornberg 82a32ed287 update
2025-11-23 11:09:18 +00:00

66 lines
2.3 KiB
Go

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)
}
}