@@ -37,8 +37,14 @@ type Kernel struct {
37
37
}
38
38
39
39
func main () {
40
+ var hassToken string
41
+ var hassHost string
42
+ var deviceName string
40
43
var configFlag string
41
44
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" )
42
48
flag .Parse ()
43
49
44
50
configFile , err := homePathFromString (configFlag )
@@ -62,10 +68,59 @@ func main() {
62
68
log .Fatalf ("failed to parse config file: %s" , err )
63
69
}
64
70
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
+
65
120
// Build the application kernel.
66
121
k := Kernel {
67
122
config : & config ,
68
- api : api .NewAPI (config . HomeAssistant . Host , config . HomeAssistant . Token ),
123
+ api : api .NewAPI (hassHost , hassToken , deviceName ),
69
124
}
70
125
71
126
// Start the main process.
@@ -241,12 +296,13 @@ func (k *Kernel) getRegistration(ctx context.Context) (api.Registration, error)
241
296
func (k * Kernel ) registerDevice (ctx context.Context ) (api.Registration , error ) {
242
297
id := util .RandomString (8 )
243
298
token := util .RandomString (8 )
299
+
244
300
registration , err := k .api .RegisterDevice (ctx , api.RegisterDeviceRequest {
245
301
DeviceID : id ,
246
302
AppID : "homeassistant-desktop-companion" ,
247
303
AppName : "Home Assistant Desktop Companion" ,
248
304
AppVersion : Version ,
249
- DeviceName : k .config . HomeAssistant .DeviceName ,
305
+ DeviceName : k .api .DeviceName ,
250
306
SupportsEncryption : false ,
251
307
AppData : api.AppData {
252
308
PushToken : token ,
0 commit comments