From 9a3c5a67b2fb1919d403ae3c30bd8a546a9d4c61 Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 19 Aug 2025 14:24:15 +0200 Subject: [PATCH] Shelly: fix tests (#23096) --- meter/shelly/gen2.go | 17 +++++------------ meter/shelly/gen2_test.go | 26 ++++++++++---------------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/meter/shelly/gen2.go b/meter/shelly/gen2.go index 79f197cb7..58209b2e1 100644 --- a/meter/shelly/gen2.go +++ b/meter/shelly/gen2.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "slices" - "strings" "time" "github.com/evcc-io/evcc/util" @@ -326,20 +325,14 @@ func (c *gen2) getAddOnSwitchId(channel int) (int, error) { return channel, err } - return parseAddOnSwitchID(channel, res) + return parseAddOnSwitchID(channel, res), nil } -func parseAddOnSwitchID(channel int, res Gen2ProAddOnGetPeripherals) (int, error) { - for key := range res.DigitalOut { - if strings.HasPrefix(key, "switch:") { - var id int - if _, err := fmt.Sscanf(key, "switch:%d", &id); err != nil { - return 0, fmt.Errorf("failed to get add-on switch id: %w", err) - } - return id, nil - } +func parseAddOnSwitchID(channel int, res Gen2ProAddOnGetPeripherals) int { + if _, ok := res.DigitalOut["switch:100"]; ok { + return 100 } // if no switch ID is found, return the channel as default - return channel, nil + return channel } diff --git a/meter/shelly/gen2_test.go b/meter/shelly/gen2_test.go index 91b2661c1..678b929ac 100644 --- a/meter/shelly/gen2_test.go +++ b/meter/shelly/gen2_test.go @@ -47,36 +47,30 @@ func TestUnmarshalGen2StatusResponse(t *testing.T) { jsonstr := `{"digital_out":{"switch:100":{}}}` require.NoError(t, json.Unmarshal([]byte(jsonstr), &res)) assert.NotEmpty(t, res.DigitalOut) - id, err := parseAddOnSwitchID(channel, res) - require.NoError(t, err) - assert.Equal(t, 100, id) + assert.Equal(t, 100, parseAddOnSwitchID(channel, res)) + // Test with no AddOn installed res = Gen2ProAddOnGetPeripherals{} jsonstr = `{"code":404,"message":"No handler for ProOutputAddon.GetPeripherals"}` require.NoError(t, json.Unmarshal([]byte(jsonstr), &res)) - id, err = parseAddOnSwitchID(channel, res) - require.NoError(t, err) - assert.Equal(t, 0, id) + assert.Equal(t, 0, parseAddOnSwitchID(channel, res)) + // Test for empty digital_out map in AddOn response res = Gen2ProAddOnGetPeripherals{} jsonstr = `{"digital_out":{}}` require.NoError(t, json.Unmarshal([]byte(jsonstr), &res)) - id, err = parseAddOnSwitchID(channel, res) - require.NoError(t, err) - assert.Equal(t, 0, id) + assert.Equal(t, 0, parseAddOnSwitchID(channel, res)) + // Test with multiple AddOns installed (only the first ID will be returned) res = Gen2ProAddOnGetPeripherals{} jsonstr = `{"digital_out":{"switch:100":{},"switch:101":{}}}` require.NoError(t, json.Unmarshal([]byte(jsonstr), &res)) - id, err = parseAddOnSwitchID(channel, res) - require.NoError(t, err) - assert.Equal(t, 100, id) - // Test for malformed switch keys + assert.Equal(t, 100, parseAddOnSwitchID(channel, res)) + + // Test for switch key <> 100 res = Gen2ProAddOnGetPeripherals{} jsonstr = `{"digital_out":{"switch:abc":{}}}` require.NoError(t, json.Unmarshal([]byte(jsonstr), &res)) - id, err = parseAddOnSwitchID(channel, res) - require.ErrorContains(t, err, "failed to get add-on switch id:") - assert.Equal(t, 0, id) + assert.Equal(t, 0, parseAddOnSwitchID(channel, res)) } }