HomeAssistant: use instance name if available (#25701)

This commit is contained in:
andig 2025-12-01 08:58:39 +01:00 • committed by GitHub
parent 9fef8d45e3
commit 9fdcba86bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"net"
"net/url"
"strings"
"github.com/evcc-io/evcc/plugin/auth"
"github.com/evcc-io/evcc/server/network"
@ -42,6 +43,8 @@ func NewHomeAssistantFromConfig(other map[string]any) (oauth2.TokenSource, error
}
func NewHomeAssistant(uri string) (oauth2.TokenSource, error) {
uri = strings.TrimRight(uri, "/") // normalize
extUrl := network.Config().ExternalURL()
redirectUri := extUrl + network.CallbackPath
@ -68,5 +71,10 @@ func NewHomeAssistant(uri string) (oauth2.TokenSource, error) {
host = h
}
// use instance name instead of host if discovered on mDNS
if name := instanceNameByUri(uri); name != "" {
host = name
}
return auth.NewOAuth(ctx, "HomeAssistant", host, &oc)
}

View file

@ -25,10 +25,23 @@ func instanceUriByName(name string) string {
return instances[name]
}
func instanceNameByUri(uri string) string {
mu.Lock()
defer mu.Unlock()
for n, u := range instances {
if uri == u {
return n
}
}
return ""
}
func addInstance(name, uri string) {
mu.Lock()
defer mu.Unlock()
instances[name] = uri
instances[name] = strings.TrimRight(uri, "/")
}
func scan() {