Skip to content

Commit

Permalink
Shelly: fix Total Energy 4 gen1 EM devices (evcc-io#6204)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Thierolf <m.thierolf@googlemail.com>
  • Loading branch information
thierolm and Markus Thierolf authored Feb 11, 2023
1 parent f0e47b4 commit cc85ed8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
29 changes: 21 additions & 8 deletions meter/shelly/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
// Connection is the Shelly connection
type Connection struct {
*request.Helper
uri string
channel int
gen int // Shelly api generation
uri string
channel int
gen int // Shelly api generation
devicetype string // Shelly device type
}

// NewConnection creates a new Shelly device connection.
Expand All @@ -40,9 +41,10 @@ func NewConnection(uri, user, password string, channel int) (*Connection, error)
}

conn := &Connection{
Helper: client,
channel: channel,
gen: resp.Gen,
Helper: client,
channel: channel,
gen: resp.Gen,
devicetype: resp.Type,
}

conn.Client.Transport = request.NewTripper(log, transport.Insecure())
Expand Down Expand Up @@ -184,13 +186,15 @@ func (d *Connection) TotalEnergy() (float64, error) {

switch {
case d.channel < len(res.Meters):
energy = res.Meters[d.channel].Total / 60
energy = res.Meters[d.channel].Total
case d.channel < len(res.EMeters):
energy = res.EMeters[d.channel].Total / 60
energy = res.EMeters[d.channel].Total
default:
return 0, errors.New("invalid channel, missing power meter")
}

energy = GetGen1Energy(d.devicetype, energy)

default:
var res Gen2StatusResponse
if err := d.execGen2Cmd("Shelly.GetStatus", false, &res); err != nil {
Expand All @@ -209,3 +213,12 @@ func (d *Connection) TotalEnergy() (float64, error) {

return energy / 1000, nil
}

// GetGen1Energy in kWh
func GetGen1Energy(devicetype string, energy float64) float64 {
// Gen 1 Shelly EM devices are providing Watt hours, Shelly EM devices are providing Watt minutes
if !strings.Contains(devicetype, "EM") {
energy = energy / 60
}
return energy
}
67 changes: 67 additions & 0 deletions meter/shelly/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package shelly

import (
"encoding/json"
"testing"
)

// Test Gen1StatusResponse response
func TestUnmarshalGen1StatusResponse(t *testing.T) {
var res Gen1StatusResponse

// Shelly 1 PM channel 0 (1)
jsonstr := `{"wifi_sta":{"connected":true,"ssid":"XXXX-WLAN","ip":"192.168.178.XXX","rssi":-54},"cloud":{"enabled":false,"connected":false},"mqtt":{"connected":false},"time":"17:59","unixtime":1676134770,"serial":2437,"has_update":true,"mac":"84CCA8XXXXXXX","cfg_changed_cnt":1,"actions_stats":{"skipped":0},"relays":[{"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"overpower":false,"source":"http"}],"meters":[{"power":4711.12,"overpower":0.00,"is_valid":true,"timestamp":1676138370,"counters":[0.000, 0.000, 0.000],"total":6472513}],"inputs":[{"input":0,"event":"","event_cnt":0}],"temperature":16.79,"overtemperature":false,"tmp":{"tC":16.79,"tF":62.22, "is_valid":true},"temperature_status":"Normal","ext_sensors":{},"ext_temperature":{},"ext_humidity":{},"update":{"status":"pending","has_update":true,"new_version":"20221108-153925/v1.12.1-1PM-fix-g2821131","old_version":"20220209-094317/v1.11.8-g8c7bb8d"},"ram_total":50456,"ram_free":37056,"fs_size":233681,"fs_free":149094,"uptime":17284290}`
if err := json.Unmarshal([]byte(jsonstr), &res); err != nil {
t.Error(err)
}
if GetGen1Energy("SHSW-PM", res.Meters[0].Total) != 107875.21666666666 {
t.Error("SHSW-PM res.Meters[0].Total) != 107875.21666666666")
}
if res.Meters[0].Power != 4711.12 {
t.Error("res.Meters[0].Power != 4711.12")
}

// Shelly 1 channel 0 (1)
res = Gen1StatusResponse{}
jsonstr = `{"wifi_sta":{"connected":true,"ssid":"XXXX-WLAN","ip":"192.168.178.XXX","rssi":-57},"cloud":{"enabled":false,"connected":false},"mqtt":{"connected":false},"time":"19:25","unixtime":1676139913,"serial":959,"has_update":true,"mac":"E8DB8XXXXXX","cfg_changed_cnt":1,"actions_stats":{"skipped":0},"relays":[{"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"source":"timer"}],"meters":[{"power":81.5,"is_valid":true}],"inputs":[{"input":0,"event":"","event_cnt":0}],"ext_sensors":{},"ext_temperature":{},"ext_humidity":{},"update":{"status":"pending","has_update":true,"new_version":"20221027-091427/v1.12.1-ga9117d3","old_version":"20211109-124958/v1.11.7-g682a0db"},"ram_total":50880,"ram_free":38796,"fs_size":233681,"fs_free":151102,"uptime":20319391}`
if err := json.Unmarshal([]byte(jsonstr), &res); err != nil {
t.Error(err)
}
if GetGen1Energy("SHSW-1", res.Meters[0].Total) != 0 {
t.Error("SHSW-1 res.Meters[0].Total) != 0")
}
if res.Meters[0].Power != 81.5 {
t.Error("res.Meters[0].Power != -81.5")
}

// Shelly EM channel 0 (1)
res = Gen1StatusResponse{}
jsonstr = `{"wifi_sta":{"connected":true,"ssid":"XXXX","ip":"192.168.178.XXX","rssi":-55},"cloud":{"enabled":false,"connected":false},"mqtt":{"connected":false},"time":"11:16","unixtime":1676110566,"serial":21580,"has_update":false,"mac":"C45BXXXXX","cfg_changed_cnt":0,"actions_stats":{"skipped":0},"relays":[{"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"overpower":false,"is_valid":true,"source":"input"}],"emeters":[{"power":-620.34,"reactive":714.48,"pf":-0.66,"voltage":235.68,"is_valid":true,"total":401472.9,"total_returned":653673.7},{"power":0.00,"reactive":0.00,"pf":0.00,"voltage":235.68,"is_valid":true,"total":173411.3,"total_returned":294.2}],"update":{"status":"idle","has_update":false,"new_version":"20221027-105518/v1.12.1-ga9117d3","old_version":"20221027-105518/v1.12.1-ga9117d3"},"ram_total":51072,"ram_free":35660,"fs_size":233681,"fs_free":156373,"uptime":2226140}`
if err := json.Unmarshal([]byte(jsonstr), &res); err != nil {
t.Error(err)
}
if GetGen1Energy("SHEM", res.EMeters[0].Total) != 401472.9 {
t.Error("SHEM res.EMeters[0].Total) != 401472.9")
}
if res.EMeters[0].Power != -620.34 {
t.Error("res.EMeters[0].Power != -620.34")
}

}

// Test Gen2StatusResponse response
func TestUnmarshalGen2StatusResponse(t *testing.T) {
var res Gen2StatusResponse

// Shelly Pro 1PM channel 0 (1)
jsonstr := `{"ble":{},"cloud":{"connected":true},"eth":{"ip":null},"input:0":{"id":0,"state":false},"input:1":{"id":1,"state":false},"mqtt":{"connected":false},"switch:0":{"id":0, "source":"HTTP", "output":false, "apower":47.11, "voltage":232.0, "current":0.000, "pf":0.00, "aenergy":{"total":5.125,"by_minute":[0.000,0.000,0.000],"minute_ts":1675718520},"temperature":{"tC":25.3, "tF":77.5}},"sys":{"mac":"30C6F78BB4D8","restart_required":false,"time":"22:22","unixtime":1675718522,"uptime":45070,"ram_size":234204,"ram_free":137716,"fs_size":524288,"fs_free":172032,"cfg_rev":13,"kvs_rev":1,"schedule_rev":0,"webhook_rev":0,"available_updates":{"beta":{"version":"0.13.0-beta3"}}},"wifi":{"sta_ip":"192.168.178.64","status":"got ip","ssid":"***","rssi":-62},"ws":{"connected":false}}`
if err := json.Unmarshal([]byte(jsonstr), &res); err != nil {
t.Error(err)
}
if res.Switch0.Aenergy.Total != 5.125 {
t.Error("res.Switch0.Aenergy.Total != 5.125")
}
if res.Switch0.Apower != 47.11 {
t.Error("res.Switch0.Apower != 47.11")
}
}

0 comments on commit cc85ed8

Please sign in to comment.