HomeAssistant: allow inverting entities by prepending minus sign (#28020)

This commit is contained in:
andig 2026-03-08 13:23:37 +01:00 • committed by GitHub
parent cc4beb0b99
commit 77e1c8825a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 9 deletions

View file

@ -68,24 +68,24 @@ func NewHomeAssistantFromConfig(other map[string]any) (api.Meter, error) {
}
// phase currents (optional)
if phases, err := homeassistant.ValidatePhaseEntities(cc.Currents); len(phases) > 0 {
currentsG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
} else if err != nil {
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) }
}
// phase voltages (optional)
if phases, err := homeassistant.ValidatePhaseEntities(cc.Voltages); len(phases) > 0 {
voltagesG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
} else if err != nil {
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); len(phases) > 0 {
powersG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
} else if err != nil {
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) }
}
if cc.Soc != "" {

View file

@ -103,6 +103,9 @@ func (c *Connection) GetIntState(entity string) (int64, error) {
// GetFloatState retrieves the state of an entity as float64
func (c *Connection) GetFloatState(entity string) (float64, error) {
// leading minus sign?
entity, invert := strings.CutPrefix(entity, "-")
state, err := c.GetState(entity)
if err != nil {
return 0, err
@ -118,6 +121,10 @@ func (c *Connection) GetFloatState(entity string) (float64, error) {
return 0, fmt.Errorf("%w for entity %s", err, entity)
}
if invert {
value = -value
}
return scale * value, nil
}