Skip to content

Commit a7a2980

Browse files
authored
Merge pull request #2392 from SmartThingsCommunity/fix/sonos-link-notification
sonos: Fix oauth account linking notifications
2 parents 6844613 + d1c82fb commit a7a2980

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ local function _oauth_reconnect_task(sonos_conn)
258258
local unauthorized = (check_auth == false)
259259

260260
if unauthorized then
261+
sonos_conn.driver:alert_unauthorized()
261262
local token, channel_error = token_receive_handle:receive()
262263
if not token then
263264
log.warn(string.format("Error requesting token: %s", channel_error))
@@ -347,6 +348,7 @@ function SonosConnection.new(driver, device)
347348
else
348349
Router.close_socket_for_player(unique_key)
349350
end
351+
self.driver:alert_unauthorized()
350352
end
351353
end
352354
elseif header.type == "groups" then

drivers/SmartThings/sonos/src/lifecycle_handlers.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function SonosDriverLifecycleHandlers.initialize_device(driver, device)
8282
local token, token_recv_err
8383
-- max 30 mins
8484
local backoff_builder = utils.backoff_builder(60 * 30, 30, 2)
85+
driver:alert_unauthorized()
8586

8687
local backoff_timer = nil
8788
while not token do

drivers/SmartThings/sonos/src/sonos_driver.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ end
4040
---@field private oauth { token: {accessToken: string, expiresAt: number}, endpoint_app_info: { state: "connected"|"disconnected" }, force_oauth: boolean? } cached OAuth info
4141
---@field private startup_state_received boolean
4242
---@field private devices_waiting_for_startup_state SonosDevice[]
43+
---@field private have_alerted_unauthorized boolean Used to track if we have requested an oauth token, this will trigger the notification used for account linking
4344
---@field package bonded_devices table<string, boolean> map of Device device_network_id to a boolean indicating if the device is currently known as a bonded device.
4445
---
4546
---@field public ssdp_task SonosPersistentSsdpTask?
@@ -388,6 +389,25 @@ function SonosDriver:oauth_app_connected()
388389
and self.oauth.endpoint_app_info.state == "connected"
389390
end
390391

392+
--- Used to trigger the notification that the user must link their sonos account.
393+
--- Will request a token a single time which will trigger preinstall isa flow.
394+
function SonosDriver:alert_unauthorized()
395+
if api_version < 14 or security == nil then
396+
return
397+
end
398+
if self.have_alerted_unauthorized then
399+
return
400+
end
401+
-- Do the request regardless if we think oauth is connected, because
402+
-- there is a possibility that we have stale data.
403+
local result, err = security.get_sonos_oauth()
404+
if not result then
405+
log.warn(string.format("Failed to alert unauthorized: %s", err))
406+
return
407+
end
408+
self.have_alerted_unauthorized = true
409+
end
410+
391411
---Create a cosock task that handles events from the persistent SSDP task.
392412
---@param driver SonosDriver
393413
---@param discovery_event_subscription cosock.Bus.Subscription
@@ -644,6 +664,7 @@ function SonosDriver.new_driver_template()
644664
oauth_token_bus = oauth_token_bus,
645665
oauth_info_bus = oauth_info_bus,
646666
oauth = {},
667+
have_alerted_unauthorized = false,
647668
startup_state_received = false,
648669
devices_waiting_for_startup_state = {},
649670
bonded_devices = utils.new_mac_address_keyed_table(),

0 commit comments

Comments
 (0)