Tasmota: refactor channel handling (#5771)
This commit is contained in:
parent
17791fb53a
commit
254f5bf4b1
4 changed files with 48 additions and 31 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue