Skip to content

Commit 4fdab16

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 4fdab16

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

drivers/SmartThings/sonos/src/sonos_driver.lua

Lines changed: 58 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,59 @@ 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+
-- notify with the updated record
688+
if self.notify_augmented_data_changed ~= nil then
689+
self:notify_augmented_data_changed("upsert", msg_val.payload.data_key, data)
690+
end
691+
elseif msg_val.kind == "Delete" then
692+
self.hub_augmented_driver_data[msg_val.payload.data_key] = nil
693+
-- notify with just the key that got deleted
694+
if self.notify_augmented_data_changed ~= nil then
695+
self:notify_augmented_data_changed("delete", msg_val.payload.data_key)
696+
end
697+
else
698+
log.warn(
699+
string.format(
700+
"Unexpected augmentDriverStore kind: %s",
701+
msg_val.kind
702+
)
703+
)
704+
end
705+
end
706+
end
707+
end
708+
654709
function SonosDriver.new_driver_template()
655710
local oauth_token_bus = cosock.bus()
656711
local oauth_info_bus = cosock.bus()
@@ -667,6 +722,8 @@ function SonosDriver.new_driver_template()
667722
bonded_devices = utils.new_mac_address_keyed_table(),
668723
dni_to_device_id = utils.new_mac_address_keyed_table(),
669724
lifecycle_handlers = SonosDriverLifecycleHandlers,
725+
-- Only overriden in the case of RPC version 100+ with API version <= 18
726+
environment_info_handler = SonosDriver.environment_info_handler,
670727
capability_handlers = {
671728
[capabilities.refresh.ID] = {
672729
[capabilities.refresh.commands.refresh.NAME] = do_refresh,

0 commit comments

Comments
 (0)