Skip to content

Commit ac98b8a

Browse files
authored
Keba: auto-detect optional meter (#1541)
1 parent 9c32874 commit ac98b8a

File tree

3 files changed

+308
-45
lines changed

3 files changed

+308
-45
lines changed

charger/keba.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func init() {
3737
registry.Add("keba", NewKebaFromConfig)
3838
}
3939

40+
//go:generate go run ../cmd/tools/decorate.go -f decorateKeba -b *Keba -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.ChargeRater,ChargedEnergy,func() (float64, error)" -t "api.MeterCurrent,Currents,func() (float64, float64, float64, error)"
41+
4042
// NewKebaFromConfig creates a new configurable charger
4143
func NewKebaFromConfig(other map[string]interface{}) (api.Charger, error) {
4244
cc := struct {
@@ -47,15 +49,30 @@ func NewKebaFromConfig(other map[string]interface{}) (api.Charger, error) {
4749
}{
4850
Timeout: udpTimeout,
4951
}
52+
5053
if err := util.DecodeOther(other, &cc); err != nil {
5154
return nil, err
5255
}
5356

54-
return NewKeba(cc.URI, cc.Serial, cc.RFID, cc.Timeout)
57+
k, err := NewKeba(cc.URI, cc.Serial, cc.RFID, cc.Timeout)
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
energy, err := k.totalEnergy()
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
if energy > 0 {
68+
return decorateKeba(k, k.currentPower, k.totalEnergy, k.chargedEnergy, k.currents), nil
69+
}
70+
71+
return k, err
5572
}
5673

5774
// NewKeba creates a new charger
58-
func NewKeba(uri, serial string, rfid RFID, timeout time.Duration) (api.Charger, error) {
75+
func NewKeba(uri, serial string, rfid RFID, timeout time.Duration) (*Keba, error) {
5976
log := util.NewLogger("keba")
6077

6178
if keba.Instance == nil {
@@ -264,43 +281,35 @@ func (c *Keba) MaxCurrentMillis(current float64) error {
264281
return nil
265282
}
266283

267-
var _ api.Meter = (*Keba)(nil)
268-
269-
// CurrentPower implements the api.Meter interface
270-
func (c *Keba) CurrentPower() (float64, error) {
284+
// currentPower implements the api.Meter interface
285+
func (c *Keba) currentPower() (float64, error) {
271286
var kr keba.Report3
272287
err := c.roundtrip("report", 3, &kr)
273288

274289
// mW to W
275290
return float64(kr.P) / 1e3, err
276291
}
277292

278-
var _ api.MeterEnergy = (*Keba)(nil)
279-
280-
// TotalEnergy implements the api.MeterEnergy interface
281-
func (c *Keba) TotalEnergy() (float64, error) {
293+
// totalEnergy implements the api.MeterEnergy interface
294+
func (c *Keba) totalEnergy() (float64, error) {
282295
var kr keba.Report3
283296
err := c.roundtrip("report", 3, &kr)
284297

285298
// mW to W
286299
return float64(kr.ETotal) / 1e4, err
287300
}
288301

289-
var _ api.ChargeRater = (*Keba)(nil)
290-
291-
// ChargedEnergy implements the ChargeRater interface
292-
func (c *Keba) ChargedEnergy() (float64, error) {
302+
// chargedEnergy implements the ChargeRater interface
303+
func (c *Keba) chargedEnergy() (float64, error) {
293304
var kr keba.Report3
294305
err := c.roundtrip("report", 3, &kr)
295306

296307
// 0,1Wh to kWh
297308
return float64(kr.EPres) / 1e4, err
298309
}
299310

300-
var _ api.MeterCurrent = (*Keba)(nil)
301-
302-
// Currents implements the api.MeterCurrent interface
303-
func (c *Keba) Currents() (float64, float64, float64, error) {
311+
// currents implements the api.MeterCurrent interface
312+
func (c *Keba) currents() (float64, float64, float64, error) {
304313
var kr keba.Report3
305314
err := c.roundtrip("report", 3, &kr)
306315

charger/keba_decorators.go

Lines changed: 281 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)