Skip to content

Commit 0a31bfc

Browse files
committed
fix: support emeter key with units
Fix #1
1 parent 83f6438 commit 0a31bfc

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

kasa/emeter.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,35 @@ type GetRealtimeRequest struct {
88
}
99

1010
type GetRealtimeResponse struct {
11-
Current float64 `mapstructure:"current_ma"`
12-
Voltage float64 `mapstructure:"voltage_mv"`
13-
Power float64 `mapstructure:"power_mw"`
14-
Total float64 `mapstructure:"total_wh"`
11+
// some fw/hw may use the following keys
12+
Current float64 `mapstructure:"current"` // unit is A
13+
Voltage float64 `mapstructure:"voltage"` // unit is V
14+
Power float64 `mapstructure:"power"` // unit is W
15+
Total float64 `mapstructure:"total"` // unit is kWh
16+
17+
// some may use these
18+
CurrentmA float64 `mapstructure:"current_ma"`
19+
VoltagemV float64 `mapstructure:"voltage_mv"`
20+
PowermW float64 `mapstructure:"power_mw"`
21+
TotalWh float64 `mapstructure:"total_wh"`
22+
}
23+
24+
func (r *GetRealtimeResponse) Normalize() {
25+
if r.TotalWh != -1 {
26+
r.Current = r.CurrentmA / 1000
27+
r.Voltage = r.VoltagemV / 1000
28+
r.Power = r.PowermW / 1000
29+
r.Total = r.TotalWh / 1000
30+
}
1531
}
1632

1733
func (s *KasaClientEmeterService) GetRealtime() (*GetRealtimeResponse, error) {
18-
var response GetRealtimeResponse
34+
response := GetRealtimeResponse{
35+
TotalWh: -1,
36+
}
1937
err := s.c.RPC("emeter", "get_realtime", GetRealtimeRequest{}, &response)
2038

39+
response.Normalize()
40+
2141
return &response, err
2242
}

kasa/kasa.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
type KasaClient struct {
1414
addr string
1515
model string
16-
conn *net.Conn
1716
}
1817

1918
type KasaClientConfig struct {

0 commit comments

Comments
 (0)