Skip to content

Commit

Permalink
ACThor: add temp source (#18431)
Browse files Browse the repository at this point in the history
  • Loading branch information
walburgf authored Jan 27, 2025
1 parent 4541a53 commit e59aead
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
26 changes: 20 additions & 6 deletions charger/mypv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package charger
import (
"context"
"encoding/binary"
"fmt"
"sync/atomic"
"time"

Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -84,13 +93,18 @@ 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)

wb := &MyPv{
log: log,
conn: conn,
statusC: statusC,
regTemp: elwaTemp[tempSource-1],
}

go wb.heartbeat(ctx, 30*time.Second)
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions templates/definition/charger/ac-thor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

0 comments on commit e59aead

Please sign in to comment.