Meter (Home Assistant): support return energy entity (#31857)
This commit is contained in:
parent
3a3d7ad36b
commit
ba279718d2
2 changed files with 49 additions and 31 deletions
|
|
@ -21,6 +21,7 @@ func NewHomeAssistantFromConfig(other map[string]any) (api.Meter, error) {
|
|||
homeassistant.Config `mapstructure:",squash"`
|
||||
Power string
|
||||
Energy string
|
||||
ReturnEnergy string
|
||||
Currents []string
|
||||
Voltages []string
|
||||
Powers []string
|
||||
|
|
@ -64,37 +65,19 @@ func NewHomeAssistantFromConfig(other map[string]any) (api.Meter, error) {
|
|||
return conn.GetFloatState(cc.Power)
|
||||
})
|
||||
|
||||
// decorators for optional interfaces
|
||||
var energyG func() (float64, error)
|
||||
var currentsG, voltagesG, powersG func() (float64, float64, float64, error)
|
||||
|
||||
// energy
|
||||
if cc.Energy != "" {
|
||||
energyG = func() (float64, error) { return conn.GetFloatState(cc.Energy) }
|
||||
implement.Has(m, implement.MeterEnergy(func() (float64, error) {
|
||||
return conn.GetFloatState(cc.Energy)
|
||||
}))
|
||||
}
|
||||
|
||||
// phase currents (optional)
|
||||
if phases, err := homeassistant.ValidatePhaseEntities(cc.Currents); err != nil {
|
||||
return nil, fmt.Errorf("currents: %w", err)
|
||||
} else if len(phases) > 0 {
|
||||
currentsG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
|
||||
if cc.ReturnEnergy != "" {
|
||||
implement.Has(m, implement.MeterReturnEnergy(func() (float64, error) {
|
||||
return conn.GetFloatState(cc.ReturnEnergy)
|
||||
}))
|
||||
}
|
||||
|
||||
// phase voltages (optional)
|
||||
if phases, err := homeassistant.ValidatePhaseEntities(cc.Voltages); err != nil {
|
||||
return nil, fmt.Errorf("voltages: %w", err)
|
||||
} else if len(phases) > 0 {
|
||||
voltagesG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
|
||||
}
|
||||
|
||||
// phase powers (optional)
|
||||
if phases, err := homeassistant.ValidatePhaseEntities(cc.Powers); err != nil {
|
||||
return nil, fmt.Errorf("powers: %w", err)
|
||||
} else if len(phases) > 0 {
|
||||
powersG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
|
||||
}
|
||||
|
||||
implement.May(m, implement.MeterEnergy(energyG))
|
||||
|
||||
if cc.Soc != "" {
|
||||
socG := func() (float64, error) { return conn.GetFloatState(cc.Soc) }
|
||||
|
||||
|
|
@ -125,9 +108,33 @@ func NewHomeAssistantFromConfig(other map[string]any) (api.Meter, error) {
|
|||
return m, nil
|
||||
}
|
||||
|
||||
implement.May(m, implement.PhaseCurrents(currentsG))
|
||||
implement.May(m, implement.PhaseVoltages(voltagesG))
|
||||
implement.May(m, implement.PhasePowers(powersG))
|
||||
// phase currents (optional)
|
||||
if phases, err := homeassistant.ValidatePhaseEntities(cc.Currents); err != nil {
|
||||
return nil, fmt.Errorf("currents: %w", err)
|
||||
} else if len(phases) > 0 {
|
||||
implement.Has(m, implement.PhaseCurrents(func() (float64, float64, float64, error) {
|
||||
return conn.GetPhaseFloatStates(phases)
|
||||
}))
|
||||
}
|
||||
|
||||
// phase voltages (optional)
|
||||
if phases, err := homeassistant.ValidatePhaseEntities(cc.Voltages); err != nil {
|
||||
return nil, fmt.Errorf("voltages: %w", err)
|
||||
} else if len(phases) > 0 {
|
||||
implement.Has(m, implement.PhaseVoltages(func() (float64, float64, float64, error) {
|
||||
return conn.GetPhaseFloatStates(phases)
|
||||
}))
|
||||
}
|
||||
|
||||
// phase powers (optional)
|
||||
if phases, err := homeassistant.ValidatePhaseEntities(cc.Powers); err != nil {
|
||||
return nil, fmt.Errorf("powers: %w", err)
|
||||
} else if len(phases) > 0 {
|
||||
implement.Has(m, implement.PhasePowers(func() (float64, float64, float64, error) {
|
||||
return conn.GetPhaseFloatStates(phases)
|
||||
}))
|
||||
}
|
||||
|
||||
implement.May(m, implement.MaxACPowerGetter(cc.pvMaxACPower.Decorator()))
|
||||
|
||||
return m, nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue