From 254f5bf4b1dd233092b2af8f1f09153a1dc9ca0a Mon Sep 17 00:00:00 2001 From: Markus Thierolf <77847348+thierolm@users.noreply.github.com> Date: Sun, 15 Jan 2023 20:31:03 +0100 Subject: [PATCH] Tasmota: refactor channel handling (#5771) --- charger/tasmota.go | 2 +- meter/tasmota.go | 11 +++++++---- meter/tasmota/connection.go | 27 ++++++------------------- meter/tasmota/types.go | 39 ++++++++++++++++++++++++++++++++----- 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/charger/tasmota.go b/charger/tasmota.go index bcb067c1b..09c0e8b9c 100644 --- a/charger/tasmota.go +++ b/charger/tasmota.go @@ -47,7 +47,7 @@ func NewTasmotaFromConfig(other map[string]interface{}) (api.Charger, error) { // NewTasmota creates Tasmota charger func NewTasmota(uri, user, password string, channel int, standbypower float64) (*Tasmota, error) { - conn, err := tasmota.NewConnection(uri, user, password) + conn, err := tasmota.NewConnection(uri, user, password, channel) if err != nil { return nil, err } diff --git a/meter/tasmota.go b/meter/tasmota.go index cef48d528..b4faadee7 100644 --- a/meter/tasmota.go +++ b/meter/tasmota.go @@ -19,23 +19,26 @@ func init() { // NewTasmotaFromConfig creates a Tasmota meter from generic config func NewTasmotaFromConfig(other map[string]interface{}) (api.Meter, error) { - var cc struct { + cc := struct { URI string User string Password string + Channel int Usage string + }{ + Channel: 1, } if err := util.DecodeOther(other, &cc); err != nil { return nil, err } - return NewTasmota(cc.URI, cc.User, cc.Password, cc.Usage) + return NewTasmota(cc.URI, cc.User, cc.Password, cc.Usage, cc.Channel) } // NewTasmota creates Tasmota meter -func NewTasmota(uri, user, password, usage string) (*Tasmota, error) { - conn, err := tasmota.NewConnection(uri, user, password) +func NewTasmota(uri, user, password, usage string, channel int) (*Tasmota, error) { + conn, err := tasmota.NewConnection(uri, user, password, channel) if err != nil { return nil, err } diff --git a/meter/tasmota/connection.go b/meter/tasmota/connection.go index 4334490d5..c94bec9e9 100644 --- a/meter/tasmota/connection.go +++ b/meter/tasmota/connection.go @@ -15,10 +15,11 @@ import ( type Connection struct { *request.Helper uri, user, password string + channel int } // NewConnection creates a Tasmota connection -func NewConnection(uri, user, password string) (*Connection, error) { +func NewConnection(uri, user, password string, channel int) (*Connection, error) { if uri == "" { return nil, errors.New("missing uri") } @@ -29,6 +30,7 @@ func NewConnection(uri, user, password string) (*Connection, error) { uri: util.DefaultScheme(strings.TrimRight(uri, "/"), "http"), user: user, password: password, + channel: channel, } c.Client.Transport = request.NewTripper(log, transport.Insecure()) @@ -50,27 +52,10 @@ func (d *Connection) ExecCmd(cmd string, res interface{}) error { // CurrentPower implements the api.Meter interface func (d *Connection) CurrentPower() (float64, error) { var res StatusSNSResponse - var err error - var power float64 - - if err = d.ExecCmd("Status 8", &res); err == nil { - switch v := res.StatusSNS.Energy.Power.(type) { - case float64: - power = res.StatusSNS.Energy.Power.(float64) - case []interface{}: - // take first power meter value in case of a power meter list - for i, vl := range v { - switch vv := vl.(type) { - case float64: - if i == 0 { - power = vv - } - } - } - } + if err := d.ExecCmd("Status 8", &res); err != nil { + return 0, err } - - return power, err + return res.StatusSNS.Energy.Power.Channel(d.channel) } // TotalEnergy implements the api.MeterEnergy interface diff --git a/meter/tasmota/types.go b/meter/tasmota/types.go index abfd4899b..14b2952c0 100644 --- a/meter/tasmota/types.go +++ b/meter/tasmota/types.go @@ -1,5 +1,11 @@ package tasmota +import ( + "encoding/json" + "fmt" + "strconv" +) + // StatusResponse is the Status part of the Tasmota Status 0 command response // https://tasmota.github.io/docs/JSON-Status-Responses/ type StatusResponse struct { @@ -68,13 +74,13 @@ type StatusSNSResponse struct { Total float64 Yesterday float64 Today float64 - Power interface{} - ApparentPower interface{} - ReactivePower interface{} - Factor interface{} + Power Channels + ApparentPower Channels + ReactivePower Channels + Factor Channels Frequency int Voltage int - Current interface{} + Current Channels } // SML sensor readings @@ -85,3 +91,26 @@ type StatusSNSResponse struct { } } } + +// Channels is a Tasmota specifc helper type to handle meter value lists and single meter values +type Channels []float64 + +func (ch *Channels) Channel(channel int) (float64, error) { + if channel < 1 || channel > len(*ch) { + return 0, fmt.Errorf("invalid channel: %d", channel) + } + return (*ch)[channel-1], nil +} + +func (ch *Channels) UnmarshalJSON(data []byte) error { + if f, err := strconv.ParseFloat(string(data), 64); err == nil { + *ch = Channels([]float64{f}) + return nil + } + + var ff []float64 + err := json.Unmarshal(data, &ff) + *ch = Channels(ff) + + return err +}