Skip to content

Commit

Permalink
fix: support emeter key with units
Browse files Browse the repository at this point in the history
Fix #1
  • Loading branch information
fffonion committed Dec 9, 2019
1 parent 83f6438 commit 0a31bfc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
30 changes: 25 additions & 5 deletions kasa/emeter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,35 @@ type GetRealtimeRequest struct {
}

type GetRealtimeResponse struct {
Current float64 `mapstructure:"current_ma"`
Voltage float64 `mapstructure:"voltage_mv"`
Power float64 `mapstructure:"power_mw"`
Total float64 `mapstructure:"total_wh"`
// some fw/hw may use the following keys
Current float64 `mapstructure:"current"` // unit is A
Voltage float64 `mapstructure:"voltage"` // unit is V
Power float64 `mapstructure:"power"` // unit is W
Total float64 `mapstructure:"total"` // unit is kWh

// some may use these
CurrentmA float64 `mapstructure:"current_ma"`
VoltagemV float64 `mapstructure:"voltage_mv"`
PowermW float64 `mapstructure:"power_mw"`
TotalWh float64 `mapstructure:"total_wh"`
}

func (r *GetRealtimeResponse) Normalize() {
if r.TotalWh != -1 {
r.Current = r.CurrentmA / 1000
r.Voltage = r.VoltagemV / 1000
r.Power = r.PowermW / 1000
r.Total = r.TotalWh / 1000
}
}

func (s *KasaClientEmeterService) GetRealtime() (*GetRealtimeResponse, error) {
var response GetRealtimeResponse
response := GetRealtimeResponse{
TotalWh: -1,
}
err := s.c.RPC("emeter", "get_realtime", GetRealtimeRequest{}, &response)

response.Normalize()

return &response, err
}
1 change: 0 additions & 1 deletion kasa/kasa.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
type KasaClient struct {
addr string
model string
conn *net.Conn
}

type KasaClientConfig struct {
Expand Down

0 comments on commit 0a31bfc

Please sign in to comment.