Homeassistant meter: fix panic
This commit is contained in:
parent
9a9f73d6c2
commit
8ead6b4b59
1 changed files with 21 additions and 47 deletions
|
|
@ -13,11 +13,8 @@ import (
|
|||
|
||||
// HomeAssistant meter implementation
|
||||
type HomeAssistant struct {
|
||||
conn *homeassistant.Connection
|
||||
power string
|
||||
energy string
|
||||
currentEntities []string
|
||||
voltageEntities []string
|
||||
conn *homeassistant.Connection
|
||||
power string
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
@ -50,41 +47,33 @@ func NewHomeAssistantFromConfig(other map[string]interface{}) (api.Meter, error)
|
|||
}
|
||||
|
||||
m := &HomeAssistant{
|
||||
conn: conn,
|
||||
power: cc.Power,
|
||||
energy: cc.Energy,
|
||||
}
|
||||
|
||||
// Set up phase currents (optional)
|
||||
if len(cc.Currents) > 0 {
|
||||
currents, err := homeassistant.ValidatePhaseEntities(cc.Currents)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("currents: %w", err)
|
||||
}
|
||||
m.currentEntities = currents
|
||||
}
|
||||
|
||||
// Set up phase voltages (optional)
|
||||
if len(cc.Voltages) > 0 {
|
||||
voltages, err := homeassistant.ValidatePhaseEntities(cc.Voltages)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("voltages: %w", err)
|
||||
}
|
||||
m.voltageEntities = voltages
|
||||
conn: conn,
|
||||
power: cc.Power,
|
||||
}
|
||||
|
||||
// decorators for optional interfaces
|
||||
var energy func() (float64, error)
|
||||
var currents, voltages func() (float64, float64, float64, error)
|
||||
|
||||
if m.energy != "" {
|
||||
energy = m.totalEnergy
|
||||
if cc.Energy != "" {
|
||||
energy = func() (float64, error) { return conn.GetFloatState(cc.Energy) }
|
||||
}
|
||||
if m.currentEntities[0] != "" {
|
||||
currents = m.currents
|
||||
|
||||
if len(cc.Currents) > 0 {
|
||||
phases, err := homeassistant.ValidatePhaseEntities(cc.Currents)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("currents: %w", err)
|
||||
}
|
||||
currents = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
|
||||
}
|
||||
if m.voltageEntities[0] != "" {
|
||||
voltages = m.voltages
|
||||
|
||||
// Set up phase voltages (optional)
|
||||
if len(cc.Voltages) > 0 {
|
||||
phases, err := homeassistant.ValidatePhaseEntities(cc.Voltages)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("voltages: %w", err)
|
||||
}
|
||||
voltages = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
|
||||
}
|
||||
|
||||
return decorateHomeAssistant(m, energy, currents, voltages), nil
|
||||
|
|
@ -96,18 +85,3 @@ var _ api.Meter = (*HomeAssistant)(nil)
|
|||
func (m *HomeAssistant) CurrentPower() (float64, error) {
|
||||
return m.conn.GetFloatState(m.power)
|
||||
}
|
||||
|
||||
// totalEnergy implements the api.MeterEnergy interface
|
||||
func (m *HomeAssistant) totalEnergy() (float64, error) {
|
||||
return m.conn.GetFloatState(m.energy)
|
||||
}
|
||||
|
||||
// currents implements the api.PhaseCurrents interface
|
||||
func (m *HomeAssistant) currents() (float64, float64, float64, error) {
|
||||
return m.conn.GetPhaseFloatStates(m.currentEntities)
|
||||
}
|
||||
|
||||
// voltages implements the api.PhaseVoltages interface
|
||||
func (m *HomeAssistant) voltages() (float64, float64, float64, error) {
|
||||
return m.conn.GetPhaseFloatStates(m.voltageEntities)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue