Skip to content

Commit 150bf68

Browse files
committed
fix: spurious oauth token requests when we already have a valid token.
1 parent 732dfa2 commit 150bf68

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

drivers/SmartThings/sonos/src/api/sonos_connection.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,11 @@ function SonosConnection.new(driver, device)
339339
device.log.warn(
340340
string.format("WebSocket connection no longer authorized, disconnecting")
341341
)
342-
local _, security_err = driver:request_oauth_token()
343-
if security_err then
344-
log.warn(string.format("Error during request for oauth token: %s", security_err))
342+
if not driver:is_waiting_for_oauth_token() then
343+
local _, security_err = driver:request_oauth_token()
344+
if security_err then
345+
log.warn(string.format("Error during request for oauth token: %s", security_err))
346+
end
345347
end
346348
-- closing the socket directly without calling `:stop()` triggers the reconnect loop,
347349
-- which is where we wait for the token to come in.

drivers/SmartThings/sonos/src/sonos_driver.lua

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,15 @@ function SonosDriver:notify_augmented_data_changed(update_kind, update_key, upda
184184
)
185185
end
186186

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
189190
and self.oauth.endpoint_app_info.state == "connected"
190191
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
192196
local _, err = self:request_oauth_token()
193197
if err then
194198
log.error(string.format("Request OAuth token error: %s", err))
@@ -322,6 +326,17 @@ function SonosDriver:check_auth(info_or_device)
322326
)
323327
end
324328

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+
325340
---@return any? ret nil on permissions violation
326341
---@return string? error nil on success
327342
function SonosDriver:request_oauth_token()
@@ -333,13 +348,17 @@ function SonosDriver:request_oauth_token()
333348
log.warn(string.format("get oauth token error: %s", maybe_err))
334349
end
335350
if type(maybe_token) == "table" and type(maybe_token.accessToken) == "string" then
351+
self.waiting_for_oauth_token = false
336352
self.oauth_token_bus:send(maybe_token)
353+
return true
337354
end
338355
local result, err = security.get_sonos_oauth()
339356
if not result then
340357
return nil, string.format("Error requesting OAuth token via Security API: %s", err)
341358
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()
343362
return result, err
344363
end
345364

0 commit comments

Comments
 (0)