Skip to content

Commit

Permalink
Hardybarth Salia: add phase switching (#16683)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Oct 16, 2024
1 parent 730ad8f commit 96e6c97
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 16 deletions.
10 changes: 8 additions & 2 deletions charger/echarge/salia/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const (
HeartBeat = "salia/heartbeat"
ChargeMode = "salia/chargemode"
PauseCharging = "salia/pausecharging"
SetPhase = "salia/phase_switching/setphase"
GridCurrentLimit = "grid_current_limit"
)

Expand Down Expand Up @@ -33,8 +34,13 @@ type Port struct {
}
}
Salia struct {
ChargeMode string
PauseCharging int `json:"pausecharging,string"`
ChargeMode string
PauseCharging int `json:"pausecharging,string"`
PhaseSwitching struct {
Actual int `json:",string"`
Status string
SetPhase string `json:"setphase,omitempty"`
} `json:"phase_switching"`
}
Metering struct {
Meter struct {
Expand Down
57 changes: 49 additions & 8 deletions charger/hardybarth-salia.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func init() {
registry.Add("hardybarth-salia", NewSaliaFromConfig)
}

//go:generate go run ../cmd/tools/decorate.go -f decorateSalia -b *Salia -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)"
//go:generate go run ../cmd/tools/decorate.go -f decorateSalia -b *Salia -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)" -t "api.PhaseSwitcher,Phases1p3p,func(int) error" -t "api.PhaseGetter,GetPhases,func() (int, error)"

// NewSaliaFromConfig creates a Salia cPH2 charger from generic config
func NewSaliaFromConfig(other map[string]interface{}) (api.Charger, error) {
Expand Down Expand Up @@ -118,17 +118,34 @@ func NewSalia(uri string, cache time.Duration) (api.Charger, error) {
}
}

if err == nil {
go wb.heartbeat()
if err != nil {
return nil, err
}

wb.pause(false)
go wb.heartbeat()

if res.Secc.Port0.Metering.Meter.Available > 0 {
return decorateSalia(wb, wb.currentPower, wb.totalEnergy, wb.currents), nil
}
wb.pause(false)

var (
currentPower func() (float64, error)
totalEnergy func() (float64, error)
currents func() (float64, float64, float64, error)
phasesG func() (int, error)
phasesS func(int) error
)

if res.Secc.Port0.Metering.Meter.Available > 0 {
currentPower = wb.currentPower
totalEnergy = wb.totalEnergy
currents = wb.currents
}

if res.Secc.Port0.Salia.PhaseSwitching.Actual > 0 {
phasesG = wb.getPhases
phasesS = wb.phases1p3p
}

return wb, err
return decorateSalia(wb, currentPower, totalEnergy, currents, phasesS, phasesG), nil
}

func (wb *Salia) heartbeat() {
Expand Down Expand Up @@ -250,6 +267,30 @@ func (wb *Salia) currents() (float64, float64, float64, error) {
// return "", api.ErrNotAvailable
// }

func (wb *Salia) getPhases() (int, error) {
res, err := wb.apiG.Get()
if err != nil {
return 0, err
}

if res.Secc.Port0.Salia.PhaseSwitching.Actual == 0 {
return 0, api.ErrNotAvailable
}

return res.Secc.Port0.Salia.PhaseSwitching.Actual, nil
}

func (wb *Salia) phases1p3p(phases int) error {
p, err := wb.getPhases()
if err != nil {
return err
}
if p == phases {
return nil
}
return wb.post(salia.SetPhase, "toggle")
}

var _ api.Diagnosis = (*Salia)(nil)

// Diagnose implements the api.Diagnosis interface
Expand Down
222 changes: 216 additions & 6 deletions charger/hardybarth-salia_decorators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 96e6c97

Please sign in to comment.