Mqtt: fix panic publishing nil interface

This commit is contained in:
andig 2026-02-20 09:13:31 +01:00
parent 8bc7d50b60
commit c69c7672bd
2 changed files with 9 additions and 1 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/evcc-io/evcc/core/vehicle"
"github.com/evcc-io/evcc/plugin/mqtt"
"github.com/evcc-io/evcc/util"
"github.com/samber/lo"
)
// MQTT is the MQTT server. It uses the MQTT client for publishing.
@ -53,7 +54,7 @@ func NewMQTT(root string, site site.API) (*MQTT, error) {
func (m *MQTT) encode(v any) string {
// nil should erase the value
if v == nil {
if v == nil || lo.IsNil(v) {
return ""
}

View file

@ -97,6 +97,13 @@ func (suite *mqttSuite) TestSlice() {
suite.Equal([]string{"2", "10", "20"}, suite.payloads, "payloads")
}
func (suite *mqttSuite) TestNilInterface() {
var ptr *time.Time
suite.publish("test", false, ptr)
suite.Equal([]string{"test"}, suite.topics, "topics")
suite.Equal([]string{""}, suite.payloads, "payloads")
}
func (suite *mqttSuite) TestMeasurement() {
topics := lo.Map([]string{
"title", "icon", "power", "energy", "powers", "currents", "excessDCPower", "capacity", "soc", "controllable", "forecast",