Support decoding openWB status

This commit is contained in:
andig 2020-04-14 10:38:13 +02:00
parent 516934ca6e
commit 67d2144fc2
2 changed files with 55 additions and 0 deletions

View file

@ -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)
}

53
provider/openwb.go Normal file
View file

@ -0,0 +1,53 @@
package provider
import (
"time"
"github.com/andig/evcc/api"
)
// openWBConfig is the specific mqtt getter/setter configuration
type openWBConfig struct {
PlugStat, ChargeStat string
Timeout time.Duration
}
type openWBStatusProvider struct {
plugStat, chargeStat BoolGetter
}
func openWBStatusFromConfig(log *api.Logger, other map[string]interface{}) (res StringGetter) {
if MQTT == nil {
log.FATAL.Fatal("mqtt not configured")
}
var pc openWBConfig
api.DecodeOther(log, other, &pc)
o := &openWBStatusProvider{
plugStat: MQTT.BoolGetter(pc.PlugStat, pc.Timeout),
chargeStat: MQTT.BoolGetter(pc.ChargeStat, pc.Timeout),
}
return o.stringGetter
}
func (o *openWBStatusProvider) stringGetter() (string, error) {
charging, err := o.chargeStat()
if err != nil {
return "", err
}
if charging {
return string(api.StatusC), nil
}
plugged, err := o.plugStat()
if err != nil {
return "", err
}
if plugged {
return string(api.StatusB), nil
}
return string(api.StatusA), nil
}