Skip to content

Commit 00b35ca

Browse files
committed
CHAD-17218: Fix Sonos augmented driver store upserts
This was broken in hubcore 59 due to IPC rework.
1 parent e57dc95 commit 00b35ca

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

drivers/SmartThings/sonos/src/sonos_driver.lua

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
local api_version = require "version".api
1+
local version = require "version"
2+
local api_version = version.api
3+
local rpc_version = version.rpc
24
local capabilities = require "st.capabilities"
35
local cosock = require "cosock"
46
local json = require "st.json"
@@ -651,6 +653,60 @@ local function do_refresh(driver, device, cmd)
651653
sonos_conn:refresh_subscriptions()
652654
end
653655

656+
--- In RPC version 100, the events for augmented driver store were changed
657+
--- to no longer match the parsing done in API version 18 and below.
658+
---
659+
--- API version 19 will handle both before and after RPC 100 changes so this only needs
660+
--- to be applied for RPC version >= 100 and API version <= 18.
661+
if rpc_version >= 100 and api_version <= 18 then
662+
log.info("Overriding environment info handler for RPC >= 100 and API <= 18")
663+
function SonosDriver:environment_info_handler(channel)
664+
log.info("Starting environment info handler for RPC >= 100 and API <= 18")
665+
local msg_type, msg_val = channel:receive()
666+
-- This driver only cares about augmentDriverStore messages currently.
667+
-- Previously, this was augmentDatastore msg_type.
668+
if msg_type == "augmentDriverStore" then
669+
if type(msg_val.payload) ~= "table" then
670+
log.warn(
671+
string.format(
672+
"Unexpected augmentDriverStore payload type: %s",
673+
type(msg_val.payload)
674+
)
675+
)
676+
return
677+
end
678+
-- The field evt_kind was renamed to kind and is a string of the enum rather than
679+
-- the integer value of the enum.
680+
if msg_val.kind == "Upsert" then
681+
self.hub_augmented_driver_data[msg_val.payload.data_key] = msg_val.payload.data_value
682+
-- This type was changed to table of u8s instead of a string
683+
local data = ""
684+
for _, v in pairs(msg_val.payload.data_value) do
685+
data = data .. string.char(v)
686+
end
687+
log.info(data)
688+
-- notify with the updated record
689+
if self.notify_augmented_data_changed ~= nil then
690+
self:notify_augmented_data_changed("upsert", msg_val.payload.data_key, data)
691+
end
692+
elseif msg_val.kind == "Delete" then
693+
self.hub_augmented_driver_data[msg_val.payload.data_key] = nil
694+
-- notify with just the key that got deleted
695+
if self.notify_augmented_data_changed ~= nil then
696+
self:notify_augmented_data_changed("delete", msg_val.payload.data_key)
697+
end
698+
else
699+
log.warn(
700+
string.format(
701+
"Unexpected augmentDriverStore kind: %s",
702+
msg_val.kind
703+
)
704+
)
705+
end
706+
end
707+
end
708+
end
709+
654710
function SonosDriver.new_driver_template()
655711
local oauth_token_bus = cosock.bus()
656712
local oauth_info_bus = cosock.bus()
@@ -667,6 +723,8 @@ function SonosDriver.new_driver_template()
667723
bonded_devices = utils.new_mac_address_keyed_table(),
668724
dni_to_device_id = utils.new_mac_address_keyed_table(),
669725
lifecycle_handlers = SonosDriverLifecycleHandlers,
726+
-- Only overriden in the case of RPC version 100+ with API version <= 18
727+
environment_info_handler = SonosDriver.environment_info_handler,
670728
capability_handlers = {
671729
[capabilities.refresh.ID] = {
672730
[capabilities.refresh.commands.refresh.NAME] = do_refresh,

0 commit comments

Comments
 (0)