Support openWB type composite status provider

This commit is contained in:
andig 2020-04-14 17:54:51 +02:00
parent 706ed627fa
commit 92d298f93f
2 changed files with 47 additions and 2 deletions

View file

@ -21,8 +21,8 @@ type Config struct {
// mqttConfig is the specific mqtt getter/setter configuration
type mqttConfig struct {
Topic, Payload string // Payload only applies to setters
Multiplier float64
Timeout time.Duration
Multiplier float64
Timeout time.Duration
}
// scriptConfig is the specific script getter/setter configuration
@ -114,6 +114,8 @@ func NewStringGetterFromConfig(log *api.Logger, config Config) (res StringGetter
if pc.Cache > 0 {
res = NewCached(res, pc.Cache).StringGetter()
}
case "openwb":
res = openWBStatusFromConfig(log, config.Other)
default:
log.FATAL.Fatalf("invalid provider type %s", config.Type)
}

43
provider/openwb.go Normal file
View file

@ -0,0 +1,43 @@
package provider
import (
"github.com/andig/evcc/api"
)
type openWBStatusProvider struct {
plugged, charging BoolGetter
}
func openWBStatusFromConfig(log *api.Logger, other map[string]interface{}) StringGetter {
cc := struct {
Plugged, Charging Config
}{}
api.DecodeOther(log, other, &cc)
o := &openWBStatusProvider{
plugged: NewBoolGetterFromConfig(log, cc.Plugged),
charging: NewBoolGetterFromConfig(log, cc.Charging),
}
return o.stringGetter
}
func (o *openWBStatusProvider) stringGetter() (string, error) {
charging, err := o.charging()
if err != nil {
return "", err
}
if charging {
return string(api.StatusC), nil
}
plugged, err := o.plugged()
if err != nil {
return "", err
}
if plugged {
return string(api.StatusB), nil
}
return string(api.StatusA), nil
}