Skip to content

Commit

Permalink
Heating: add generic heatpump charger (#19008)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Feb 24, 2025
1 parent af8a575 commit 30ab9a4
Show file tree
Hide file tree
Showing 14 changed files with 620 additions and 107 deletions.
15 changes: 6 additions & 9 deletions charger/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"fmt"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/meter"
"github.com/evcc-io/evcc/charger/measurement"
meter "github.com/evcc-io/evcc/meter/measurement"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
)
Expand Down Expand Up @@ -36,12 +37,8 @@ func NewConfigurableFromConfig(ctx context.Context, other map[string]interface{}
Wakeup *plugin.Config
Soc *plugin.Config
Tos bool

// optional measurements
Power *plugin.Config
Energy *plugin.Config

Currents, Voltages []plugin.Config
measurement.Energy `mapstructure:",squash"` // optional
meter.Phases `mapstructure:",squash"` // optional
}

if err := util.DecodeOther(other, &cc); err != nil {
Expand Down Expand Up @@ -123,12 +120,12 @@ func NewConfigurableFromConfig(ctx context.Context, other map[string]interface{}
}

// decorate measurements
powerG, energyG, err := meter.BuildMeasurements(ctx, cc.Power, cc.Energy)
powerG, energyG, err := cc.Energy.Configure(ctx)
if err != nil {
return nil, err
}

currentsG, voltagesG, _, err := meter.BuildPhaseMeasurements(ctx, cc.Currents, cc.Voltages, nil)
currentsG, voltagesG, _, err := cc.Phases.Configure(ctx)
if err != nil {
return nil, err
}
Expand Down
159 changes: 159 additions & 0 deletions charger/heatpump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package charger

// LICENSE

// Copyright (c) 2024 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 (
"context"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/measurement"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
)

// Heatpump charger implementation
type Heatpump struct {
*embed
phases int
power int64
maxPowerG func() (int64, error)
maxPowerS func(int64) error
}

func init() {
registry.AddCtx("heatpump", NewHeatpumpFromConfig)
}

//go:generate go tool decorate -f decorateHeatpump -b *Heatpump -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.Battery,Soc,func() (float64, error)" -t "api.SocLimiter,GetLimitSoc,func() (int64, error)"

// NewHeatpumpFromConfig creates heatpump configurable charger from generic config
func NewHeatpumpFromConfig(ctx context.Context, other map[string]interface{}) (api.Charger, error) {
cc := struct {
embed `mapstructure:",squash"`
SetMaxPower plugin.Config
GetMaxPower *plugin.Config // optional
measurement.Temperature `mapstructure:",squash"`
measurement.Energy `mapstructure:",squash"`
Phases int
}{
embed: embed{
Icon_: "heatpump",
Features_: []api.Feature{api.Heating, api.IntegratedDevice},
},
Phases: 1,
}

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

maxPowerG, err := cc.GetMaxPower.IntGetter(ctx)
if err != nil {
return nil, err
}

maxPowerS, err := cc.SetMaxPower.IntSetter(ctx, "maxpower")
if err != nil {
return nil, err
}

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

res, err := NewHeatpump(ctx, &cc.embed, maxPowerS, maxPowerG, cc.Phases)
if err != nil {
return nil, err
}

powerG, energyG, err := cc.Energy.Configure(ctx)
if err != nil {
return nil, err
}

tempG, limitTempG, err := cc.Temperature.Configure(ctx)
if err != nil {
return nil, err
}

return decorateHeatpump(res, powerG, energyG, tempG, limitTempG), nil
}

// NewHeatpump creates heatpump charger
func NewHeatpump(ctx context.Context, embed *embed, maxPowerS func(int64) error, maxPowerG func() (int64, error), phases int) (*Heatpump, error) {
res := &Heatpump{
embed: embed,
maxPowerG: maxPowerG,
maxPowerS: maxPowerS,
phases: phases,
}

return res, nil
}

func (wb *Heatpump) getMaxPower() (int64, error) {
if wb.maxPowerG == nil {
return wb.power, nil
}
return wb.maxPowerG()
}

func (wb *Heatpump) setMaxPower(power int64) error {
err := wb.maxPowerS(power)
if err == nil {
wb.power = power
}

return err
}

// Status implements the api.Charger interface
func (wb *Heatpump) Status() (api.ChargeStatus, error) {
power, err := wb.getMaxPower()
if err != nil {
return api.StatusNone, err
}

status := map[bool]api.ChargeStatus{false: api.StatusB, true: api.StatusC}
return status[power > 0], nil
}

// Enabled implements the api.Charger interface
func (wb *Heatpump) Enabled() (bool, error) {
power, err := wb.getMaxPower()
return power > 0, err
}

// Enable implements the api.Charger interface
func (wb *Heatpump) Enable(enable bool) error {
var power int64
if enable {
power = wb.power
}
return wb.setMaxPower(power)
}

// MaxCurrent implements the api.Charger interface
func (wb *Heatpump) MaxCurrent(current int64) error {
return wb.MaxCurrentEx(float64(current))
}

// MaxCurrent implements the api.Charger interface
func (wb *Heatpump) MaxCurrentEx(current float64) error {
return wb.setMaxPower(int64(230 * current * float64(wb.phases)))
}
Loading

0 comments on commit 30ab9a4

Please sign in to comment.