evcc-io/cmd/configure/configure.go
Andreas Linde cfb174f9f5
Some checks failed
Default / Build (push) Failing after 2m0s
Default / Build UI (push) Failing after 13s
Release / Publish Docker :release (push) Has been cancelled
Release / call-build-workflow (push) Has been cancelled
Release / Publish Github & APT release (push) Has been cancelled
More template stuff (#2164)
- Configure now asks for the users knowledge level, so advanced mode can be reached without calling with `--advanced`
- Add template for solaredge inverter
- Add beta template for sungrow inverter
- Add template for MQTT based meter
- Add more template documentation
- Add check and error message for invalid config properties when using templates
- Option to specify language specific template descriptions
- Option to make params dependent on values of other params 
- Option to add sponsortoken in advanced mode
- Option to set minCurrent in advanced mode
- Remove sponsorship requirement from Daheimladen wallbox
- Multiple fixes (FritzDect meter, Kostsal Piko meter, ...)
2022-01-09 12:59:54 +01:00

120 lines
3 KiB
Go

package configure
import (
"bytes"
_ "embed"
"text/template"
"github.com/Masterminds/sprig/v3"
)
type device struct {
Name string
Title string
Yaml string
ChargerHasMeter bool // only used with chargers to detect if we need to ask for a charge meter
}
type loadpoint struct {
Title string // TODO Perspektivisch können wir was aus core wiederverwenden, für später
Charger string
ChargeMeter string
Vehicles []string
Mode string
MinCurrent int
MaxCurrent int
Phases int
ResetOnDisconnect string
}
type config struct {
Meters []device
Chargers []device
Vehicles []device
Loadpoints []loadpoint
Site struct { // TODO Perspektivisch können wir was aus core wiederverwenden, für später
Title string
Grid string
PVs []string
Batteries []string
}
Hems string
EEBUS string
MQTT string
SponsorToken string
}
type Configure struct {
config config
}
// AddDevice adds a device reference of a specific category to the configuration
// e.g. a PV meter to site.PVs
func (c *Configure) AddDevice(d device, category DeviceCategory) {
switch DeviceCategories[category].class {
case DeviceClassCharger:
c.config.Chargers = append(c.config.Chargers, d)
case DeviceClassMeter:
c.config.Meters = append(c.config.Meters, d)
switch DeviceCategories[category].categoryFilter {
case DeviceCategoryGridMeter:
c.config.Site.Grid = d.Name
case DeviceCategoryPVMeter:
c.config.Site.PVs = append(c.config.Site.PVs, d.Name)
case DeviceCategoryBatteryMeter:
c.config.Site.Batteries = append(c.config.Site.Batteries, d.Name)
}
case DeviceClassVehicle:
c.config.Vehicles = append(c.config.Vehicles, d)
}
}
// DevicesOfClass returns all configured devices of a given DeviceClass
func (c *Configure) DevicesOfClass(class DeviceClass) []device {
switch class {
case DeviceClassCharger:
return c.config.Chargers
case DeviceClassMeter:
return c.config.Meters
case DeviceClassVehicle:
return c.config.Vehicles
}
return nil
}
// AddLoadpoint adds a loadpoint to the configuration
func (c *Configure) AddLoadpoint(l loadpoint) {
c.config.Loadpoints = append(c.config.Loadpoints, l)
}
// MetersOfCategory returns the number of configured meters of a given DeviceCategory
func (c *Configure) MetersOfCategory(category DeviceCategory) int {
switch category {
case DeviceCategoryGridMeter:
if c.config.Site.Grid != "" {
return 1
}
case DeviceCategoryPVMeter:
return len(c.config.Site.PVs)
case DeviceCategoryBatteryMeter:
return len(c.config.Site.Batteries)
}
return 0
}
//go:embed configure.tpl
var configTmpl string
// RenderConfiguration creates a yaml configuration
func (c *Configure) RenderConfiguration() ([]byte, error) {
tmpl, err := template.New("yaml").Funcs(template.FuncMap(sprig.FuncMap())).Parse(configTmpl)
if err != nil {
panic(err)
}
out := new(bytes.Buffer)
err = tmpl.Execute(out, c.config)
return bytes.TrimSpace(out.Bytes()), err
}