@@ -104,9 +104,9 @@ local function update_supported_arguments(device, current_run_mode, operating_st
104
104
-- Get the tag of the current run mode
105
105
local current_tag = 0xFFFF
106
106
local supported_run_modes = device :get_field (RUN_MODE_SUPPORTED_MODES ) or {}
107
- for i , mode in ipairs (supported_run_modes ) do
108
- if mode [ 1 ] == current_run_mode then
109
- current_tag = mode [ 2 ]
107
+ for _ , mode in ipairs (supported_run_modes ) do
108
+ if mode . label == current_run_mode then
109
+ current_tag = mode . tag
110
110
break
111
111
end
112
112
end
@@ -126,9 +126,9 @@ local function update_supported_arguments(device, current_run_mode, operating_st
126
126
127
127
-- Set supported run arguments
128
128
local supported_arguments = {} -- For generic plugin
129
- for i , mode in ipairs (supported_run_modes ) do
130
- if mode [ 2 ] == clusters .RvcRunMode .types .ModeTag .IDLE or can_be_non_idle == true then
131
- table.insert (supported_arguments , mode [ 1 ] )
129
+ for _ , mode in ipairs (supported_run_modes ) do
130
+ if mode . tag == clusters .RvcRunMode .types .ModeTag .IDLE or can_be_non_idle == true then
131
+ table.insert (supported_arguments , mode . label )
132
132
end
133
133
end
134
134
@@ -140,8 +140,8 @@ local function update_supported_arguments(device, current_run_mode, operating_st
140
140
-- Set supported clean arguments
141
141
local supported_clean_modes = device :get_field (CLEAN_MODE_SUPPORTED_MODES ) or {}
142
142
supported_arguments = {}
143
- for i , mode in ipairs (supported_clean_modes ) do
144
- table.insert (supported_arguments , mode )
143
+ for _ , mode in ipairs (supported_clean_modes ) do
144
+ table.insert (supported_arguments , mode . label )
145
145
end
146
146
147
147
-- Send event to set supported clean modes
@@ -155,13 +155,13 @@ end
155
155
-- Matter Handlers --
156
156
local function run_mode_supported_mode_handler (driver , device , ib , response )
157
157
local supported_modes = {}
158
- local supported_modes_with_tag = {}
159
- for i , mode in ipairs (ib .data .elements ) do
158
+ local supported_modes_id_tag = {}
159
+ for _ , mode in ipairs (ib .data .elements ) do
160
160
if version .api < 10 then
161
161
clusters .RvcRunMode .types .ModeOptionStruct :augment_type (mode )
162
162
end
163
163
local tag = 0xFFFF
164
- for i , t in ipairs (mode .elements .mode_tags .elements ) do
164
+ for _ , t in ipairs (mode .elements .mode_tags .elements ) do
165
165
if t .elements .value .value == clusters .RvcRunMode .types .ModeTag .IDLE then
166
166
tag = clusters .RvcRunMode .types .ModeTag .IDLE
167
167
break
@@ -175,10 +175,10 @@ local function run_mode_supported_mode_handler(driver, device, ib, response)
175
175
end
176
176
if tag ~= 0xFFFF then
177
177
table.insert (supported_modes , mode .elements .label .value )
178
- table.insert (supported_modes_with_tag , {mode .elements .label .value , tag })
178
+ table.insert (supported_modes_id_tag , { label = mode .elements .label .value , id = mode . elements . mode . value , tag = tag })
179
179
end
180
180
end
181
- device :set_field (RUN_MODE_SUPPORTED_MODES , supported_modes_with_tag , { persist = true })
181
+ device :set_field (RUN_MODE_SUPPORTED_MODES , supported_modes_id_tag , { persist = true })
182
182
183
183
-- Update Supported Modes
184
184
local component = device .profile .components [" runMode" ]
@@ -203,12 +203,12 @@ local function run_mode_current_mode_handler(driver, device, ib, response)
203
203
device .log .info (string.format (" run_mode_current_mode_handler currentMode: %s" , ib .data .value ))
204
204
205
205
-- Get label of current mode
206
- local mode_index = ib .data .value
206
+ local mode_id = ib .data .value
207
207
local supported_run_mode = device :get_field (RUN_MODE_SUPPORTED_MODES ) or {}
208
208
local current_run_mode = nil
209
- for i , mode in ipairs (supported_run_mode ) do
210
- if i - 1 == mode_index then
211
- current_run_mode = mode [ 1 ]
209
+ for _ , mode in ipairs (supported_run_mode ) do
210
+ if mode . id == mode_id then
211
+ current_run_mode = mode . label
212
212
end
213
213
end
214
214
if current_run_mode == nil then
@@ -231,13 +231,15 @@ end
231
231
local function clean_mode_supported_mode_handler (driver , device , ib , response )
232
232
device .log .info (string.format (" clean_mode_supported_mode_handler" ))
233
233
local supported_modes = {}
234
- for i , mode in ipairs (ib .data .elements ) do
234
+ local supported_modes_id = {}
235
+ for _ , mode in ipairs (ib .data .elements ) do
235
236
if version .api < 10 then
236
237
clusters .RvcRunMode .types .ModeOptionStruct :augment_type (mode )
237
238
end
238
239
table.insert (supported_modes , mode .elements .label .value )
240
+ table.insert (supported_modes_id , { label = mode .elements .label .value , id = mode .elements .mode .value })
239
241
end
240
- device :set_field (CLEAN_MODE_SUPPORTED_MODES , supported_modes , { persist = true })
242
+ device :set_field (CLEAN_MODE_SUPPORTED_MODES , supported_modes_id , { persist = true })
241
243
242
244
local component = device .profile .components [" cleanMode" ]
243
245
local event = capabilities .mode .supportedModes (supported_modes , {visibility = {displayed = false }})
@@ -248,12 +250,12 @@ end
248
250
249
251
local function clean_mode_current_mode_handler (driver , device , ib , response )
250
252
device .log .info (string.format (" clean_mode_current_mode_handler currentMode: %s" , ib .data .value ))
251
- local mode_index = ib .data .value
253
+ local mode_id = ib .data .value
252
254
local supported_clean_mode = device :get_field (CLEAN_MODE_SUPPORTED_MODES ) or {}
253
- for i , mode in ipairs (supported_clean_mode ) do
254
- if i - 1 == mode_index then
255
+ for _ , mode in ipairs (supported_clean_mode ) do
256
+ if mode . id == mode_id then
255
257
local component = device .profile .components [" cleanMode" ]
256
- device :emit_component_event (component , capabilities .mode .mode (mode ))
258
+ device :emit_component_event (component , capabilities .mode .mode (mode . label ))
257
259
break
258
260
end
259
261
end
@@ -329,17 +331,19 @@ local function handle_robot_cleaner_mode(driver, device, cmd)
329
331
local endpoint_id = device :component_to_endpoint (cmd .component )
330
332
if cmd .component == " runMode" then
331
333
local supported_modes = device :get_field (RUN_MODE_SUPPORTED_MODES ) or {}
332
- for i , mode in ipairs (supported_modes ) do
333
- if cmd .args .mode == mode [1 ] then
334
- device :send (clusters .RvcRunMode .commands .ChangeToMode (device , endpoint_id , i - 1 ))
334
+ for _ , mode in ipairs (supported_modes ) do
335
+ if cmd .args .mode == mode .label then
336
+ device .log .info (string.format (" mode.label: %s, mode.id: %s" , mode .label , mode .id ))
337
+ device :send (clusters .RvcRunMode .commands .ChangeToMode (device , endpoint_id , mode .id ))
335
338
return
336
339
end
337
340
end
338
341
elseif cmd .component == " cleanMode" then
339
342
local supported_modes = device :get_field (CLEAN_MODE_SUPPORTED_MODES ) or {}
340
- for i , mode in ipairs (supported_modes ) do
341
- if cmd .args .mode == mode then
342
- device :send (clusters .RvcCleanMode .commands .ChangeToMode (device , endpoint_id , i - 1 ))
343
+ for _ , mode in ipairs (supported_modes ) do
344
+ if cmd .args .mode == mode .label then
345
+ device .log .info (string.format (" mode.label: %s, mode.id: %s" , mode .label , mode .id ))
346
+ device :send (clusters .RvcCleanMode .commands .ChangeToMode (device , endpoint_id , mode .id ))
343
347
return
344
348
end
345
349
end
0 commit comments