ACThor: add temp source (#18431)

This commit is contained in:
Frank Walburg 2025-01-27 20:26:48 +01:00 • committed by GitHub
parent 4541a535ca
commit e59aead868
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 6 deletions

View file

@ -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
}

View file

@ -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 }}