Shelly: fix tests (#23096)
This commit is contained in:
parent
9e1addb6d5
commit
9a3c5a67b2
2 changed files with 15 additions and 28 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue