@@ -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_cached_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 ))
224
228
--- @return boolean ? auth_success true if the driver can authenticate against the provided arguments , false otherwise
225
229
--- @return string ? api_key_or_err if ` auth_success` is true , this will be the API key that is known to auth. If ` auth_success` is false , this will be nil. If ` auth_success` is ` nil` , this will be an error message.
226
230
function SonosDriver :check_auth (info_or_device )
227
- local maybe_token , _ = self :get_oauth_token ()
231
+ local maybe_token , _ = self :get_cached_oauth_token ()
228
232
229
233
local token_valid = (api_version >= 14 and security ~= nil )
230
234
and self .oauth
@@ -322,30 +326,45 @@ 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 ()
328
343
if api_version < 14 or security == nil then
329
344
return nil , " not supported"
330
345
end
331
- local maybe_token , maybe_err = self :get_oauth_token ()
346
+ local maybe_token , maybe_err = self :get_cached_oauth_token ()
332
347
if maybe_err then
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
346
365
--- @return { accessToken : string , expiresAt : number }? the token if a currently valid token is available , nil if not
347
366
--- @return " token expired" | " no token" | " not supported" | nil reason the reason a token was not provided , nil if there is a valid token available
348
- function SonosDriver :get_oauth_token ()
367
+ function SonosDriver :get_cached_oauth_token ()
349
368
if api_version < 14 or security == nil then
350
369
return nil , " not supported"
351
370
end
@@ -494,7 +513,7 @@ function SonosDriver:handle_player_discovery_info(api_key, info, device)
494
513
api_key = api_key or self :get_fallback_api_key ()
495
514
496
515
local rest_url = net_url .parse (info .discovery_info .restUrl )
497
- local maybe_token , no_token_reason = self :get_oauth_token ()
516
+ local maybe_token , no_token_reason = self :get_cached_oauth_token ()
498
517
local headers = SonosApi .make_headers (api_key , maybe_token and maybe_token .accessToken )
499
518
local response , response_err =
500
519
SonosApi .RestApi .get_groups_info (rest_url , info .ssdp_info .household_id , headers )
0 commit comments