Skip to content

Commit 30525dc

Browse files
Merge pull request #5 from pschmitt/flags-and-env-vars
2 parents 0602b27 + a0e8163 commit 30525dc

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

api/api.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,17 @@ func (r Registration) JSON() ([]byte, error) {
110110
type API struct {
111111
Host string
112112
Token string
113+
DeviceName string
113114
client http.Client
114115
Registration Registration
115116
}
116117

117-
func NewAPI(host, token string) *API {
118+
func NewAPI(host, token string, deviceName string) *API {
118119
return &API{
119-
Host: host,
120-
Token: token,
121-
client: http.Client{Timeout: 5 * time.Second},
120+
Host: host,
121+
Token: token,
122+
DeviceName: deviceName,
123+
client: http.Client{Timeout: 5 * time.Second},
122124
}
123125
}
124126

main.go

+58-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ type Kernel struct {
3737
}
3838

3939
func main() {
40+
var hassToken string
41+
var hassHost string
42+
var deviceName string
4043
var configFlag string
4144
flag.StringVar(&configFlag, "config", "~/.config/hacompanion.toml", "Path to the config file")
45+
flag.StringVar(&hassHost, "host", "", "Home Assistant host")
46+
flag.StringVar(&hassToken, "token", "", "Long-lived access token")
47+
flag.StringVar(&deviceName, "device-name", "", "Device name")
4248
flag.Parse()
4349

4450
configFile, err := homePathFromString(configFlag)
@@ -62,10 +68,59 @@ func main() {
6268
log.Fatalf("failed to parse config file: %s", err)
6369
}
6470

71+
// Home Assistant host/url
72+
// The --host flag takes precedence the env var
73+
// if neither the flag nor the env var are set, use the value from the config
74+
// file
75+
// Default value: homeassistant.local
76+
if hassHost == "" {
77+
hassHost = os.Getenv("HASS_HOST")
78+
if hassHost == "" {
79+
hassHost = config.HomeAssistant.Host
80+
}
81+
// Default to homeassistant.local
82+
if hassHost == "" {
83+
hassHost = "http://homeassistant.local:8123"
84+
}
85+
}
86+
87+
// Home Assistant token
88+
// The --token flag takes precedence the HASS_TOKEN env var
89+
// if neither the flag nor the env var are set, use the value in the config
90+
// file
91+
if hassToken == "" {
92+
hassToken = os.Getenv("HASS_TOKEN")
93+
if hassToken == "" {
94+
hassToken = config.HomeAssistant.Token
95+
}
96+
}
97+
98+
// device name
99+
// The --device-name flag takes precedence the HASS_TOKEN env var
100+
// if neither the flag nor the env var are set, use the value in the config
101+
// file
102+
// Default value: current hostname
103+
if deviceName == "" {
104+
deviceName = os.Getenv("HASS_DEVICE_NAME")
105+
// Fall back to device name set in config
106+
if deviceName == "" {
107+
deviceName = config.HomeAssistant.DeviceName
108+
}
109+
if deviceName == "" {
110+
// Fall back to system hostname if device name is unset in config
111+
hostname, err := os.Hostname()
112+
if err != nil {
113+
log.Fatalf("failed to determine hostname: %s. Please set device name via HASS_DEVICE_NAME or the config file", err)
114+
}
115+
116+
deviceName = hostname
117+
}
118+
}
119+
65120
// Build the application kernel.
66121
k := Kernel{
67122
config: &config,
68-
api: api.NewAPI(config.HomeAssistant.Host, config.HomeAssistant.Token),
123+
api: api.NewAPI(hassHost, hassToken, deviceName),
69124
}
70125

71126
// Start the main process.
@@ -241,12 +296,13 @@ func (k *Kernel) getRegistration(ctx context.Context) (api.Registration, error)
241296
func (k *Kernel) registerDevice(ctx context.Context) (api.Registration, error) {
242297
id := util.RandomString(8)
243298
token := util.RandomString(8)
299+
244300
registration, err := k.api.RegisterDevice(ctx, api.RegisterDeviceRequest{
245301
DeviceID: id,
246302
AppID: "homeassistant-desktop-companion",
247303
AppName: "Home Assistant Desktop Companion",
248304
AppVersion: Version,
249-
DeviceName: k.config.HomeAssistant.DeviceName,
305+
DeviceName: k.api.DeviceName,
250306
SupportsEncryption: false,
251307
AppData: api.AppData{
252308
PushToken: token,

0 commit comments

Comments
 (0)