@@ -184,11 +184,15 @@ function SonosDriver:notify_augmented_data_changed(update_kind, update_key, upda
184
184
)
185
185
end
186
186
187
- if
188
- self .oauth .endpoint_app_info
187
+ local maybe_token , _ = self :get_oauth_token ()
188
+
189
+ local should_request_token = self .oauth .endpoint_app_info
189
190
and self .oauth .endpoint_app_info .state == " connected"
190
191
and not already_connected
191
- then
192
+ and type (maybe_token ) ~= " table"
193
+ and not self :is_waiting_for_oauth_token ()
194
+
195
+ if should_request_token then
192
196
local _ , err = self :request_oauth_token ()
193
197
if err then
194
198
log .error (string.format (" Request OAuth token error: %s" , err ))
@@ -322,6 +326,17 @@ function SonosDriver:check_auth(info_or_device)
322
326
)
323
327
end
324
328
329
+ --- @return boolean is_connected
330
+ function SonosDriver :is_account_linked ()
331
+ return (
332
+ self .oauth
333
+ and self .oauth .endpoint_app_info
334
+ and self .oauth .endpoint_app_info .state == " connected"
335
+ )
336
+ and true
337
+ or false
338
+ end
339
+
325
340
--- @return any ? ret nil on permissions violation
326
341
--- @return string ? error nil on success
327
342
function SonosDriver :request_oauth_token ()
@@ -333,13 +348,17 @@ function SonosDriver:request_oauth_token()
333
348
log .warn (string.format (" get oauth token error: %s" , maybe_err ))
334
349
end
335
350
if type (maybe_token ) == " table" and type (maybe_token .accessToken ) == " string" then
351
+ self .waiting_for_oauth_token = false
336
352
self .oauth_token_bus :send (maybe_token )
353
+ return true
337
354
end
338
355
local result , err = security .get_sonos_oauth ()
339
356
if not result then
340
357
return nil , string.format (" Error requesting OAuth token via Security API: %s" , err )
341
358
end
342
- self .waiting_for_oauth_token = true
359
+ -- if the account isn't linked, then we're not actually "waiting" for the token yet,
360
+ -- because we need to wait for the account link to succeed and the endpoint app upsert
361
+ self .waiting_for_oauth_token = self :is_account_linked ()
343
362
return result , err
344
363
end
345
364
0 commit comments