From 9eed7cd13fc0142558dd9150af25306506ab66f1 Mon Sep 17 00:00:00 2001
From: Markus Thierolf <77847348+thierolm@users.noreply.github.com>
Date: Mon, 1 May 2023 12:40:48 +0200
Subject: [PATCH] Homematic: reduce api requests to relax CCU duty cycle
(#7735)
---
charger/homematic.go | 13 ++-
meter/homematic.go | 13 ++-
meter/homematic/connection.go | 148 +++++++++++++++++++++++-----------
meter/homematic/types.go | 40 ++++-----
meter/homematic/types_test.go | 31 ++++++-
5 files changed, 168 insertions(+), 77 deletions(-)
diff --git a/charger/homematic.go b/charger/homematic.go
index aa06a6448..30c547f33 100644
--- a/charger/homematic.go
+++ b/charger/homematic.go
@@ -1,6 +1,8 @@
package charger
import (
+ "time"
+
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/meter/homematic"
"github.com/evcc-io/evcc/util"
@@ -18,7 +20,7 @@ func init() {
// NewCCUFromConfig creates a Homematic charger from generic config
func NewCCUFromConfig(other map[string]interface{}) (api.Charger, error) {
- var cc struct {
+ cc := struct {
embed `mapstructure:",squash"`
URI string
Device string
@@ -27,18 +29,21 @@ func NewCCUFromConfig(other map[string]interface{}) (api.Charger, error) {
User string
Password string
StandbyPower float64
+ Cache time.Duration
+ }{
+ Cache: time.Second,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
- return NewCCU(cc.embed, cc.URI, cc.Device, cc.MeterChannel, cc.SwitchChannel, cc.User, cc.Password, cc.StandbyPower)
+ return NewCCU(cc.embed, cc.URI, cc.Device, cc.MeterChannel, cc.SwitchChannel, cc.User, cc.Password, cc.StandbyPower, cc.Cache)
}
// NewCCU creates a new connection with standbypower for charger
-func NewCCU(embed embed, uri, deviceid, meterid, switchid, user, password string, standbypower float64) (*CCU, error) {
- conn, err := homematic.NewConnection(uri, deviceid, meterid, switchid, user, password)
+func NewCCU(embed embed, uri, deviceid, meterid, switchid, user, password string, standbypower float64, cache time.Duration) (*CCU, error) {
+ conn, err := homematic.NewConnection(uri, deviceid, meterid, switchid, user, password, cache)
c := &CCU{
conn: conn,
diff --git a/meter/homematic.go b/meter/homematic.go
index 0b417f9f5..36aec4996 100644
--- a/meter/homematic.go
+++ b/meter/homematic.go
@@ -1,6 +1,8 @@
package meter
import (
+ "time"
+
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/meter/homematic"
"github.com/evcc-io/evcc/util"
@@ -18,7 +20,7 @@ func init() {
// NewCCUFromConfig creates a Homematic meter from generic config
func NewCCUFromConfig(other map[string]interface{}) (api.Meter, error) {
- var cc struct {
+ cc := struct {
URI string
Device string
MeterChannel string
@@ -26,18 +28,21 @@ func NewCCUFromConfig(other map[string]interface{}) (api.Meter, error) {
User string
Password string
Usage string
+ Cache time.Duration
+ }{
+ Cache: time.Second,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
- return NewCCU(cc.URI, cc.Device, cc.MeterChannel, cc.SwitchChannel, cc.User, cc.Password, cc.Usage)
+ return NewCCU(cc.URI, cc.Device, cc.MeterChannel, cc.SwitchChannel, cc.User, cc.Password, cc.Usage, cc.Cache)
}
// NewCCU creates a new connection with usage for meter
-func NewCCU(uri, deviceid, meterid, switchid, user, password, usage string) (*CCU, error) {
- conn, err := homematic.NewConnection(uri, deviceid, meterid, switchid, user, password)
+func NewCCU(uri, deviceid, meterid, switchid, user, password, usage string, cache time.Duration) (*CCU, error) {
+ conn, err := homematic.NewConnection(uri, deviceid, meterid, switchid, user, password, cache)
m := &CCU{
conn: conn,
diff --git a/meter/homematic/connection.go b/meter/homematic/connection.go
index 30f0f854d..691789dc0 100644
--- a/meter/homematic/connection.go
+++ b/meter/homematic/connection.go
@@ -5,7 +5,9 @@ import (
"fmt"
"net/http"
"strings"
+ "time"
+ "github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
@@ -18,6 +20,7 @@ import (
// Homematic CCU settings
type Settings struct {
URI, Device, MeterChannel, SwitchChannel, User, Password string
+ Cache time.Duration
}
// Connection is the Homematic CCU connection
@@ -25,10 +28,12 @@ type Connection struct {
log *util.Logger
*request.Helper
*Settings
+ meterCache provider.Cacheable[MethodResponse]
+ switchCache provider.Cacheable[MethodResponse]
}
// NewConnection creates a new Homematic device connection.
-func NewConnection(uri, device, meterchannel, switchchannel, user, password string) (*Connection, error) {
+func NewConnection(uri, device, meterchannel, switchchannel, user, password string, cache time.Duration) (*Connection, error) {
log := util.NewLogger("homematic")
settings := &Settings{
@@ -36,6 +41,7 @@ func NewConnection(uri, device, meterchannel, switchchannel, user, password stri
Device: device,
MeterChannel: meterchannel,
SwitchChannel: switchchannel,
+ Cache: cache,
}
conn := &Connection{
@@ -51,13 +57,82 @@ func NewConnection(uri, device, meterchannel, switchchannel, user, password stri
conn.Client.Transport = transport.BasicAuth(user, password, conn.Client.Transport)
}
- if err := conn.Init(); err != nil {
- return conn, err
- }
+ conn.switchCache = provider.ResettableCached(func() (MethodResponse, error) {
+ return conn.XmlCmd("getParamset", conn.SwitchChannel, Param{CCUString: "VALUES"})
+ }, conn.Cache)
+
+ conn.meterCache = provider.ResettableCached(func() (MethodResponse, error) {
+ return conn.XmlCmd("getParamset", conn.MeterChannel, Param{CCUString: "VALUES"})
+ }, conn.Cache)
return conn, nil
}
+// Enable sets the homematic HMIP-PSM switchchannel state to true=on/false=off
+func (c *Connection) Enable(enable bool) error {
+ onoff := map[bool]string{true: "1", false: "0"}
+ _, err := c.XmlCmd("setValue", c.SwitchChannel, Param{CCUString: "STATE"}, Param{CCUBool: onoff[enable]})
+ if err == nil {
+ c.switchCache.Reset()
+ c.meterCache.Reset()
+ }
+ return err
+}
+
+// Enabled reads the homematic HMIP-PSM switchchannel state true=on/false=off
+func (c *Connection) Enabled() (bool, error) {
+ res, err := c.switchCache.Get()
+ if err != nil {
+ return false, err
+ }
+ return getBoolValue(res, "STATE"), nil
+}
+
+// CurrentPower reads the homematic HMIP-PSM meterchannel power in W
+func (c *Connection) CurrentPower() (float64, error) {
+ res, err := c.meterCache.Get()
+ if err != nil {
+ return 0, err
+ }
+ return getFloatValue(res, "POWER"), nil
+}
+
+// TotalEnergy reads the homematic HMIP-PSM meterchannel energy in Wh
+func (c *Connection) TotalEnergy() (float64, error) {
+ res, err := c.meterCache.Get()
+ if err != nil {
+ return 0, err
+ }
+ return getFloatValue(res, "ENERGY_COUNTER") / 1e3, nil
+}
+
+// Currents reads the homematic HMIP-PSM meterchannel L1 current in A
+func (c *Connection) Currents() (float64, float64, float64, error) {
+ res, err := c.meterCache.Get()
+ if err != nil {
+ return 0, 0, 0, err
+ }
+ return getFloatValue(res, "CURRENT") / 1e3, 0, 0, nil
+}
+
+// GridCurrentPower reads the homematic HM-ES-TX-WM grid meterchannel power in W
+func (c *Connection) GridCurrentPower() (float64, error) {
+ res, err := c.meterCache.Get()
+ if err != nil {
+ return 0, err
+ }
+ return getFloatValue(res, "IEC_POWER"), nil
+}
+
+// GridTotalEnergy reads the homematic HM-ES-TX-WM grid meterchannel energy in kWh
+func (c *Connection) GridTotalEnergy() (float64, error) {
+ res, err := c.meterCache.Get()
+ if err != nil {
+ return 0, err
+ }
+ return getFloatValue(res, "IEC_ENERGY_COUNTER"), nil
+}
+
func (c *Connection) XmlCmd(method, channel string, values ...Param) (MethodResponse, error) {
target := fmt.Sprintf("%s:%s", c.Device, channel)
hmc := MethodCall{
@@ -97,53 +172,26 @@ func (c *Connection) XmlCmd(method, channel string, values ...Param) (MethodResp
return hmr, parseError(hmr)
}
-// Initialze CCU methods via system.listMethods call
-func (c *Connection) Init() error {
- _, err := c.XmlCmd("system.listMethods", c.SwitchChannel)
- return err
+// getCCUFloat selects a float value of a CCU API response member
+func getFloatValue(res MethodResponse, valueName string) float64 {
+ for _, m := range res.Member {
+ if m.Name == valueName {
+ return m.Value.CCUFloat
+ }
+ }
+
+ return 0
}
-// Enabled reads the homematic HMIP-PSM switchchannel state true=on/false=off
-func (c *Connection) Enabled() (bool, error) {
- res, err := c.XmlCmd("getValue", c.SwitchChannel, Param{CCUString: "STATE"})
- return res.Value.CCUBool == "1", err
-}
+// getCCUBool selects a float value of a CCU API response member
+func getBoolValue(res MethodResponse, valueName string) bool {
+ for _, m := range res.Member {
+ if m.Name == valueName {
+ return m.Value.CCUBool
+ }
+ }
-// Enable sets the homematic HMIP-PSM switchchannel state to true=on/false=off
-func (c *Connection) Enable(enable bool) error {
- onoff := map[bool]string{true: "1", false: "0"}
- _, err := c.XmlCmd("setValue", c.SwitchChannel, Param{CCUString: "STATE"}, Param{CCUBool: onoff[enable]})
- return err
-}
-
-// CurrentPower reads the homematic HMIP-PSM meterchannel power in W
-func (c *Connection) CurrentPower() (float64, error) {
- res, err := c.XmlCmd("getValue", c.MeterChannel, Param{CCUString: "POWER"})
- return res.Value.CCUFloat, err
-}
-
-// TotalEnergy reads the homematic HMIP-PSM meterchannel energy in Wh
-func (c *Connection) TotalEnergy() (float64, error) {
- res, err := c.XmlCmd("getValue", c.MeterChannel, Param{CCUString: "ENERGY_COUNTER"})
- return res.Value.CCUFloat / 1000, err
-}
-
-// Currents reads the homematic HMIP-PSM meterchannel L1 current in A
-func (c *Connection) Currents() (float64, float64, float64, error) {
- res, err := c.XmlCmd("getValue", c.MeterChannel, Param{CCUString: "CURRENT"})
- return res.Value.CCUFloat / 1000, 0, 0, err
-}
-
-// GridCurrentPower reads the homematic HM-ES-TX-WM grid meterchannel power in W
-func (c *Connection) GridCurrentPower() (float64, error) {
- res, err := c.XmlCmd("getValue", c.MeterChannel, Param{CCUString: "IEC_POWER"})
- return res.Value.CCUFloat, err
-}
-
-// GridTotalEnergy reads the homematic HM-ES-TX-WM grid meterchannel energy in Wh
-func (c *Connection) GridTotalEnergy() (float64, error) {
- res, err := c.XmlCmd("getValue", c.MeterChannel, Param{CCUString: "IEC_ENERGY_COUNTER"})
- return res.Value.CCUFloat, err
+ return false
}
// parseError checks on Homematic CCU error codes
@@ -162,6 +210,10 @@ func parseError(res MethodResponse) error {
}
}
+ if faultString == "" {
+ faultString = "Unknown Homematic API Error"
+ }
+
if faultCode != 0 {
return fmt.Errorf("%s (%v)", faultString, faultCode)
}
diff --git a/meter/homematic/types.go b/meter/homematic/types.go
index ee0d84cab..13464c307 100644
--- a/meter/homematic/types.go
+++ b/meter/homematic/types.go
@@ -8,32 +8,36 @@ import (
// https://homematic-ip.com/sites/default/files/downloads/HM_XmlRpc_API.pdf
// https://homematic-ip.com/sites/default/files/downloads/HMIP_XmlRpc_API_Addendum.pdf
-type Param struct {
- CCUBool string `xml:"value>boolean,omitempty"`
- CCUFloat float64 `xml:"value>double,omitempty"`
- CCUInt int64 `xml:"value>i4,omitempty"`
- CCUString string `xml:"value>string,omitempty"`
-}
-
type MethodCall struct {
XMLName xml.Name `xml:"methodCall"`
MethodName string `xml:"methodName"`
Params []Param `xml:"params>param,omitempty"`
}
-type Member struct {
- Name string `xml:"name,omitempty"`
- Value FaultValue `xml:"value,omitempty"`
+type Param struct {
+ CCUBool string `xml:"value>boolean,omitempty"`
+ CCUFloat float64 `xml:"value>double,omitempty"`
+ CCUInt int64 `xml:"value>i4,omitempty"`
+ CCUString string `xml:"value>string,omitempty"`
}
-
-type FaultValue struct {
- XMLName xml.Name `xml:"value"`
- CCUString string `xml:",chardata"`
- CCUInt int64 `xml:"i4,omitempty"`
+type Member struct {
+ Name string `xml:"name,omitempty"`
+ Value Value `xml:"value,omitempty"`
}
type MethodResponse struct {
- XMLName xml.Name `xml:"methodResponse"`
- Value Param `xml:"params>param,omitempty"`
- Fault []Member `xml:"fault>value>struct>member,omitempty"`
+ XMLName xml.Name `xml:"methodResponse"`
+ CCUBool string `xml:"params>param>value>boolean,omitempty"`
+ CCUFloat float64 `xml:"params>param>value>double,omitempty"`
+ CCUInt int64 `xml:"params>param>value>i4,omitempty"`
+ CCUString string `xml:"params>param>value>string,omitempty"`
+ Member []Member `xml:"params>param>value>struct>member,omitempty"`
+ Fault []Member `xml:"fault>value>struct>member,omitempty"`
+}
+type Value struct {
+ XMLName xml.Name `xml:"value"`
+ CCUString string `xml:",chardata"`
+ CCUInt int64 `xml:"i4,omitempty"`
+ CCUBool bool `xml:"boolean,omitempty"`
+ CCUFloat float64 `xml:"double,omitempty"`
}
diff --git a/meter/homematic/types_test.go b/meter/homematic/types_test.go
index 9d9992481..ac60e93c6 100644
--- a/meter/homematic/types_test.go
+++ b/meter/homematic/types_test.go
@@ -12,13 +12,38 @@ import (
func TestUnmarshalMethodResponse(t *testing.T) {
{
- // Double response test
+ // BidCos-RF (Port 2001) getParamset measure-channel response test
var res MethodResponse
- xmlstr := `20698.0`
+ xmlstr := `IEC_ENERGY_COUNTER689.586500IEC_POWER166.390000`
+ assert.NoError(t, xml.Unmarshal([]byte(strings.Replace(string(xmlstr), "iso-8859-1", "UTF-8", 1)), &res))
+
+ assert.Equal(t, "IEC_ENERGY_COUNTER", res.Member[0].Name)
+ assert.Equal(t, float64(689.586500), res.Member[0].Value.CCUFloat)
+ }
+
+ {
+ // BidCos-IP (Port 2010) getParamset measure-channel response test
+ var res MethodResponse
+
+ xmlstr := `VOLTAGE230.6POWER_STATUS0ENERGY_COUNTER10888.7CURRENT_STATUS0FREQUENCY49.97ENERGY_COUNTER_OVERFLOW0POWER0.05VOLTAGE_STATUS0CURRENT0.0FREQUENCY_STATUS0`
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)
+ assert.Equal(t, "ENERGY_COUNTER", res.Member[2].Name)
+ assert.Equal(t, float64(10888.7), res.Member[2].Value.CCUFloat)
+ assert.Equal(t, "POWER", res.Member[6].Name)
+ assert.Equal(t, float64(0.05), res.Member[6].Value.CCUFloat)
+ }
+
+ {
+ // BidCos-IP (Port 2010) getParamset switch-channel response test
+ var res MethodResponse
+
+ xmlstr := `SECTION_STATUS0PROCESS0STATE1SECTION2`
+ assert.NoError(t, xml.Unmarshal([]byte(strings.Replace(string(xmlstr), "ISO-8859-1", "UTF-8", 1)), &res))
+
+ assert.Equal(t, "STATE", res.Member[2].Name)
+ assert.Equal(t, true, res.Member[2].Value.CCUBool)
}
{