Homeassistant: fix template required values (#24933)

This commit is contained in:
andig 2025-11-01 10:41:21 +01:00 • committed by GitHub
parent b41e535611
commit 06b92eb0cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 33 additions and 38 deletions

View file

@ -79,26 +79,18 @@ func NewHomeAssistantFromConfig(other map[string]any) (api.Charger, error) {
energy = func() (float64, error) { return conn.GetFloatState(cc.Energy) }
}
// Set up phase currents (optional)
if len(cc.Currents) > 0 {
res, err := homeassistant.ValidatePhaseEntities(cc.Currents)
if err != nil {
return nil, fmt.Errorf("currents: %w", err)
}
currents = func() (float64, float64, float64, error) {
return conn.GetPhaseFloatStates(res)
}
// phase currents (optional)
if phases, err := homeassistant.ValidatePhaseEntities(cc.Currents); len(phases) > 0 {
currents = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
} else if err != nil {
return nil, fmt.Errorf("currents: %w", err)
}
// Set up phase voltages (optional)
if len(cc.Voltages) > 0 {
res, err := homeassistant.ValidatePhaseEntities(cc.Voltages)
if err != nil {
return nil, fmt.Errorf("voltages: %w", err)
}
voltages = func() (float64, float64, float64, error) {
return conn.GetPhaseFloatStates(res)
}
// phase voltages (optional)
if phases, err := homeassistant.ValidatePhaseEntities(cc.Voltages); len(phases) > 0 {
voltages = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
} else if err != nil {
return nil, fmt.Errorf("voltages: %w", err)
}
return decorateHomeAssistant(c, power, energy, currents, voltages), nil

View file

@ -23,7 +23,7 @@ func init() {
// NewHomeAssistantFromConfig creates a HomeAssistant meter from generic config
func NewHomeAssistantFromConfig(other map[string]any) (api.Meter, error) {
cc := struct {
var cc struct {
URI string
Token string
Power string
@ -31,7 +31,7 @@ func NewHomeAssistantFromConfig(other map[string]any) (api.Meter, error) {
Currents []string
Voltages []string
Soc string
}{}
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
@ -61,21 +61,18 @@ func NewHomeAssistantFromConfig(other map[string]any) (api.Meter, error) {
energy = func() (float64, error) { return conn.GetFloatState(cc.Energy) }
}
if len(cc.Currents) > 0 {
phases, err := homeassistant.ValidatePhaseEntities(cc.Currents)
if err != nil {
return nil, fmt.Errorf("currents: %w", err)
}
// phase currents (optional)
if phases, err := homeassistant.ValidatePhaseEntities(cc.Currents); len(phases) > 0 {
currents = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
} else if err != nil {
return nil, fmt.Errorf("currents: %w", err)
}
// 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)
}
// phase voltages (optional)
if phases, err := homeassistant.ValidatePhaseEntities(cc.Voltages); len(phases) > 0 {
voltages = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) }
} else if err != nil {
return nil, fmt.Errorf("voltages: %w", err)
}
if cc.Soc != "" {

View file

@ -2,8 +2,8 @@ template: homeassistant
products:
- brand: Home Assistant
description:
de: Home Assistant Sensor
en: Home Assistant Sensor
de: Meter
en: Meter
group: generic
requirements:
description:

View file

@ -1,8 +1,9 @@
template: homeassistant
products:
- description:
de: Home Assistant Fahrzeug
en: Home Assistant Vehicle
- brand: Home Assistant
description:
de: Fahrzeug
en: Vehicle
group: generic
requirements:
description:

View file

@ -12,6 +12,7 @@ import (
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/samber/lo"
)
// Connection represents a Home Assistant API connection
@ -31,7 +32,7 @@ func NewConnection(log *util.Logger, uri, token string) (*Connection, error) {
c := &Connection{
Helper: request.NewHelper(log.Redact(token)),
uri: strings.TrimSuffix(uri, "/"),
uri: util.DefaultScheme(strings.TrimSuffix(uri, "/"), "http"),
}
// Set up authentication headers
@ -230,7 +231,11 @@ func (c *Connection) GetPhaseFloatStates(entities []string) (float64, float64, f
}
// ValidatePhaseEntities validates that phase entity arrays contain 1 or 3 entities
func ValidatePhaseEntities(entities []string) ([]string, error) {
func ValidatePhaseEntities(phases []string) ([]string, error) {
entities := lo.FilterMap(phases, func(s string, _ int) (string, bool) {
t := strings.TrimSpace(s)
return t, t != ""
})
switch len(entities) {
case 0:
return nil, nil