forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
358 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package shelly | ||
|
||
import "github.com/evcc-io/evcc/api" | ||
|
||
type EnergyMeter struct { | ||
*Connection | ||
} | ||
|
||
func NewEnergyMeter(conn *Connection) *EnergyMeter { | ||
res := &EnergyMeter{ | ||
Connection: conn, | ||
} | ||
|
||
return res | ||
} | ||
|
||
// CurrentPower implements the api.Meter interface | ||
func (sh *EnergyMeter) CurrentPower() (float64, error) { | ||
var res Gen2EmStatusResponse | ||
if err := sh.Connection.execGen2Cmd("EM.GetStatus", false, &res); err != nil { | ||
return 0, err | ||
} | ||
|
||
return res.TotalPower, nil | ||
} | ||
|
||
// TotalEnergy implements the api.Meter interface | ||
func (sh *EnergyMeter) TotalEnergy() (float64, error) { | ||
var res Gen2EmDataStatusResponse | ||
if err := sh.Connection.execGen2Cmd("EMData.GetStatus", false, &res); err != nil { | ||
return 0, err | ||
} | ||
|
||
return res.TotalEnergy, nil | ||
} | ||
|
||
var _ api.PhaseCurrents = (*EnergyMeter)(nil) | ||
|
||
// Currents implements the api.PhaseCurrents interface | ||
func (sh *EnergyMeter) Currents() (float64, float64, float64, error) { | ||
var res Gen2EmStatusResponse | ||
if err := sh.Connection.execGen2Cmd("EM.GetStatus", false, &res); err != nil { | ||
return 0, 0, 0, err | ||
} | ||
|
||
return res.CurrentA, res.CurrentB, res.CurrentC, nil | ||
} | ||
|
||
var _ api.PhaseVoltages = (*EnergyMeter)(nil) | ||
|
||
// Voltages implements the api.PhaseVoltages interface | ||
func (sh *EnergyMeter) Voltages() (float64, float64, float64, error) { | ||
var res Gen2EmStatusResponse | ||
if err := sh.Connection.execGen2Cmd("EM.GetStatus", false, &res); err != nil { | ||
return 0, 0, 0, err | ||
} | ||
|
||
return res.VoltageA, res.VoltageB, res.VoltageC, nil | ||
} | ||
|
||
var _ api.PhasePowers = (*EnergyMeter)(nil) | ||
|
||
// Powers implements the api.PhasePowers interface | ||
func (sh *EnergyMeter) Powers() (float64, float64, float64, error) { | ||
var res Gen2EmStatusResponse | ||
if err := sh.Connection.execGen2Cmd("EM.GetStatus", false, &res); err != nil { | ||
return 0, 0, 0, err | ||
} | ||
|
||
return res.PowerA, res.PowerB, res.PowerC, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package shelly | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Switch struct { | ||
*Connection | ||
} | ||
|
||
func NewSwitch(conn *Connection) *Switch { | ||
res := &Switch{ | ||
Connection: conn, | ||
} | ||
|
||
return res | ||
} | ||
|
||
// CurrentPower implements the api.Meter interface | ||
func (sh *Switch) CurrentPower() (float64, error) { | ||
var power float64 | ||
|
||
d := sh.Connection | ||
switch d.gen { | ||
case 0, 1: | ||
var res Gen1StatusResponse | ||
uri := fmt.Sprintf("%s/status", d.uri) | ||
if err := d.GetJSON(uri, &res); err != nil { | ||
return 0, err | ||
} | ||
|
||
switch { | ||
case d.channel < len(res.Meters): | ||
power = res.Meters[d.channel].Power | ||
case d.channel < len(res.EMeters): | ||
power = res.EMeters[d.channel].Power | ||
default: | ||
return 0, errors.New("invalid channel, missing power meter") | ||
} | ||
|
||
default: | ||
var res Gen2StatusResponse | ||
if err := d.execGen2Cmd("Shelly.GetStatus", false, &res); err != nil { | ||
return 0, err | ||
} | ||
|
||
switch d.channel { | ||
case 1: | ||
power = res.Switch1.Apower | ||
case 2: | ||
power = res.Switch2.Apower | ||
default: | ||
power = res.Switch0.Apower | ||
} | ||
} | ||
|
||
return power, nil | ||
} | ||
|
||
// Enabled implements the api.Charger interface | ||
func (sh *Switch) Enabled() (bool, error) { | ||
d := sh.Connection | ||
switch d.gen { | ||
case 0, 1: | ||
var res Gen1SwitchResponse | ||
uri := fmt.Sprintf("%s/relay/%d", d.uri, d.channel) | ||
err := d.GetJSON(uri, &res) | ||
return res.Ison, err | ||
|
||
default: | ||
var res Gen2SwitchResponse | ||
err := d.execGen2Cmd("Switch.GetStatus", false, &res) | ||
return res.Output, err | ||
} | ||
} | ||
|
||
// Enable implements the api.Charger interface | ||
func (sh *Switch) Enable(enable bool) error { | ||
var err error | ||
onoff := map[bool]string{true: "on", false: "off"} | ||
|
||
d := sh.Connection | ||
switch d.gen { | ||
case 0, 1: | ||
var res Gen1SwitchResponse | ||
uri := fmt.Sprintf("%s/relay/%d?turn=%s", d.uri, d.channel, onoff[enable]) | ||
err = d.GetJSON(uri, &res) | ||
|
||
default: | ||
var res Gen2SwitchResponse | ||
err = d.execGen2Cmd("Switch.Set", enable, &res) | ||
} | ||
|
||
return err | ||
} | ||
|
||
// TotalEnergy implements the api.Meter interface | ||
func (sh *Switch) TotalEnergy() (float64, error) { | ||
var energy float64 | ||
|
||
d := sh.Connection | ||
switch d.gen { | ||
case 0, 1: | ||
var res Gen1StatusResponse | ||
uri := fmt.Sprintf("%s/status", d.uri) | ||
if err := d.GetJSON(uri, &res); err != nil { | ||
return 0, err | ||
} | ||
|
||
switch { | ||
case d.channel < len(res.Meters): | ||
energy = res.Meters[d.channel].Total | ||
case d.channel < len(res.EMeters): | ||
energy = res.EMeters[d.channel].Total | ||
default: | ||
return 0, errors.New("invalid channel, missing power meter") | ||
} | ||
|
||
energy = gen1Energy(d.devicetype, energy) | ||
|
||
default: | ||
var res Gen2StatusResponse | ||
if err := d.execGen2Cmd("Shelly.GetStatus", false, &res); err != nil { | ||
return 0, err | ||
} | ||
|
||
switch d.channel { | ||
case 1: | ||
energy = res.Switch1.Aenergy.Total | ||
case 2: | ||
energy = res.Switch2.Aenergy.Total | ||
default: | ||
energy = res.Switch0.Aenergy.Total | ||
} | ||
} | ||
|
||
return energy / 1000, nil | ||
} | ||
|
||
// gen1Energy in kWh | ||
func gen1Energy(devicetype string, energy float64) float64 { | ||
// Gen 1 Shelly EM devices are providing Watt hours, Shelly EM devices are providing Watt minutes | ||
if !strings.Contains(devicetype, "EM") { | ||
energy = energy / 60 | ||
} | ||
return energy | ||
} |
Oops, something went wrong.