Add HomeWizard (#9102)
This commit is contained in:
parent
60ab9cc666
commit
642b457618
11 changed files with 326 additions and 17 deletions
100
charger/homewizard.go
Normal file
100
charger/homewizard.go
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
package charger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/meter/homewizard"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
)
|
||||
|
||||
// HomeWizard project homepage
|
||||
// https://homewizard-energy-api.readthedocs.io/index.html
|
||||
|
||||
// HomeWizard charger implementation
|
||||
type HomeWizard struct {
|
||||
conn *homewizard.Connection
|
||||
*switchSocket
|
||||
}
|
||||
|
||||
func init() {
|
||||
registry.Add("homewizard", NewHomeWizardFromConfig)
|
||||
}
|
||||
|
||||
// NewHomeWizardFromConfig creates a HomeWizard charger from generic config
|
||||
func NewHomeWizardFromConfig(other map[string]interface{}) (api.Charger, error) {
|
||||
var cc = struct {
|
||||
embed `mapstructure:",squash"`
|
||||
URI string
|
||||
StandbyPower float64
|
||||
}{}
|
||||
|
||||
if err := util.DecodeOther(other, &cc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewHomeWizard(cc.embed, cc.URI, cc.StandbyPower)
|
||||
}
|
||||
|
||||
// NewHomeWizard creates HomeWizard charger
|
||||
func NewHomeWizard(embed embed, uri string, standbypower float64) (*HomeWizard, error) {
|
||||
conn, err := homewizard.NewConnection(uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &HomeWizard{
|
||||
conn: conn,
|
||||
}
|
||||
|
||||
// Check compatible product type
|
||||
if c.conn.ProductType != "HWE-SKT" {
|
||||
return nil, errors.New("not supported product type: " + c.conn.ProductType)
|
||||
}
|
||||
|
||||
c.switchSocket = NewSwitchSocket(&embed, c.Enabled, c.conn.CurrentPower, standbypower)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Enabled implements the api.Charger interface
|
||||
func (c *HomeWizard) Enabled() (bool, error) {
|
||||
var res homewizard.StateResponse
|
||||
err := c.conn.GetJSON(fmt.Sprintf("%s/data", c.conn.URI), &res)
|
||||
return res.PowerOn, err
|
||||
}
|
||||
|
||||
// Enable implements the api.Charger interface
|
||||
func (c *HomeWizard) Enable(enable bool) error {
|
||||
var res homewizard.StateResponse
|
||||
data := map[string]interface{}{
|
||||
"power_on": enable,
|
||||
}
|
||||
|
||||
req, err := request.New(http.MethodPut, fmt.Sprintf("%s/state", c.conn.URI), request.MarshalJSON(data), request.JSONEncoding)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.conn.DoJSON(req, &res); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch {
|
||||
case enable && !res.PowerOn:
|
||||
return errors.New("switchOn failed")
|
||||
case !enable && res.PowerOn:
|
||||
return errors.New("switchOff failed")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var _ api.MeterEnergy = (*HomeWizard)(nil)
|
||||
|
||||
// TotalEnergy implements the api.MeterEnergy interface
|
||||
func (c *HomeWizard) TotalEnergy() (float64, error) {
|
||||
return c.conn.TotalEnergy()
|
||||
}
|
||||
58
meter/homewizard.go
Normal file
58
meter/homewizard.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package meter
|
||||
|
||||
import (
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/meter/homewizard"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
)
|
||||
|
||||
// HomeWizard meter implementation
|
||||
type HomeWizard struct {
|
||||
conn *homewizard.Connection
|
||||
}
|
||||
|
||||
// HomeWizard meter implementation
|
||||
func init() {
|
||||
registry.Add("homewizard", NewHomeWizardFromConfig)
|
||||
}
|
||||
|
||||
// NewHomeWizardFromConfig creates a HomeWizard meter from generic config
|
||||
func NewHomeWizardFromConfig(other map[string]interface{}) (api.Meter, error) {
|
||||
cc := struct {
|
||||
URI string
|
||||
}{}
|
||||
|
||||
if err := util.DecodeOther(other, &cc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewHomeWizard(cc.URI)
|
||||
}
|
||||
|
||||
// NewHomeWizard creates HomeWizard meter
|
||||
func NewHomeWizard(uri string) (*HomeWizard, error) {
|
||||
conn, err := homewizard.NewConnection(uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &HomeWizard{
|
||||
conn: conn,
|
||||
}
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
var _ api.Meter = (*HomeWizard)(nil)
|
||||
|
||||
// CurrentPower implements the api.Meter interface
|
||||
func (c *HomeWizard) CurrentPower() (float64, error) {
|
||||
return c.conn.CurrentPower()
|
||||
}
|
||||
|
||||
var _ api.MeterEnergy = (*HomeWizard)(nil)
|
||||
|
||||
// TotalEnergy implements the api.MeterEnergy interface
|
||||
func (c *HomeWizard) TotalEnergy() (float64, error) {
|
||||
return c.conn.TotalEnergy()
|
||||
}
|
||||
61
meter/homewizard/connection.go
Normal file
61
meter/homewizard/connection.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package homewizard
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/util/transport"
|
||||
)
|
||||
|
||||
// Connection is the homewizard connection
|
||||
type Connection struct {
|
||||
*request.Helper
|
||||
URI string
|
||||
ProductType string
|
||||
}
|
||||
|
||||
// NewConnection creates a homewizard connection
|
||||
func NewConnection(uri string) (*Connection, error) {
|
||||
if uri == "" {
|
||||
return nil, errors.New("missing uri")
|
||||
}
|
||||
|
||||
log := util.NewLogger("homewizard")
|
||||
c := &Connection{
|
||||
Helper: request.NewHelper(log),
|
||||
URI: fmt.Sprintf("%s/api", util.DefaultScheme(strings.TrimRight(uri, "/"), "http")),
|
||||
}
|
||||
|
||||
c.Client.Transport = request.NewTripper(log, transport.Insecure())
|
||||
|
||||
// Check and set API version + product type
|
||||
var res ApiResponse
|
||||
if err := c.GetJSON(c.URI, &res); err != nil {
|
||||
return c, err
|
||||
}
|
||||
if res.ApiVersion != "v1" {
|
||||
return nil, errors.New("not supported api version: " + res.ApiVersion)
|
||||
}
|
||||
|
||||
c.URI = c.URI + "/" + res.ApiVersion
|
||||
c.ProductType = res.ProductType
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// CurrentPower implements the api.Meter interface
|
||||
func (d *Connection) CurrentPower() (float64, error) {
|
||||
var res DataResponse
|
||||
err := d.GetJSON(fmt.Sprintf("%s/data", d.URI), &res)
|
||||
return res.ActivePowerW, err
|
||||
}
|
||||
|
||||
// TotalEnergy implements the api.MeterEnergy interface
|
||||
func (d *Connection) TotalEnergy() (float64, error) {
|
||||
var res DataResponse
|
||||
err := d.GetJSON(fmt.Sprintf("%s/data", d.URI), &res)
|
||||
return res.TotalPowerImportT1kWh + res.TotalPowerImportT2kWh + res.TotalPowerImportT3kWh + res.TotalPowerImportT4kWh, err
|
||||
}
|
||||
24
meter/homewizard/types.go
Normal file
24
meter/homewizard/types.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package homewizard
|
||||
|
||||
// ApiResponse returns allows you to get basic information from the HomeWizard Energy Socket
|
||||
// https://homewizard-energy-api.readthedocs.io/endpoints.html#basic-information-api
|
||||
type ApiResponse struct {
|
||||
ProductType string `json:"product_type"`
|
||||
ApiVersion string `json:"api_version"`
|
||||
}
|
||||
|
||||
// StateResponse returns the actual state of the HomeWizard Energy Socket
|
||||
// https://homewizard-energy-api.readthedocs.io/endpoints.html#recent-measurement-api-v1-data
|
||||
type StateResponse struct {
|
||||
PowerOn bool `json:"power_on"`
|
||||
}
|
||||
|
||||
// DataResponse returns the most recent measurements from the HomeWizard device
|
||||
// https://homewizard-energy-api.readthedocs.io/endpoints.html#state-api-v1-state
|
||||
type DataResponse struct {
|
||||
ActivePowerW float64 `json:"active_power_w"`
|
||||
TotalPowerImportT1kWh float64 `json:"total_power_import_t1_kwh"`
|
||||
TotalPowerImportT2kWh float64 `json:"total_power_import_t2_kwh"`
|
||||
TotalPowerImportT3kWh float64 `json:"total_power_import_t3_kwh"`
|
||||
TotalPowerImportT4kWh float64 `json:"total_power_import_t4_kwh"`
|
||||
}
|
||||
46
meter/homewizard/types_test.go
Normal file
46
meter/homewizard/types_test.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package homewizard
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Test ApiResponse
|
||||
func TestUnmarshalApiResponse(t *testing.T) {
|
||||
{
|
||||
var res ApiResponse
|
||||
|
||||
jsonstr := `{"product_type": "HWE-SKT","product_name": "P1 Meter","serial": "3c39e7aabbcc","firmware_version": "2.11","api_version": "v1"}`
|
||||
assert.NoError(t, json.Unmarshal([]byte(jsonstr), &res))
|
||||
|
||||
assert.Equal(t, "HWE-SKT", res.ProductType)
|
||||
assert.Equal(t, "v1", res.ApiVersion)
|
||||
}
|
||||
}
|
||||
|
||||
// Test StateResponse
|
||||
func TestUnmarshalStateResponse(t *testing.T) {
|
||||
{
|
||||
var res StateResponse
|
||||
|
||||
jsonstr := `{"power_on": true,"switch_lock": false,"brightness": 255}`
|
||||
assert.NoError(t, json.Unmarshal([]byte(jsonstr), &res))
|
||||
|
||||
assert.Equal(t, true, res.PowerOn)
|
||||
}
|
||||
}
|
||||
|
||||
// Test DataResponse
|
||||
func TestUnmarshalDataResponse(t *testing.T) {
|
||||
{
|
||||
var res DataResponse
|
||||
|
||||
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": 676}`
|
||||
assert.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(543), res.ActivePowerW)
|
||||
}
|
||||
}
|
||||
10
templates/definition/charger/homewizard.yaml
Normal file
10
templates/definition/charger/homewizard.yaml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
template: homewizard
|
||||
products:
|
||||
- brand: HomeWizard
|
||||
group: switchsockets
|
||||
params:
|
||||
- name: host
|
||||
- preset: switchsocket
|
||||
render: |
|
||||
type: homewizard
|
||||
uri: http://{{ .host }}
|
||||
|
|
@ -3,7 +3,6 @@ products:
|
|||
- brand: Tasmota
|
||||
group: switchsockets
|
||||
params:
|
||||
- name: host
|
||||
- name: host
|
||||
- name: user
|
||||
required: false
|
||||
|
|
|
|||
|
|
@ -5,22 +5,9 @@ products:
|
|||
generic: Wi-Fi P1 Meter
|
||||
params:
|
||||
- name: usage
|
||||
choice: ["grid"]
|
||||
choice: ["grid", "pv"]
|
||||
- name: host
|
||||
required: true
|
||||
render: |
|
||||
type: custom
|
||||
power:
|
||||
source: http
|
||||
uri: http://{{ .host }}/api/v1/data
|
||||
method: GET
|
||||
headers:
|
||||
- content-type: application/json
|
||||
jq: .active_power_w
|
||||
energy:
|
||||
source: http
|
||||
uri: http://{{ .host }}/api/v1/data
|
||||
method: GET
|
||||
headers:
|
||||
- content-type: application/json
|
||||
jq: '[to_entries[] | select(.key|startswith("total_power_import_t")) | .value] | add'
|
||||
type: homewizard
|
||||
uri: http://{{ .host }}
|
||||
|
|
|
|||
16
templates/docs/charger/homewizard_0.yaml
Normal file
16
templates/docs/charger/homewizard_0.yaml
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
product:
|
||||
brand: HomeWizard
|
||||
group: Schaltbare Steckdosen
|
||||
render:
|
||||
- default: |
|
||||
type: template
|
||||
template: homewizard
|
||||
host: 192.0.2.2 # IP-Adresse oder Hostname
|
||||
standbypower: 15 # Leistung oberhalb des angegebenen Wertes wird als Ladeleistung gewertet (Optional)
|
||||
advanced: |
|
||||
type: template
|
||||
template: homewizard
|
||||
host: 192.0.2.2 # IP-Adresse oder Hostname
|
||||
standbypower: 15 # Leistung oberhalb des angegebenen Wertes wird als Ladeleistung gewertet (Optional)
|
||||
integrateddevice: # Optional
|
||||
icon: # Icon in der Benutzeroberfläche (Optional)
|
||||
|
|
@ -8,3 +8,9 @@ render:
|
|||
template: homewizard
|
||||
usage: grid
|
||||
host: 192.0.2.2 # IP-Adresse oder Hostname
|
||||
- usage: pv
|
||||
default: |
|
||||
type: template
|
||||
template: homewizard
|
||||
usage: pv
|
||||
host: 192.0.2.2 # IP-Adresse oder Hostname
|
||||
|
|
|
|||
|
|
@ -67,6 +67,7 @@
|
|||
"SmartPlugs": [
|
||||
"AVM",
|
||||
"Homematic / Homematic IP",
|
||||
"HomeWizard",
|
||||
"myStrom",
|
||||
"Shelly",
|
||||
"Tasmota",
|
||||
|
|
@ -146,6 +147,7 @@
|
|||
"GoodWe",
|
||||
"Growatt",
|
||||
"Homematic IP",
|
||||
"HomeWizard",
|
||||
"Hoymiles",
|
||||
"Huawei",
|
||||
"IGEN Tech",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue