Skip to content

Commit bd1cc72

Browse files
authored
Merge pull request #1671 from SmartThingsCommunity/fix/hue-datastore-size-reduction
Remove state cache usage from devices to reduce datastore size
2 parents 8569fcc + 413f395 commit bd1cc72

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

drivers/SmartThings/philips-hue/src/fields.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ local Fields = {
3030
PARENT_DEVICE_ID = "parent_device_id_local",
3131
RESOURCE_ID = "rid",
3232
RETRY_MIGRATION = "retry_migration",
33-
WRAPPED_HUE = "_wrapped_hue"
33+
WRAPPED_HUE = "_wrapped_hue",
34+
COLOR_SATURATION = "color_saturation",
35+
COLOR_HUE = "color_hue",
36+
SWITCH_STATE = "switch_state_cache",
3437
}
3538

3639
return Fields

drivers/SmartThings/philips-hue/src/handlers/attribute_emitters.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ local function _emit_light_events_inner(light_device, light_repr)
3535

3636
if light_repr.on and light_repr.on.on then
3737
light_device:emit_event(capabilities.switch.switch.on())
38+
light_device:set_field(Fields.SWITCH_STATE, "on", {persist = true})
3839
elseif light_repr.on and not light_repr.on.on then
3940
light_device:emit_event(capabilities.switch.switch.off())
41+
light_device:set_field(Fields.SWITCH_STATE, "off", {persist = true})
4042
end
4143

4244
if light_repr.dimming then
@@ -96,6 +98,7 @@ local function _emit_light_events_inner(light_device, light_repr)
9698
)
9799
else
98100
light_device:emit_event(capabilities.colorControl.hue(adjusted_hue))
101+
light_device:set_field(Fields.COLOR_HUE, adjusted_hue, {persist = true})
99102
end
100103

101104
if utils.is_nan(adjusted_sat) then
@@ -107,6 +110,7 @@ local function _emit_light_events_inner(light_device, light_repr)
107110
)
108111
else
109112
light_device:emit_event(capabilities.colorControl.saturation(adjusted_sat))
113+
light_device:set_field(Fields.COLOR_SATURATION, adjusted_sat, {persist = true})
110114
end
111115
end
112116
end

drivers/SmartThings/philips-hue/src/handlers/commands.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local capabilities = require "st.capabilities"
21
local log = require "log"
32
local st_utils = require "st.utils"
43

@@ -92,8 +91,7 @@ local function do_switch_level_action(driver, device, args)
9291
return
9392
end
9493

95-
local is_off = device:get_latest_state(
96-
"main", capabilities.switch.ID, capabilities.switch.switch.NAME) == "off"
94+
local is_off = device:get_field(Fields.SWITCH_STATE) == "off"
9795

9896
if is_off then
9997
local resp, err = hue_api:set_light_on_state(light_id, true)
@@ -178,7 +176,7 @@ end
178176
local function do_setHue_action(driver, device, args)
179177

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

195193
-- Use existing 'hue' value for device or set to 0 and pass arg values to function 'do_color_action'
196-
local currentHue = device:get_latest_state("main", capabilities.colorControl.ID, capabilities.colorControl.hue.NAME, 0)
194+
local currentHue = device:get_field(Fields.COLOR_HUE) or 0
197195
args.args.color = {
198196
hue = currentHue,
199197
saturation = args.args.saturation

drivers/SmartThings/philips-hue/src/handlers/lifecycle_handlers/init.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local log = require "log"
22
local st_utils = require "st.utils"
3+
local capabilities = require "st.capabilities"
34

45
local Discovery = require "disco"
56
local Fields = require "fields"
@@ -43,6 +44,19 @@ local migration_handlers = utils.lazy_handler_loader("handlers.migration_handler
4344
---@class LifecycleHandlers
4445
local LifecycleHandlers = {}
4546

47+
local function emit_component_event_no_cache(device, component, capability_event)
48+
if not device:supports_capability(capability_event.capability, component.id) then
49+
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)
50+
log.warn_with({ hub_logs = true }, err_msg)
51+
return false, err_msg
52+
end
53+
local event, err = capabilities.emit_event(device, component.id, device.capability_channel, capability_event)
54+
if err ~= nil then
55+
log.warn_with({ hub_logs = true }, err)
56+
end
57+
return event, err
58+
end
59+
4660
---@param driver HueDriver
4761
---@param device HueDevice
4862
---@param ... any arguments for device specific handler
@@ -55,6 +69,10 @@ function LifecycleHandlers.device_init(driver, device, ...)
5569
)
5670
)
5771
inner_handlers[device_type].init(driver, device, ...)
72+
73+
-- Remove usage of the state cache for hue devices to avoid large datastores
74+
device:set_field("__state_cache", nil, {persist = true})
75+
device:extend_device("emit_component_event", emit_component_event_no_cache)
5876
end
5977

6078
---@param driver HueDriver

0 commit comments

Comments
 (0)