Skip to content

Commit

Permalink
Homematic: better error messages (evcc-io#6835)
Browse files Browse the repository at this point in the history
  • Loading branch information
thierolm authored Mar 15, 2023
1 parent 4cf901f commit 10056d0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
29 changes: 24 additions & 5 deletions meter/homematic/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ func (c *Connection) XmlCmd(method, channel string, values ...Param) (MethodResp
return hmr, err
}

if strings.Contains(string(res), "faultCode") {
return hmr, fmt.Errorf("ccu: %s", string(res))
}

// correct Homematic IP Legacy API (CCU port 2010) method response encoding value
res = []byte(strings.Replace(string(res), "ISO-8859-1", "UTF-8", 1))

Expand All @@ -98,7 +94,7 @@ func (c *Connection) XmlCmd(method, channel string, values ...Param) (MethodResp
return hmr, err
}

return hmr, err
return hmr, parseError(hmr)
}

// Initialze CCU methods via system.listMethods call
Expand Down Expand Up @@ -149,3 +145,26 @@ func (c *Connection) GridTotalEnergy() (float64, error) {
res, err := c.XmlCmd("getValue", c.MeterChannel, Param{CCUString: "IEC_ENERGY_COUNTER"})
return res.Value.CCUFloat, err
}

// parseError checks on Homematic CCU error codes
// Refer to page 30 of https://homematic-ip.com/sites/default/files/downloads/HM_XmlRpc_API.pdf
func parseError(res MethodResponse) error {
var faultCode int64
var faultString string

faultCode = 0
for _, f := range res.Fault {
if f.Name == "faultCode" {
faultCode = f.Value.CCUInt
}
if f.Name == "faultString" {
faultString = f.Value.CCUString
}
}

if faultCode != 0 {
return fmt.Errorf("%s (%v)", faultString, faultCode)
}

return nil
}
12 changes: 12 additions & 0 deletions meter/homematic/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ type MethodCall struct {
Params []Param `xml:"params>param,omitempty"`
}

type Member struct {
Name string `xml:"name,omitempty"`
Value FaultValue `xml:"value,omitempty"`
}

type FaultValue struct {
XMLName xml.Name `xml:"value"`
CCUString string `xml:",chardata"`
CCUInt int64 `xml:"i4,omitempty"`
}

type MethodResponse struct {
XMLName xml.Name `xml:"methodResponse"`
Value Param `xml:"params>param,omitempty"`
Fault []Member `xml:"fault>value>struct>member,omitempty"`
}
37 changes: 37 additions & 0 deletions meter/homematic/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package homematic

import (
"encoding/xml"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

// Test MethodResponse response
func TestUnmarshalMethodResponse(t *testing.T) {

{
// Double response test
var res MethodResponse

xmlstr := `<?xml version="1.0" encoding="ISO-8859-1"?><methodResponse><params><param><value><double>20698.0</double></value></param></params></methodResponse>`
assert.NoError(t, xml.Unmarshal([]byte(strings.Replace(string(xmlstr), "ISO-8859-1", "UTF-8", 1)), &res))

assert.Equal(t, float64(20698), res.Value.CCUFloat)
}

{
// Faulty response test
var res MethodResponse

xmlstr := `<?xml version="1.0" encoding="ISO-8859-1"?><methodResponse><fault><value><struct><member><name>faultCode</name><value><i4>-2</i4></value></member><member><name>faultString</name><value>Invalid device</value></member></struct></value></fault></methodResponse>`
assert.NoError(t, xml.Unmarshal([]byte(strings.Replace(string(xmlstr), "ISO-8859-1", "UTF-8", 1)), &res))

assert.Equal(t, "faultCode", res.Fault[0].Name)
assert.Equal(t, int64(-2), res.Fault[0].Value.CCUInt)
assert.Equal(t, "faultString", res.Fault[1].Name)
assert.Equal(t, "Invalid device", res.Fault[1].Value.CCUString)
}

}

0 comments on commit 10056d0

Please sign in to comment.