-
Notifications
You must be signed in to change notification settings - Fork 544
Matter Ikea Scroll: Aggregate multiple responses into one event, add global bounds checking #3014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hcarter-775
wants to merge
2
commits into
main
Choose a base branch
from
add/multi-response-block-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−26
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
drivers/SmartThings/matter-switch/src/sub_drivers/ikea_scroll/scroll_utils/event_utils.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| -- Copyright © 2026 SmartThings, Inc. | ||
| -- Licensed under the Apache License, Version 2.0 | ||
|
|
||
| local st_utils = require "st.utils" | ||
| local clusters = require "st.matter.clusters" | ||
| local scroll_fields = require "sub_drivers.ikea_scroll.scroll_utils.fields" | ||
|
|
||
| local IkeaScrollEventUtils = {} | ||
|
|
||
|
|
||
| function IkeaScrollEventUtils.requeue_clear_scroll_state(device) | ||
| -- Stores a timer object, which is required to cancel a timer early | ||
| local CLEAR_STATE_TIMER = "__clear_state_timer" | ||
| -- cancel any previously queued clear state actions to prevent unintended clears | ||
| if device:get_field(CLEAR_STATE_TIMER) then | ||
| device.thread:cancel_timer(device:get_field(CLEAR_STATE_TIMER)) | ||
| end | ||
| local delay_s = 8 | ||
| local new_timer = device.thread:call_with_delay(delay_s, function() | ||
| device:set_field(scroll_fields.GLOBAL_ROTATE_AMOUNT_STATE, 0) | ||
| end) | ||
| device:set_field(CLEAR_STATE_TIMER, new_timer) | ||
| end | ||
|
|
||
| function IkeaScrollEventUtils.is_valid_scroll_amount(device, scroll_amount) | ||
| local global_rotate_amount_state = device:get_field(scroll_fields.GLOBAL_ROTATE_AMOUNT_STATE) or 0 | ||
| local is_rotate_amount_state_at_bounds = (scroll_amount < 0 and global_rotate_amount_state <= -100) or (scroll_amount > 0 and global_rotate_amount_state >= 100) | ||
| if is_rotate_amount_state_at_bounds then | ||
| return false | ||
| end | ||
|
|
||
| device:set_field(scroll_fields.GLOBAL_ROTATE_AMOUNT_STATE, st_utils.clamp_value(global_rotate_amount_state + scroll_amount, -100, 100)) | ||
| IkeaScrollEventUtils.requeue_clear_scroll_state(device) | ||
| return true | ||
| end | ||
|
|
||
| -- inspect all info blocks to find the last one that is not an InitialPress event. We will | ||
| -- only try to emit a rotateAmount event if the current info block being handled is that last one. | ||
| function IkeaScrollEventUtils.is_last_valid_info_block(cur_info_block_event_id, cur_info_block_value, info_blocks) | ||
| local last_valid_emission_idx = #info_blocks | ||
| -- Ignore all InitialPress events in a multi-response block | ||
| while (last_valid_emission_idx > 1) and (info_blocks[last_valid_emission_idx].info_block.event_id == clusters.Switch.events.InitialPress) do | ||
| last_valid_emission_idx = last_valid_emission_idx - 1 | ||
| end | ||
|
|
||
| -- Because the info block does not include the unique_key Matter defined event number, this | ||
| -- logic is a best guess at matching the current info block to the last valid info block. | ||
| local emission_ib = info_blocks[last_valid_emission_idx].info_block | ||
| if emission_ib.event_id ~= cur_info_block_event_id then | ||
| return false | ||
| elseif emission_ib.event_id == clusters.Switch.events.MultiPressComplete.ID then | ||
| local last_valid_ib_value = emission_ib.data.elements and emission_ib.data.elements.total_number_of_presses_counted.value or 0 | ||
| return last_valid_ib_value == cur_info_block_value | ||
| elseif emission_ib.event_id == clusters.Switch.events.MultiPressOngoing.ID then | ||
| local last_valid_ib_value = emission_ib.data.elements and emission_ib.data.elements.current_number_of_presses_counted.value or 0 | ||
| return last_valid_ib_value == cur_info_block_value | ||
| elseif last_valid_emission_idx == 1 then -- aka, all ib's are InitialPress | ||
| return true | ||
| end | ||
|
|
||
| return false | ||
| end | ||
|
|
||
| function IkeaScrollEventUtils.aggregate_scroll_amount_for_info_blocks(device, info_blocks) | ||
| local total_presses = 0 | ||
| local presses_in_current_chain = 0 | ||
| for _, ib in ipairs(info_blocks) do | ||
| if ib.info_block.event_id == clusters.Switch.events.MultiPressOngoing.ID then | ||
| presses_in_current_chain = presses_in_current_chain + (ib.info_block.data.elements and ib.info_block.data.elements.current_number_of_presses_counted.value or 0) | ||
| elseif ib.info_block.event_id == clusters.Switch.events.MultiPressComplete.ID then | ||
| total_presses = total_presses + (ib.info_block.data.elements and ib.info_block.data.elements.total_number_of_presses_counted.value or 0) | ||
| presses_in_current_chain = 0 | ||
| end | ||
| end | ||
| total_presses = total_presses + presses_in_current_chain -- aggregate any presses to the total from the current chain | ||
| return { total_presses = total_presses, presses_in_current_chain = presses_in_current_chain } | ||
| end | ||
|
|
||
| return IkeaScrollEventUtils | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest putting this definition and the timeout (
delay_s = 8) into fields along with the other fields