From 32332fe8a265cc7f5665039fad039def47fff646 Mon Sep 17 00:00:00 2001 From: Markus Thierolf <77847348+thierolm@users.noreply.github.com> Date: Sat, 9 Nov 2024 16:04:08 +0100 Subject: [PATCH] Add Homewizard kWh meter (#17150) --- charger/homewizard.go | 7 ++--- meter/homewizard.go | 7 ++--- meter/homewizard/connection.go | 13 ++++++++- meter/homewizard/types.go | 4 +++ meter/homewizard/types_test.go | 27 ++++++++++++++++--- .../definition/meter/homewizard-kwh.yaml | 13 +++++++++ .../{homewizard.yaml => homewizard-p1.yaml} | 3 ++- 7 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 templates/definition/meter/homewizard-kwh.yaml rename templates/definition/meter/{homewizard.yaml => homewizard-p1.yaml} (81%) diff --git a/charger/homewizard.go b/charger/homewizard.go index 3a14bf81e0..445503095c 100644 --- a/charger/homewizard.go +++ b/charger/homewizard.go @@ -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 }{ @@ -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 } diff --git a/meter/homewizard.go b/meter/homewizard.go index 52d2f1d5bc..73233b4f11 100644 --- a/meter/homewizard.go +++ b/meter/homewizard.go @@ -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, @@ -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 } diff --git a/meter/homewizard/connection.go b/meter/homewizard/connection.go index 4411eca616..6aa3d3150e 100644 --- a/meter/homewizard/connection.go +++ b/meter/homewizard/connection.go @@ -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") } @@ -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()) @@ -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 } diff --git a/meter/homewizard/types.go b/meter/homewizard/types.go index 2ab9d97dc0..68a3110cfe 100644 --- a/meter/homewizard/types.go +++ b/meter/homewizard/types.go @@ -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"` diff --git a/meter/homewizard/types_test.go b/meter/homewizard/types_test.go index b05e889351..381c5844fd 100644 --- a/meter/homewizard/types_test.go +++ b/meter/homewizard/types_test.go @@ -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) @@ -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) + } +} diff --git a/templates/definition/meter/homewizard-kwh.yaml b/templates/definition/meter/homewizard-kwh.yaml new file mode 100644 index 0000000000..6f8b199419 --- /dev/null +++ b/templates/definition/meter/homewizard-kwh.yaml @@ -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 }} diff --git a/templates/definition/meter/homewizard.yaml b/templates/definition/meter/homewizard-p1.yaml similarity index 81% rename from templates/definition/meter/homewizard.yaml rename to templates/definition/meter/homewizard-p1.yaml index 6afc3bd167..4f2b70b50b 100644 --- a/templates/definition/meter/homewizard.yaml +++ b/templates/definition/meter/homewizard-p1.yaml @@ -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 }}