Skip to content

Commit

Permalink
Merge pull request SmartThingsCommunity#1671 from SmartThingsCommunit…
Browse files Browse the repository at this point in the history
…y/fix/hue-datastore-size-reduction

Remove state cache usage from devices to reduce datastore size
  • Loading branch information
cjswedes authored Oct 8, 2024
2 parents 8569fcc + 413f395 commit bd1cc72
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
5 changes: 4 additions & 1 deletion drivers/SmartThings/philips-hue/src/fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ local Fields = {
PARENT_DEVICE_ID = "parent_device_id_local",
RESOURCE_ID = "rid",
RETRY_MIGRATION = "retry_migration",
WRAPPED_HUE = "_wrapped_hue"
WRAPPED_HUE = "_wrapped_hue",
COLOR_SATURATION = "color_saturation",
COLOR_HUE = "color_hue",
SWITCH_STATE = "switch_state_cache",
}

return Fields
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ local function _emit_light_events_inner(light_device, light_repr)

if light_repr.on and light_repr.on.on then
light_device:emit_event(capabilities.switch.switch.on())
light_device:set_field(Fields.SWITCH_STATE, "on", {persist = true})
elseif light_repr.on and not light_repr.on.on then
light_device:emit_event(capabilities.switch.switch.off())
light_device:set_field(Fields.SWITCH_STATE, "off", {persist = true})
end

if light_repr.dimming then
Expand Down Expand Up @@ -96,6 +98,7 @@ local function _emit_light_events_inner(light_device, light_repr)
)
else
light_device:emit_event(capabilities.colorControl.hue(adjusted_hue))
light_device:set_field(Fields.COLOR_HUE, adjusted_hue, {persist = true})
end

if utils.is_nan(adjusted_sat) then
Expand All @@ -107,6 +110,7 @@ local function _emit_light_events_inner(light_device, light_repr)
)
else
light_device:emit_event(capabilities.colorControl.saturation(adjusted_sat))
light_device:set_field(Fields.COLOR_SATURATION, adjusted_sat, {persist = true})
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions drivers/SmartThings/philips-hue/src/handlers/commands.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local capabilities = require "st.capabilities"
local log = require "log"
local st_utils = require "st.utils"

Expand Down Expand Up @@ -92,8 +91,7 @@ local function do_switch_level_action(driver, device, args)
return
end

local is_off = device:get_latest_state(
"main", capabilities.switch.ID, capabilities.switch.switch.NAME) == "off"
local is_off = device:get_field(Fields.SWITCH_STATE) == "off"

if is_off then
local resp, err = hue_api:set_light_on_state(light_id, true)
Expand Down Expand Up @@ -178,7 +176,7 @@ end
local function do_setHue_action(driver, device, args)

-- Use existing 'saturation' value for device or set to 0 and pass arg values to function 'do_color_action'
local currentSaturation = device:get_latest_state("main", capabilities.colorControl.ID, capabilities.colorControl.saturation.NAME, 0)
local currentSaturation = device:get_field(Fields.COLOR_SATURATION) or 0
args.args.color = {
hue = args.args.hue,
saturation = currentSaturation
Expand All @@ -193,7 +191,7 @@ end
local function do_setSaturation_action(driver, device, args)

-- Use existing 'hue' value for device or set to 0 and pass arg values to function 'do_color_action'
local currentHue = device:get_latest_state("main", capabilities.colorControl.ID, capabilities.colorControl.hue.NAME, 0)
local currentHue = device:get_field(Fields.COLOR_HUE) or 0
args.args.color = {
hue = currentHue,
saturation = args.args.saturation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local log = require "log"
local st_utils = require "st.utils"
local capabilities = require "st.capabilities"

local Discovery = require "disco"
local Fields = require "fields"
Expand Down Expand Up @@ -43,6 +44,19 @@ local migration_handlers = utils.lazy_handler_loader("handlers.migration_handler
---@class LifecycleHandlers
local LifecycleHandlers = {}

local function emit_component_event_no_cache(device, component, capability_event)
if not device:supports_capability(capability_event.capability, component.id) then
local err_msg = string.format("Attempted to generate event for %s.%s but it does not support capability %s", device.id, component.id, capability_event.capability.NAME)
log.warn_with({ hub_logs = true }, err_msg)
return false, err_msg
end
local event, err = capabilities.emit_event(device, component.id, device.capability_channel, capability_event)
if err ~= nil then
log.warn_with({ hub_logs = true }, err)
end
return event, err
end

---@param driver HueDriver
---@param device HueDevice
---@param ... any arguments for device specific handler
Expand All @@ -55,6 +69,10 @@ function LifecycleHandlers.device_init(driver, device, ...)
)
)
inner_handlers[device_type].init(driver, device, ...)

-- Remove usage of the state cache for hue devices to avoid large datastores
device:set_field("__state_cache", nil, {persist = true})
device:extend_device("emit_component_event", emit_component_event_no_cache)
end

---@param driver HueDriver
Expand Down

0 comments on commit bd1cc72

Please sign in to comment.