Add TWC3 support for Tesla vehicles (#5341)
This commit is contained in:
parent
069fc76256
commit
8a503c376e
9 changed files with 225 additions and 3 deletions
|
|
@ -117,12 +117,17 @@ type ChargeState interface {
|
|||
Status() (ChargeStatus, error)
|
||||
}
|
||||
|
||||
// CurrentLimiter provides current charging status
|
||||
type CurrentLimiter interface {
|
||||
MaxCurrent(current int64) error
|
||||
}
|
||||
|
||||
// Charger is able to provide current charging status and enable/disable charging
|
||||
type Charger interface {
|
||||
ChargeState
|
||||
Enabled() (bool, error)
|
||||
Enable(enable bool) error
|
||||
MaxCurrent(current int64) error
|
||||
CurrentLimiter
|
||||
}
|
||||
|
||||
// ChargerEx provides milli-amp precision charger current control
|
||||
|
|
|
|||
177
charger/twc3.go
Normal file
177
charger/twc3.go
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
package charger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/core/loadpoint"
|
||||
"github.com/evcc-io/evcc/provider"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
)
|
||||
|
||||
// Twc3 is an api.Vehicle implementation for Twc3 cars
|
||||
type Twc3 struct {
|
||||
*request.Helper
|
||||
lp loadpoint.API
|
||||
uri string
|
||||
vitalsG func() (Vitals, error)
|
||||
enabled bool
|
||||
}
|
||||
|
||||
func init() {
|
||||
registry.Add("twc3", NewTwc3FromConfig)
|
||||
}
|
||||
|
||||
// Vitals is the /api/1/vitals response
|
||||
type Vitals struct {
|
||||
ContactorClosed bool `json:"contactor_closed"` //false
|
||||
VehicleConnected bool `json:"vehicle_connected"` //false
|
||||
SessionS int64 `json:"session_s"` //0
|
||||
GridV float64 `json:"grid_v"` //230.1
|
||||
GridHz float64 `json:"grid_hz"` //49.928
|
||||
VehicleCurrentA float64 `json:"vehicle_current_a"` //0.1
|
||||
CurrentAA float64 `json:"currentA_a"` //0.0
|
||||
CurrentBA float64 `json:"currentB_a"` //0.1
|
||||
CurrentCA float64 `json:"currentC_a"` //0.0
|
||||
CurrentNA float64 `json:"currentN_a"` //0.0
|
||||
VoltageAV float64 `json:"voltageA_v"` //0.0
|
||||
VoltageBV float64 `json:"voltageB_v"` //0.0
|
||||
VoltageCV float64 `json:"voltageC_v"` //0.0
|
||||
RelayCoilV float64 `json:"relay_coil_v"` //11.8
|
||||
PcbaTempC float64 `json:"pcba_temp_c"` //19.2
|
||||
HandleTempC float64 `json:"handle_temp_c"` //15.3
|
||||
McuTempC float64 `json:"mcu_temp_c"` //25.1
|
||||
UptimeS int `json:"uptime_s"` //831580
|
||||
InputThermopileUv float64 `json:"input_thermopile_uv"` //-233
|
||||
ProxV float64 `json:"prox_v"` //0.0
|
||||
PilotHighV float64 `json:"pilot_high_v"` //11.9
|
||||
PilotLowV float64 `json:"pilot_low_v"` //11.9
|
||||
SessionEnergyWh float64 `json:"session_energy_wh"` //22864.699
|
||||
ConfigStatus int `json:"config_status"` //5
|
||||
EvseState int `json:"evse_state"` //1
|
||||
CurrentAlerts []any `json:"current_alerts"` //[]
|
||||
}
|
||||
|
||||
// NewTwc3FromConfig creates a new vehicle
|
||||
func NewTwc3FromConfig(other map[string]interface{}) (api.Charger, error) {
|
||||
cc := struct {
|
||||
URI string
|
||||
Cache time.Duration
|
||||
}{
|
||||
Cache: time.Second,
|
||||
}
|
||||
|
||||
if err := util.DecodeOther(other, &cc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log := util.NewLogger("twc3")
|
||||
|
||||
c := &Twc3{
|
||||
Helper: request.NewHelper(log),
|
||||
uri: util.DefaultScheme(strings.TrimSuffix(cc.URI, "/"), "http"),
|
||||
}
|
||||
|
||||
c.vitalsG = provider.Cached(func() (Vitals, error) {
|
||||
var res Vitals
|
||||
uri := fmt.Sprintf("%s/api/1/vitals", c.uri)
|
||||
err := c.GetJSON(uri, &res)
|
||||
return res, err
|
||||
}, time.Second)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Enabled implements the api.Charger interface
|
||||
func (c *Twc3) Enabled() (bool, error) {
|
||||
return c.enabled, nil
|
||||
}
|
||||
|
||||
// Enable implements the api.Charger interface
|
||||
func (c *Twc3) Enable(enable bool) error {
|
||||
if c.lp == nil {
|
||||
return errors.New("loadpoint not initialized")
|
||||
}
|
||||
|
||||
v, ok := c.lp.GetVehicle().(api.VehicleChargeController)
|
||||
if !ok {
|
||||
return errors.New("vehicle not capable of start/stop")
|
||||
}
|
||||
|
||||
var err error
|
||||
if enable {
|
||||
err = v.StartCharge()
|
||||
} else {
|
||||
err = v.StopCharge()
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
c.enabled = enable
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// MaxCurrent implements the api.Charger interface
|
||||
func (c *Twc3) MaxCurrent(current int64) error {
|
||||
if c.lp == nil {
|
||||
return errors.New("loadpoint not initialized")
|
||||
}
|
||||
|
||||
v, ok := c.lp.GetVehicle().(api.CurrentLimiter)
|
||||
if !ok {
|
||||
return errors.New("vehicle not capable of current control")
|
||||
}
|
||||
|
||||
return v.MaxCurrent(current)
|
||||
}
|
||||
|
||||
// Status implements the api.Charger interface
|
||||
func (v *Twc3) Status() (api.ChargeStatus, error) {
|
||||
status := api.StatusA // disconnected
|
||||
|
||||
res, err := v.vitalsG()
|
||||
switch {
|
||||
case res.ContactorClosed:
|
||||
status = api.StatusC
|
||||
case res.VehicleConnected:
|
||||
status = api.StatusB
|
||||
}
|
||||
|
||||
return status, err
|
||||
}
|
||||
|
||||
var _ api.ChargeRater = (*Twc3)(nil)
|
||||
|
||||
// ChargedEnergy implements the api.ChargeRater interface
|
||||
func (v *Twc3) ChargedEnergy() (float64, error) {
|
||||
res, err := v.vitalsG()
|
||||
return res.SessionEnergyWh / 1e3, err
|
||||
}
|
||||
|
||||
var _ api.ChargeTimer = (*Twc3)(nil)
|
||||
|
||||
// ChargingTime implements the api.ChargeTimer interface
|
||||
func (v *Twc3) ChargingTime() (time.Duration, error) {
|
||||
res, err := v.vitalsG()
|
||||
return time.Duration(res.SessionS) * time.Second, err
|
||||
}
|
||||
|
||||
var _ api.MeterCurrent = (*Twc3)(nil)
|
||||
|
||||
// Currents implements the api.MeterCurrent interface
|
||||
func (v *Twc3) Currents() (float64, float64, float64, error) {
|
||||
res, err := v.vitalsG()
|
||||
return res.CurrentAA, res.CurrentBA, res.CurrentCA, err
|
||||
}
|
||||
|
||||
var _ loadpoint.Controller = (*Twc3)(nil)
|
||||
|
||||
// LoadpointControl implements loadpoint.Controller
|
||||
func (v *Twc3) LoadpointControl(lp loadpoint.API) {
|
||||
v.lp = lp
|
||||
}
|
||||
|
|
@ -87,6 +87,8 @@ type API interface {
|
|||
// vehicles
|
||||
//
|
||||
|
||||
// GetVehicle gets the active vehicle
|
||||
GetVehicle() api.Vehicle
|
||||
// SetVehicle sets the active vehicle
|
||||
SetVehicle(vehicle api.Vehicle)
|
||||
// StartVehicleDetection allows triggering vehicle detection for debugging purposes
|
||||
|
|
|
|||
|
|
@ -324,6 +324,13 @@ func (lp *LoadPoint) GetRemainingEnergy() float64 {
|
|||
return lp.chargeRemainingEnergy
|
||||
}
|
||||
|
||||
// GetVehicle gets the active vehicle
|
||||
func (lp *LoadPoint) GetVehicle() api.Vehicle {
|
||||
lp.Lock()
|
||||
defer lp.Unlock()
|
||||
return lp.vehicle
|
||||
}
|
||||
|
||||
// SetVehicle sets the active vehicle
|
||||
func (lp *LoadPoint) SetVehicle(vehicle api.Vehicle) {
|
||||
// TODO develop universal locking approach
|
||||
|
|
|
|||
3
go.mod
3
go.mod
|
|
@ -98,13 +98,12 @@ require (
|
|||
gorm.io/gorm v1.24.2
|
||||
)
|
||||
|
||||
require github.com/ahmetb/go-linq/v3 v3.2.0 // indirect
|
||||
|
||||
require (
|
||||
cloud.google.com/go/compute v1.14.0 // indirect
|
||||
cloud.google.com/go/compute/metadata v0.2.2 // indirect
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/Masterminds/semver/v3 v3.2.0 // indirect
|
||||
github.com/ahmetb/go-linq/v3 v3.2.0 // indirect
|
||||
github.com/andybalholm/cascadia v1.3.1 // indirect
|
||||
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
|
|
|
|||
14
templates/definition/charger/twc3.yaml
Normal file
14
templates/definition/charger/twc3.yaml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
template: twc3
|
||||
products:
|
||||
- brand: Tesla
|
||||
description:
|
||||
generic: TWC3
|
||||
requirements:
|
||||
description:
|
||||
en: The Tesla vehicle must be associated with the TWC3 in the evcc ui.
|
||||
de: Das Teslafahrzeug muss dem TWC3 im evcc UI zugewiesen sein.
|
||||
params:
|
||||
- name: host
|
||||
render: |
|
||||
type: twc3
|
||||
uri: http://{{ .host }}
|
||||
10
templates/docs/charger/twc3_0.yaml
Normal file
10
templates/docs/charger/twc3_0.yaml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
product:
|
||||
brand: Tesla
|
||||
description: TWC3
|
||||
description: |
|
||||
Das Teslafahrzeug muss dem TWC3 im evcc UI zugewiesen sein.
|
||||
render:
|
||||
- default: |
|
||||
type: template
|
||||
template: twc3
|
||||
host: 192.0.2.2 # IP-Adresse oder Hostname
|
||||
|
|
@ -39,6 +39,7 @@
|
|||
"Sonnen",
|
||||
"Stark in Strom",
|
||||
"TechniSat",
|
||||
"Tesla",
|
||||
"TinkerForge",
|
||||
"Ubitricity",
|
||||
"Vestel",
|
||||
|
|
|
|||
|
|
@ -198,6 +198,13 @@ func (v *Tesla) TargetSoC() (float64, error) {
|
|||
return 0, err
|
||||
}
|
||||
|
||||
var _ api.CurrentLimiter = (*Tesla)(nil)
|
||||
|
||||
// StartCharge implements the api.VehicleChargeController interface
|
||||
func (v *Tesla) MaxCurrent(current int64) error {
|
||||
return v.vehicle.SetChargingAmps(int(current))
|
||||
}
|
||||
|
||||
var _ api.VehicleChargeController = (*Tesla)(nil)
|
||||
|
||||
// StartCharge implements the api.VehicleChargeController interface
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue