Skip to content

Commit

Permalink
Add Victron charger (#10133)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Oct 1, 2023
1 parent 9f7d6a3 commit 64b7eb3
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
150 changes: 150 additions & 0 deletions charger/victron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package charger

// LICENSE

// Copyright (c) 2023 andig

// This module is NOT covered by the MIT license. All rights reserved.

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import (
"encoding/binary"
"errors"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
"github.com/evcc-io/evcc/util/sponsor"
)

// Victron charger implementation
type Victron struct {
conn *modbus.Connection
}

const (
victronRegMode = 3815
victronRegEnergy = 3816
victronRegPower = 3821
victronRegStatus = 3824
victronRegSetCurrent = 3825
victronRegEnabled = 3826
)

func init() {
registry.Add("victron", NewVictronFromConfig)
}

// NewVictronFromConfig creates a ABB charger from generic config

This comment has been minimized.

Copy link
@premultiply

premultiply Oct 1, 2023

Member

ABB?

func NewVictronFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.TcpSettings{
ID: 100,
}

if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}

return NewVictron(cc.URI, cc.ID)
}

// NewVictron creates Victron charger
func NewVictron(uri string, slaveID uint8) (api.Charger, error) {
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, slaveID)
if err != nil {
return nil, err
}

if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}

log := util.NewLogger("victron")
conn.Logger(log.TRACE)

wb := &Victron{
conn: conn,
}

b, err := wb.conn.ReadHoldingRegisters(victronRegMode, 1)
if err != nil {
return nil, err
}

if binary.BigEndian.Uint16(b) != 0 {
return nil, errors.New("charger must be in manual mode")
}

return wb, err
}

// Status implements the api.Charger interface
func (wb *Victron) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadHoldingRegisters(victronRegStatus, 1)
if err != nil {
return api.StatusNone, err
}

return api.ChargeStatusString(string('A' + rune(binary.BigEndian.Uint16(b))))
}

// Enabled implements the api.Charger interface
func (wb *Victron) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(victronRegEnabled, 1)
if err != nil {
return false, err
}

return binary.BigEndian.Uint16(b) != 0, nil
}

// Enable implements the api.Charger interface
func (wb *Victron) Enable(enable bool) error {
var u uint16
if enable {
u = 1
}

_, err := wb.conn.WriteSingleRegister(victronRegEnabled, u)
return err
}

// MaxCurrent implements the api.Charger interface
func (wb *Victron) MaxCurrent(current int64) error {
_, err := wb.conn.WriteSingleRegister(victronRegSetCurrent, uint16(current))
return err
}

var _ api.Meter = (*Victron)(nil)

// CurrentPower implements the api.Meter interface
func (wb *Victron) CurrentPower() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(victronRegPower, 1)
if err != nil {
return 0, err
}

return float64(binary.BigEndian.Uint16(b)), nil
}

var _ api.ChargeRater = (*Victron)(nil)

// ChargedEnergy implements the api.MeterEnergy interface
func (wb *Victron) ChargedEnergy() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(victronRegEnergy, 2)
if err != nil {
return 0, err
}

return float64(binary.BigEndian.Uint32(b)) / 100, nil
}
16 changes: 16 additions & 0 deletions templates/definition/charger/victron.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
template: victron
products:
- brand: Victron
description:
generic: EV charging station
requirements:
description:
en: Charger has to be in manual mode and Modbus has to be configured for ID 100.
de: Wallbox muss sich im Modus "Manual" befinden und Modbus ID 100 konfiguriert sein.
params:
- name: modbus
choice: ["tcpip"]
id: 100
render: |
type: victron
{{- include "modbus" . }}
1 change: 1 addition & 0 deletions templates/evcc.io/brands.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"TinkerForge",
"Ubitricity",
"Vestel",
"Victron",
"Volkswagen",
"Wallbe",
"wallbox",
Expand Down

0 comments on commit 64b7eb3

Please sign in to comment.