HomeAssistant: allow inverting entities by prepending minus sign (#28020)
This commit is contained in:
parent
cc4beb0b99
commit
77e1c8825a
2 changed files with 16 additions and 9 deletions
|
|
@ -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 != "" {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue