Skip to content

Commit dc1f604

Browse files
committed
sonos: Address a few nits from previous PR
This also has the additional goal of getting another Alpha build that will use the new packaging, which is needed to inject the new API key.
1 parent b583c61 commit dc1f604

File tree

6 files changed

+37
-66
lines changed

6 files changed

+37
-66
lines changed

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

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
local json = require "st.json"
12
local log = require "log"
2-
local net_url = require "net.url"
33
local st_utils = require "st.utils"
4-
local json = require "st.json"
54

65
local RestClient = require "lunchbox.rest"
76

@@ -13,11 +12,13 @@ local RestClient = require "lunchbox.rest"
1312
--- @return string|nil partial contents of partial read if successful
1413
local function process_rest_response(response, err, partial, err_callback)
1514
if err == nil and response == nil then
16-
log.error(st_utils.stringify_table({
17-
resp = response,
18-
maybe_err = err,
19-
maybe_partial = partial,
20-
}, "[SonosRestApi] Unexpected nil for both response and error processing REST reply", false))
15+
log.error(
16+
st_utils.stringify_table({
17+
resp = response,
18+
maybe_err = err,
19+
maybe_partial = partial,
20+
}, "[SonosRestApi] Unexpected nil for both response and error processing REST reply", false)
21+
)
2122
end
2223
if err ~= nil then
2324
if type(err_callback) == "function" then
@@ -71,70 +72,32 @@ end
7172
local SonosRestApi = {}
7273

7374
--- Query a Sonos Group IP address for individual player info
74-
---@param ip_or_url string|table
75-
---@param ... unknown
75+
---@param url table a URL table created by `net_url`
76+
---@param headers table<string,string>?
7677
---@return SonosDiscoveryInfo|SonosErrorResponse|nil
7778
---@return string|nil error
78-
---@overload fun(ip_or_url: table, headers: table<string,string>): SonosDiscoveryInfo?,string?
79-
---@overload fun(ip_or_url: string, port: number, headers: table<string,string>): SonosDiscoveryInfo?,string?
80-
function SonosRestApi.get_player_info(ip_or_url, ...)
81-
local url
82-
local headers
83-
if type(ip_or_url) == "table" then
84-
headers = select(1, ...)
85-
ip_or_url.path = "/api/v1/players/local/info"
86-
url = ip_or_url
87-
else
88-
local port = select(1, ...)
89-
headers = select(2, ...)
90-
url = net_url.parse(string.format("https://%s:%s/api/v1/players/local/info", ip_or_url, port))
91-
end
79+
function SonosRestApi.get_player_info(url, headers)
80+
url.path = "/api/v1/players/local/info"
9281
return process_rest_response(RestClient.one_shot_get(url, headers))
9382
end
9483

95-
---@param ip_or_url string|table
96-
---@param ... unknown
84+
---@param url table a URL table created by `net_url`
85+
---@param household HouseholdId
86+
---@param headers table<string,string>?
9787
---@return SonosGroupsResponseBody|SonosErrorResponse|nil response
9888
---@return nil|string error
99-
---@overload fun(ip_or_url: table, household: HouseholdId, headers: table<string,string>?): SonosGroupsResponseBody?,string?
100-
---@overload fun(ip_or_url: string, port: number, household: HouseholdId, headers: table<string,string>?): SonosGroupsResponseBody?,string?
101-
function SonosRestApi.get_groups_info(ip_or_url, ...)
102-
local url
103-
local headers
104-
if type(ip_or_url) == "table" then
105-
local household = select(1, ...)
106-
headers = select(2, ...)
107-
ip_or_url.path = string.format("/api/v1/households/%s/groups", household)
108-
url = ip_or_url
109-
else
110-
local port = select(1, ...)
111-
local household = select(2, ...)
112-
headers = select(3, ...)
113-
url = net_url.parse(string.format("https://%s:%s/api/v1/households/%s/groups", ip_or_url, port, household))
114-
end
89+
function SonosRestApi.get_groups_info(url, household, headers)
90+
url.path = string.format("/api/v1/households/%s/groups", household)
11591
return process_rest_response(RestClient.one_shot_get(url, headers))
11692
end
11793

118-
---@param ip_or_url string|table
119-
---@param ... unknown
94+
---@param url table a URL table created by `net_url`
95+
---@param household HouseholdId
96+
---@param headers table<string,string>?
12097
---@return SonosFavoritesResponseBody|SonosErrorResponse|nil response
12198
---@return nil|string error
122-
---@overload fun(ip_or_url: table, household: HouseholdId, headers: table<string,string>?): SonosFavoritesResponseBody?,string?
123-
---@overload fun(ip_or_url: string, port: number, household: HouseholdId, headers: table<string,string>?): SonosFavoritesResponseBody?,string?
124-
function SonosRestApi.get_favorites(ip_or_url, ...)
125-
local url
126-
local headers
127-
if type(ip_or_url) == "table" then
128-
local household = select(1, ...)
129-
headers = select(2, ...)
130-
ip_or_url.path = string.format("/api/v1/households/%s/favorites", household)
131-
url = ip_or_url
132-
else
133-
local port = select(1, ...)
134-
local household = select(2, ...)
135-
headers = select(3, ...)
136-
url = net_url.parse(string.format("https://%s:%s/api/v1/households/%s/favorites", ip_or_url, port, household))
137-
end
99+
function SonosRestApi.get_favorites(url, household, headers)
100+
url.path = string.format("/api/v1/households/%s/favorites", household)
138101
return process_rest_response(RestClient.one_shot_get(url, headers))
139102
end
140103

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,11 @@ function SonosConnection.new(driver, device)
410410
end
411411

412412
local url_ip = lb_utils.force_url_table(coordinator_player.websocketUrl).host
413-
413+
local base_url = lb_utils.force_url_table(
414+
string.format("https://%s:%s", url_ip, SonosApi.DEFAULT_SONOS_PORT)
415+
)
414416
local favorites_response, err, _ =
415-
SonosRestApi.get_favorites(url_ip, SonosApi.DEFAULT_SONOS_PORT, header.householdId)
417+
SonosRestApi.get_favorites(base_url, header.householdId)
416418

417419
if err or not favorites_response then
418420
log.error("Error querying for favorites: " .. err)

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local cosock = require "cosock"
22
local log = require "log"
3+
local net_url = require "net.url"
34
local ssdp = require "ssdp"
45
local st_utils = require "st.utils"
56

@@ -268,8 +269,12 @@ function sonos_ssdp.spawn_persistent_ssdp_task()
268269

269270
if is_new_information then
270271
local headers = SonosApi.make_headers()
271-
local discovery_info, err =
272-
SonosApi.RestApi.get_player_info(sonos_ssdp_info.ip, SonosApi.DEFAULT_SONOS_PORT, headers)
272+
local discovery_info, err = SonosApi.RestApi.get_player_info(
273+
net_url.parse(
274+
string.format("https://%s:%s", sonos_ssdp_info.ip, SonosApi.DEFAULT_SONOS_PORT)
275+
),
276+
headers
277+
)
273278
if not discovery_info then
274279
log.error(string.format("Error getting discovery info from SSDP response: %s", err))
275280
else

drivers/SmartThings/sonos/src/lifecycle_handlers.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local api_version = require("version").api
12
local capabilities = require "st.capabilities"
23

34
local cosock = require "cosock"
@@ -45,7 +46,7 @@ function SonosDriverLifecycleHandlers.initialize_device(driver, device)
4546
capabilities.mediaTrackControl.commands.previousTrack.NAME,
4647
}))
4748

48-
if not driver:has_received_startup_state() then
49+
if api_version >= 14 and not driver:has_received_startup_state() then
4950
device.log.debug("Driver startup state not yet received, delaying initialization of device.")
5051
driver:queue_device_init_for_startup_state(device)
5152
return

drivers/SmartThings/sonos/src/sonos_driver.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ function SonosDriver:get_oauth_token()
316316
if now < expiration then
317317
-- token is expiring soon, so we pre-emptively refresh
318318
if math.abs(expiration - now) < ONE_HOUR_IN_SECONDS then
319-
local result, err = self:request_oauth_token()
319+
local result, err = security.get_sonos_oauth()
320320
if not result then
321321
log.warn(string.format("Error requesting OAuth token via Security API: %s", err))
322322
end

drivers/SmartThings/sonos/src/ssdp.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ function _ssdp_mt:setwaker(kind, waker)
355355

356356
assert(
357357
self.waker_ref == nil or waker == nil,
358-
"Waker already set, cannot await SSDP serach instance from multiple locations."
358+
"Waker already set, cannot await SSDP search instance from multiple locations."
359359
)
360360

361361
self.waker_ref = waker

0 commit comments

Comments
 (0)