Tasmota: simplify and clarify method names (#3788)

This commit is contained in:
andig 2022-07-07 16:29:00 +02:00 • committed by GitHub
parent f5fcd77f46
commit 48ba7aead6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 44 deletions

View file

@ -53,7 +53,7 @@ var _ api.Meter = (*Tasmota)(nil)
// CurrentPower implements the api.Meter interface
func (c *Tasmota) CurrentPower() (float64, error) {
if c.usage == "grid" {
return c.conn.GridCurrentPower()
return c.conn.SmlPower()
}
return c.conn.CurrentPower()
}
@ -63,7 +63,7 @@ var _ api.MeterEnergy = (*Tasmota)(nil)
// TotalEnergy implements the api.MeterEnergy interface
func (c *Tasmota) TotalEnergy() (float64, error) {
if c.usage == "grid" {
return c.conn.GridTotalEnergy()
return c.conn.SmlTotalEnergy()
}
return c.conn.TotalEnergy()
}

View file

@ -44,54 +44,33 @@ func (d *Connection) ExecCmd(cmd string, res interface{}) error {
"cmnd": []string{cmd},
}
err := d.GetJSON(fmt.Sprintf("%s/cm?%s", d.uri, parameters.Encode()), res)
if err != nil {
return err
}
return nil
return d.GetJSON(fmt.Sprintf("%s/cm?%s", d.uri, parameters.Encode()), res)
}
// CurrentPower provides current power consumption
// CurrentPower implements the api.Meter interface
func (d *Connection) CurrentPower() (float64, error) {
var res *StatusSNSResponse
var res StatusSNSResponse
err := d.ExecCmd("Status 8", &res)
if err != nil {
return 0, err
}
return float64(res.StatusSNS.Energy.Power), nil
return float64(res.StatusSNS.Energy.Power), err
}
// TotalEnergy implements the api.MeterEnergy interface
func (d *Connection) TotalEnergy() (float64, error) {
var res *StatusSNSResponse
var res StatusSNSResponse
err := d.ExecCmd("Status 8", &res)
if err != nil {
return 0, err
}
return float64(res.StatusSNS.Energy.Total), nil
return res.StatusSNS.Energy.Total, err
}
// GridCurrentPower provides current power consumption
func (d *Connection) GridCurrentPower() (float64, error) {
var res *StatusSNSResponse
// SmlPower provides the sml sensor power
func (d *Connection) SmlPower() (float64, error) {
var res StatusSNSResponse
err := d.ExecCmd("Status 8", &res)
if err != nil {
return 0, err
}
return float64(res.StatusSNS.SML.Power_curr), nil
return float64(res.StatusSNS.SML.PowerCurr), err
}
// GridTotalEnergy implements the api.MeterEnergy interface
func (d *Connection) GridTotalEnergy() (float64, error) {
var res *StatusSNSResponse
// SmlTotalEnergy provides the sml sensor total import energy
func (d *Connection) SmlTotalEnergy() (float64, error) {
var res StatusSNSResponse
err := d.ExecCmd("Status 8", &res)
if err != nil {
return 0, err
}
return float64(res.StatusSNS.SML.Total_in), nil
return res.StatusSNS.SML.TotalIn, err
}

View file

@ -60,7 +60,9 @@ type PowerResponse struct {
// https://tasmota.github.io/docs/JSON-Status-Responses/
type StatusSNSResponse struct {
StatusSNS struct {
Time string
Time string
// Energy readings
Energy struct {
TotalStartTime string
Total float64
@ -73,10 +75,12 @@ type StatusSNSResponse struct {
Voltage int
Current float64
}
// SML sensor readings
SML struct {
Total_in float64
Total_out float64
Power_curr int
TotalIn float64 `json:"total_in"`
TotalOut float64 `json:"total_out"`
PowerCurr int `json:"power_curr"`
}
}
}

View file

@ -75,7 +75,7 @@ func (d *Connection) ExecCmd(cmd string, res interface{}) error {
return json.Unmarshal(buf.Bytes(), res)
}
// CurrentPower provides current power consumption
// CurrentPower implements the api.Meter interface
func (d *Connection) CurrentPower() (float64, error) {
var res EmeterResponse
if err := d.ExecCmd(`{"emeter":{"get_realtime":null}}`, &res); err != nil {
@ -83,7 +83,7 @@ func (d *Connection) CurrentPower() (float64, error) {
}
if err := res.Emeter.GetRealtime.ErrCode; err != 0 {
return 0, fmt.Errorf("get_realtime error %d", err)
return 0, fmt.Errorf("get_realtime: %d", err)
}
power := res.Emeter.GetRealtime.PowerMw / 1000
@ -102,7 +102,7 @@ func (d *Connection) TotalEnergy() (float64, error) {
}
if err := res.Emeter.GetRealtime.ErrCode; err != 0 {
return 0, fmt.Errorf("get_realtime error %d", err)
return 0, fmt.Errorf("get_realtime: %d", err)
}
energy := res.Emeter.GetRealtime.TotalWh / 1000