Skip to content

Commit

Permalink
Add Homewizard kWh meter (#17150)
Browse files Browse the repository at this point in the history
  • Loading branch information
thierolm authored Nov 9, 2024
1 parent ba75820 commit 32332fe
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 11 deletions.
7 changes: 4 additions & 3 deletions charger/homewizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewHomeWizardFromConfig(other map[string]interface{}) (api.Charger, error)
cc := struct {
embed `mapstructure:",squash"`
URI string
Usage string
StandbyPower float64
Cache time.Duration
}{
Expand All @@ -37,12 +38,12 @@ func NewHomeWizardFromConfig(other map[string]interface{}) (api.Charger, error)
return nil, err
}

return NewHomeWizard(cc.embed, cc.URI, cc.StandbyPower, cc.Cache)
return NewHomeWizard(cc.embed, cc.URI, cc.Usage, cc.StandbyPower, cc.Cache)
}

// NewHomeWizard creates HomeWizard charger
func NewHomeWizard(embed embed, uri string, standbypower float64, cache time.Duration) (*HomeWizard, error) {
conn, err := homewizard.NewConnection(uri, cache)
func NewHomeWizard(embed embed, uri string, usage string, standbypower float64, cache time.Duration) (*HomeWizard, error) {
conn, err := homewizard.NewConnection(uri, usage, cache)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions meter/homewizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func init() {
func NewHomeWizardFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
URI string
Usage string
Cache time.Duration
}{
Cache: time.Second,
Expand All @@ -31,12 +32,12 @@ func NewHomeWizardFromConfig(other map[string]interface{}) (api.Meter, error) {
return nil, err
}

return NewHomeWizard(cc.URI, cc.Cache)
return NewHomeWizard(cc.URI, cc.Usage, cc.Cache)
}

// NewHomeWizard creates HomeWizard meter
func NewHomeWizard(uri string, cache time.Duration) (*HomeWizard, error) {
conn, err := homewizard.NewConnection(uri, cache)
func NewHomeWizard(uri string, usage string, cache time.Duration) (*HomeWizard, error) {
conn, err := homewizard.NewConnection(uri, usage, cache)
if err != nil {
return nil, err
}
Expand Down
13 changes: 12 additions & 1 deletion meter/homewizard/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
type Connection struct {
*request.Helper
uri string
usage string
ProductType string
dataG provider.Cacheable[DataResponse]
stateG provider.Cacheable[StateResponse]
}

// NewConnection creates a homewizard connection
func NewConnection(uri string, cache time.Duration) (*Connection, error) {
func NewConnection(uri string, usage string, cache time.Duration) (*Connection, error) {
if uri == "" {
return nil, errors.New("missing uri")
}
Expand All @@ -32,6 +33,7 @@ func NewConnection(uri string, cache time.Duration) (*Connection, error) {
c := &Connection{
Helper: request.NewHelper(log),
uri: fmt.Sprintf("%s/api", util.DefaultScheme(strings.TrimRight(uri, "/"), "http")),
usage: usage,
}

c.Client.Transport = request.NewTripper(log, transport.Insecure())
Expand Down Expand Up @@ -100,18 +102,27 @@ func (c *Connection) Enabled() (bool, error) {
// CurrentPower implements the api.Meter interface
func (c *Connection) CurrentPower() (float64, error) {
res, err := c.dataG.Get()
if c.usage == "pv" {
return -res.ActivePowerW, err
}
return res.ActivePowerW, err
}

// TotalEnergy implements the api.MeterEnergy interface
func (c *Connection) TotalEnergy() (float64, error) {
res, err := c.dataG.Get()
if c.usage == "pv" {
return res.TotalPowerExportT1kWh + res.TotalPowerExportT2kWh + res.TotalPowerExportT3kWh + res.TotalPowerExportT4kWh, err
}
return res.TotalPowerImportT1kWh + res.TotalPowerImportT2kWh + res.TotalPowerImportT3kWh + res.TotalPowerImportT4kWh, err
}

// Currents implements the api.PhaseCurrents interface
func (c *Connection) Currents() (float64, float64, float64, error) {
res, err := c.dataG.Get()
if c.usage == "pv" {
return -res.ActiveCurrentL1A, -res.ActiveCurrentL2A, -res.ActiveCurrentL3A, err
}
return res.ActiveCurrentL1A, res.ActiveCurrentL2A, res.ActiveCurrentL3A, err
}

Expand Down
4 changes: 4 additions & 0 deletions meter/homewizard/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type DataResponse struct {
TotalPowerImportT2kWh float64 `json:"total_power_import_t2_kwh"`
TotalPowerImportT3kWh float64 `json:"total_power_import_t3_kwh"`
TotalPowerImportT4kWh float64 `json:"total_power_import_t4_kwh"`
TotalPowerExportT1kWh float64 `json:"total_power_export_t1_kwh"`
TotalPowerExportT2kWh float64 `json:"total_power_export_t2_kwh"`
TotalPowerExportT3kWh float64 `json:"total_power_export_t3_kwh"`
TotalPowerExportT4kWh float64 `json:"total_power_export_t4_kwh"`
ActiveCurrentL1A float64 `json:"active_current_l1_a"`
ActiveCurrentL2A float64 `json:"active_current_l2_a"`
ActiveCurrentL3A float64 `json:"active_current_l3_a"`
Expand Down
27 changes: 24 additions & 3 deletions meter/homewizard/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ func TestUnmarshalStateResponse(t *testing.T) {
}
}

// Test DataResponse
func TestUnmarshalDataResponse(t *testing.T) {
// Test homewizard kWh Meter 1-Phase response
func TestUnmarshalKwhDataResponse(t *testing.T) {
{
var res DataResponse

// https://www.homewizard.com/shop/wi-fi-kwh-meter-1-phase/
jsonstr := `{"wifi_ssid": "My Wi-Fi","wifi_strength": 100,"total_power_import_t1_kwh": 30.511,"total_power_export_t1_kwh": 85.951,"active_power_w": 543,"active_power_l1_w": 28,"active_power_l2_w": 0,"active_power_l3_w": -181,"active_voltage_l1_v": 235.4,"active_voltage_l2_v": 235.8,"active_voltage_l3_v": 236.1,"active_current_l1_a": 1.19,"active_current_l2_a": 0.37,"active_current_l3_a": -0.93}`
require.NoError(t, json.Unmarshal([]byte(jsonstr), &res))

assert.Equal(t, float64(30.511), res.TotalPowerImportT1kWh+res.TotalPowerImportT2kWh+res.TotalPowerImportT3kWh+res.TotalPowerImportT4kWh)
assert.Equal(t, float64(85.951), res.TotalPowerExportT1kWh+res.TotalPowerExportT2kWh+res.TotalPowerExportT3kWh+res.TotalPowerExportT4kWh)
assert.Equal(t, float64(543), res.ActivePowerW)
assert.Equal(t, float64(235.4), res.ActiveVoltageL1V)
assert.Equal(t, float64(235.8), res.ActiveVoltageL2V)
Expand All @@ -51,3 +52,23 @@ func TestUnmarshalDataResponse(t *testing.T) {
assert.Equal(t, float64(-0.93), res.ActiveCurrentL3A)
}
}

// Test homewizard P1 Meter response
func TestUnmarshalP1DataResponse(t *testing.T) {
{
var res DataResponse
// https://www.homewizard.com/shop/wi-fi-p1-meter-rj12-2/
jsonstr := `{"wifi_ssid":"redacted","wifi_strength":78,"smr_version":50,"meter_model":"Landis + Gyr","unique_id":"redacted","active_tariff":2,"total_power_import_kwh":18664.997,"total_power_import_t1_kwh":10909.724,"total_power_import_t2_kwh":7755.273,"total_power_export_kwh":13823.608,"total_power_export_t1_kwh":4243.981,"total_power_export_t2_kwh":9579.627,"active_power_w":203.000,"active_power_l1_w":-21.000,"active_power_l2_w":57.000,"active_power_l3_w":168.000,"active_voltage_l1_v":228.000,"active_voltage_l2_v":226.000,"active_voltage_l3_v":225.000,"active_current_a":1.091,"active_current_l1_a":-0.092,"active_current_l2_a":0.252,"active_current_l3_a":0.747,"voltage_sag_l1_count":12.000,"voltage_sag_l2_count":12.000,"voltage_sag_l3_count":19.000,"voltage_swell_l1_count":5055.000,"voltage_swell_l2_count":1950.000,"voltage_swell_l3_count":0.000,"any_power_fail_count":12.000,"long_power_fail_count":2.000,"total_gas_m3":5175.363,"gas_timestamp":241106093006,"gas_unique_id":"redacted","external":[{"unique_id":"redacted","type":"gas_meter","timestamp":241106093006,"value":5175.363,"unit":"m3"}]}`
require.NoError(t, json.Unmarshal([]byte(jsonstr), &res))

assert.Equal(t, float64(18664.997), res.TotalPowerImportT1kWh+res.TotalPowerImportT2kWh+res.TotalPowerImportT3kWh+res.TotalPowerImportT4kWh)
assert.Equal(t, float64(13823.608), res.TotalPowerExportT1kWh+res.TotalPowerExportT2kWh+res.TotalPowerExportT3kWh+res.TotalPowerExportT4kWh)
assert.Equal(t, float64(203), res.ActivePowerW)
assert.Equal(t, float64(228), res.ActiveVoltageL1V)
assert.Equal(t, float64(226), res.ActiveVoltageL2V)
assert.Equal(t, float64(225), res.ActiveVoltageL3V)
assert.Equal(t, float64(-0.092), res.ActiveCurrentL1A)
assert.Equal(t, float64(0.252), res.ActiveCurrentL2A)
assert.Equal(t, float64(0.747), res.ActiveCurrentL3A)
}
}
13 changes: 13 additions & 0 deletions templates/definition/meter/homewizard-kwh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
template: homewizard-kwh
products:
- brand: HomeWizard
description:
generic: kWh Meter
params:
- name: usage
choice: ["pv"]
- name: host
render: |
type: homewizard
uri: http://{{ .host }}
usage: {{ .usage }}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ products:
generic: Wi-Fi P1 Meter
params:
- name: usage
choice: ["grid", "pv"]
choice: ["grid"]
- name: host
render: |
type: homewizard
uri: http://{{ .host }}
usage: {{ .usage }}

0 comments on commit 32332fe

Please sign in to comment.