Meter (Home Assistant): support return energy entity (#31857)

This commit is contained in:
github-actions[bot] 2026-07-17 09:03:24 +02:00 • committed by GitHub
parent 3a3d7ad36b
commit ba279718d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 31 deletions

View file

@ -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