From e59aead86898fcac0abdbae6739d03000c3435a8 Mon Sep 17 00:00:00 2001 From: Frank Walburg <23580715+walburgf@users.noreply.github.com> Date: Mon, 27 Jan 2025 20:26:48 +0100 Subject: [PATCH] ACThor: add temp source (#18431) --- charger/mypv.go | 26 +++++++++++++++++------ templates/definition/charger/ac-thor.yaml | 7 ++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/charger/mypv.go b/charger/mypv.go index e20d7e40b..b01370073 100644 --- a/charger/mypv.go +++ b/charger/mypv.go @@ -20,6 +20,7 @@ package charger import ( "context" "encoding/binary" + "fmt" "sync/atomic" "time" @@ -38,16 +39,18 @@ type MyPv struct { power uint32 statusC uint16 enabled bool + regTemp uint16 } const ( elwaRegSetPower = 1000 - elwaRegTemp = 1001 elwaRegTempLimit = 1002 elwaRegStatus = 1003 elwaRegPower = 1000 // https://github.com/evcc-io/evcc/issues/18020#issuecomment-2585300804 ) +var elwaTemp = []uint16{1001, 1030, 1031} + func init() { // https://github.com/evcc-io/evcc/discussions/12761 registry.AddCtx("ac-elwa-2", func(ctx context.Context, other map[string]interface{}) (api.Charger, error) { @@ -62,19 +65,25 @@ func init() { // newMyPvFromConfig creates a MyPv charger from generic config func newMyPvFromConfig(ctx context.Context, name string, other map[string]interface{}, statusC uint16) (api.Charger, error) { - cc := modbus.TcpSettings{ - ID: 1, + cc := struct { + modbus.TcpSettings `mapstructure:",squash"` + TempSource int + }{ + TcpSettings: modbus.TcpSettings{ + ID: 1, // default + }, + TempSource: 1, } if err := util.DecodeOther(other, &cc); err != nil { return nil, err } - return NewMyPv(ctx, name, cc.URI, cc.ID, statusC) + return NewMyPv(ctx, name, cc.URI, cc.ID, cc.TempSource, statusC) } // NewMyPv creates myPV AC Elwa 2 or Thor charger -func NewMyPv(ctx context.Context, name, uri string, slaveID uint8, statusC uint16) (api.Charger, error) { +func NewMyPv(ctx context.Context, name, uri string, slaveID uint8, tempSource int, statusC uint16) (api.Charger, error) { conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, slaveID) if err != nil { return nil, err @@ -84,6 +93,10 @@ func NewMyPv(ctx context.Context, name, uri string, slaveID uint8, statusC uint1 return nil, api.ErrSponsorRequired } + if tempSource < 1 || tempSource > len(elwaTemp) { + return nil, fmt.Errorf("invalid temp source: %d", tempSource) + } + log := util.NewLogger(name) conn.Logger(log.TRACE) @@ -91,6 +104,7 @@ func NewMyPv(ctx context.Context, name, uri string, slaveID uint8, statusC uint1 log: log, conn: conn, statusC: statusC, + regTemp: elwaTemp[tempSource-1], } go wb.heartbeat(ctx, 30*time.Second) @@ -225,7 +239,7 @@ var _ api.Battery = (*MyPv)(nil) // CurrentPower implements the api.Meter interface func (wb *MyPv) Soc() (float64, error) { - b, err := wb.conn.ReadHoldingRegisters(elwaRegTemp, 1) + b, err := wb.conn.ReadHoldingRegisters(wb.regTemp, 1) if err != nil { return 0, err } diff --git a/templates/definition/charger/ac-thor.yaml b/templates/definition/charger/ac-thor.yaml index 534b287cd..94d331ef9 100644 --- a/templates/definition/charger/ac-thor.yaml +++ b/templates/definition/charger/ac-thor.yaml @@ -9,6 +9,13 @@ requirements: params: - name: modbus choice: ["tcpip"] + - name: tempsource + choice: ["1", "2", "3"] + default: "1" + description: + en: "Temperature sensor" + de: "Temperatursensor" render: | type: ac-thor {{- include "modbus" . }} + tempsource: {{ .tempsource }}