evcc-io/util/homeassistant/instance.go
Markus e5b9a014c5
Home Assistant: Enable insecure option for self-signed certificates in oauth2 flow (#30648)
Co-authored-by: andig <cpuidle@gmail.com>
Co-authored-by: Michael Geers <michael@geers.tv>
2026-06-18 16:37:04 +02:00

55 lines
931 B
Go

package homeassistant
import (
"fmt"
"sync"
"golang.org/x/oauth2"
)
type proxyInstance struct {
mu sync.Mutex
home, uri string
insecure bool
oauth2.TokenSource
}
func (inst *proxyInstance) URI() string {
inst.mu.Lock()
defer inst.mu.Unlock()
if inst.uri == "" && inst.home != "" {
// Try to resolve home name to URI (backward compatibility)
inst.uri = instanceUriByName(inst.home)
if inst.uri == "" {
return ""
}
}
return inst.uri
}
func (inst *proxyInstance) Token() (*oauth2.Token, error) {
uri := inst.URI()
if uri == "" {
if inst.home != "" {
return nil, fmt.Errorf("unknown instance: %s", inst.home)
}
return nil, fmt.Errorf("no URI configured")
}
inst.mu.Lock()
defer inst.mu.Unlock()
if inst.TokenSource == nil {
ts, err := NewHomeAssistant(uri, inst.insecure)
if err != nil {
return nil, err
}
inst.TokenSource = ts
}
return inst.TokenSource.Token()
}