11-- Copyright 2022 SmartThings, Inc.
22-- Licensed under the Apache License, Version 2.0
33
4-
54-- Note: Currently only support for window shades with the PositionallyAware Feature
65-- Note: No support for setting device into calibration mode, it must be done manually
76local capabilities = require " st.capabilities"
87local im = require " st.matter.interaction_model"
98local clusters = require " st.matter.clusters"
9+ local device_lib = require " st.device"
1010local MatterDriver = require " st.matter.driver"
1111local utils = require " st.utils"
1212
13+ local IS_PARENT_CHILD_DEVICE = " __is_parent_child_device"
14+ --- If the ASSIGNED_CHILD_KEY field is populated for an endpoint, it should be
15+ --- used as the key in the get_child_by_parent_assigned_key() function. This allows
16+ --- multiple endpoints to associate with the same child device, though right now child
17+ local ASSIGNED_CHILD_KEY = " __assigned_child_key"
18+
1319local battery_support = {
1420 NO_BATTERY = " NO_BATTERY" ,
1521 BATTERY_LEVEL = " BATTERY_LEVEL" ,
@@ -36,6 +42,16 @@ local TARGET_TILT_PERCENT = {
3642
3743local TARGET_REACH_TOLERANCE = 0.5
3844
45+ local function find_child (parent_device , ep_id )
46+ local assigned_key = parent_device :get_field (string.format (" %s_%d" , ASSIGNED_CHILD_KEY , ep_id )) or ep_id
47+ return parent_device :get_child_by_parent_assigned_key (string.format (" %d" , assigned_key ))
48+ end
49+
50+ --- Resolves the device that owns the given endpoint: the child device if one exists, otherwise the device itself.
51+ local function resolve_device_for_endpoint (device , ep_id )
52+ return find_child (device , ep_id ) or device
53+ end
54+
3955--- Gets the current window shade status based on the lift and tilt positions.
4056--- @param device any a Matter device object
4157--- @return any status the current window shade status
@@ -173,11 +189,37 @@ local function match_profile(device, battery_supported)
173189 device :try_update_metadata ({profile = profile_name })
174190end
175191
192+ local function create_or_update_multi_devices (driver , device , wc_cluster_eps )
193+ local default_endpoint_id = find_default_endpoint (device , clusters .WindowCovering .ID )
194+ table.sort (wc_cluster_eps )
195+ for device_num , ep_id in ipairs (wc_cluster_eps ) do
196+ if ep_id ~= default_endpoint_id then -- don't create a child device that maps to the main endpoint
197+ local label_and_name = string.format (" %s %d" , device .label or " Window Covering" , device_num )
198+ local existing_child_device = device :get_field (IS_PARENT_CHILD_DEVICE ) and find_child (device , ep_id )
199+ if not existing_child_device then
200+ driver :try_create_device ({
201+ type = " EDGE_CHILD" ,
202+ label = label_and_name ,
203+ profile = " window-covering" ,
204+ parent_device_id = device .id ,
205+ parent_assigned_child_key = string.format (" %d" , ep_id ),
206+ vendor_provided_label = label_and_name
207+ })
208+ end
209+ end
210+ end
211+ -- Persist so that the find_child function is always set on each driver init.
212+ device :set_field (IS_PARENT_CHILD_DEVICE , true , {persist = true })
213+ device :set_find_child (find_child )
214+ end
215+
176216local function device_init (driver , device )
217+ if device :get_field (IS_PARENT_CHILD_DEVICE ) then
218+ device :set_find_child (find_child )
219+ end
177220 -- set unused fields to nil. These field settings can be removed after a single driver update.
178221 device :set_field (" __preset_level_key" , nil )
179222 device :set_field (" __reverse_polarity" , nil )
180-
181223 device :set_component_to_endpoint_fn (component_to_endpoint )
182224 if device :supports_capability_by_id (capabilities .windowShadePreset .ID ) and
183225 device :get_latest_state (" main" , capabilities .windowShadePreset .ID , capabilities .windowShadePreset .position .NAME ) == nil then
@@ -187,10 +229,16 @@ local function device_init(driver, device)
187229 local preset_position = device .preferences .presetPosition or DEFAULT_PRESET_LEVEL
188230 device :emit_event (capabilities .windowShadePreset .position (preset_position , {visibility = {displayed = false }}))
189231 end
190- device :subscribe ()
232+ if device .network_type ~= device_lib .NETWORK_TYPE_CHILD then
233+ device :subscribe ()
234+ end
191235end
192236
193237local function do_configure (driver , device )
238+ local wc_cluster_eps = device :get_endpoints (clusters .WindowCovering .ID )
239+ if # wc_cluster_eps > 1 then
240+ create_or_update_multi_devices (driver , device , wc_cluster_eps )
241+ end
194242 local battery_feature_eps = device :get_endpoints (clusters .PowerSource .ID , {feature_bitmap = clusters .PowerSource .types .PowerSourceFeature .BATTERY })
195243 if # battery_feature_eps > 0 then
196244 local attribute_list_read = im .InteractionRequest (im .InteractionRequest .RequestType .READ , {})
204252local function info_changed (driver , device , event , args )
205253 if device .profile .id ~= args .old_st_store .profile .id then
206254 device :subscribe ()
207- elseif device .matter_version .software ~= args .old_st_store .matter_version .software then
255+ elseif args . old_st_store . matter_version and device .matter_version .software ~= args .old_st_store .matter_version .software then
208256 local battery_feature_eps = device :get_endpoints (clusters .PowerSource .ID , {feature_bitmap = clusters .PowerSource .types .PowerSourceFeature .BATTERY })
209257 if # battery_feature_eps > 0 then
210258 local attribute_list_read = im .InteractionRequest (im .InteractionRequest .RequestType .READ , {})
@@ -239,8 +287,7 @@ local function handle_preset(driver, device, cmd)
239287end
240288
241289local function handle_set_preset (driver , device , cmd )
242- local endpoint_id = device :component_to_endpoint (cmd .component )
243- device :emit_event_for_endpoint (endpoint_id , capabilities .windowShadePreset .position (cmd .args .position ))
290+ device :emit_event (capabilities .windowShadePreset .position (cmd .args .position ))
244291end
245292
246293local function handle_close (driver , device , cmd )
@@ -317,28 +364,29 @@ end
317364
318365-- attribute handlers
319366local function current_position_lift_percent_100ths_handler (driver , device , ib , response )
367+ device = resolve_device_for_endpoint (device , ib .endpoint_id )
320368 if not ib .data .value then return end
321369 local lift_percent = 100 - math.floor (ib .data .value / 100 )
322- device :emit_event_for_endpoint (ib .endpoint_id , capabilities .windowShadeLevel .shadeLevel (lift_percent ))
323-
370+ device :emit_event (capabilities .windowShadeLevel .shadeLevel (lift_percent ))
324371 if is_target_value_reached (lift_percent , device :get_field (TARGET_LIFT_PERCENT .STATE )) then
325372 clear_cached_data (device , TARGET_LIFT_PERCENT )
326373 try_emit_idle_window_shade_status (device )
327374 end
328375end
329376
330377local function current_position_tilt_percent_100ths_handler (driver , device , ib , response )
378+ device = resolve_device_for_endpoint (device , ib .endpoint_id )
331379 if not ib .data .value then return end
332380 local tilt_percent = 100 - math.floor (ib .data .value / 100 )
333- device :emit_event_for_endpoint (ib .endpoint_id , capabilities .windowShadeTiltLevel .shadeTiltLevel (tilt_percent ))
334-
381+ device :emit_event (capabilities .windowShadeTiltLevel .shadeTiltLevel (tilt_percent ))
335382 if is_target_value_reached (tilt_percent , device :get_field (TARGET_TILT_PERCENT .STATE )) then
336383 clear_cached_data (device , TARGET_TILT_PERCENT )
337384 try_emit_idle_window_shade_status (device )
338385 end
339386end
340387
341388local function target_position_lift_percent_100ths_handler (driver , device , ib , response )
389+ device = resolve_device_for_endpoint (device , ib .endpoint_id )
342390 if type (ib .data .value ) == " number" then
343391 local target_lift_percent = 100 - math.floor (ib .data .value / 100 )
344392 local latest_lift_percentage = device :get_latest_state (
@@ -356,6 +404,7 @@ local function target_position_lift_percent_100ths_handler(driver, device, ib, r
356404end
357405
358406local function target_position_tilt_percent_100ths_handler (driver , device , ib , response )
407+ device = resolve_device_for_endpoint (device , ib .endpoint_id )
359408 if type (ib .data .value ) == " number" then
360409 local target_tilt_percent = 100 - math.floor (ib .data .value / 100 )
361410 local latest_tilt_percent = device :get_latest_state (
374423
375424-- checks the current position of the shade
376425local function operational_status_handler (driver , device , ib , response )
426+ device = resolve_device_for_endpoint (device , ib .endpoint_id )
377427 if not ib .data .value then return end
378428 local global_op_status = ib .data .value & clusters .WindowCovering .types .OperationalStatus .GLOBAL
379-
380429 local reverse = device .preferences .reverse
381430 local windowShade = capabilities .windowShade .windowShade
382431 local status_event
@@ -390,17 +439,19 @@ local function operational_status_handler(driver, device, ib, response)
390439 status_event = windowShade .unknown ()
391440 end
392441 if status_event then
393- device :emit_event_for_endpoint ( ib . endpoint_id , status_event )
442+ device :emit_event ( status_event )
394443 end
395444end
396445
397446local function battery_percent_remaining_attr_handler (driver , device , ib , response )
447+ device = resolve_device_for_endpoint (device , ib .endpoint_id )
398448 if ib .data .value then
399449 device :emit_event (capabilities .battery .battery (math.floor (ib .data .value / 2.0 + 0.5 )))
400450 end
401451end
402452
403453local function battery_charge_level_attr_handler (driver , device , ib , response )
454+ device = resolve_device_for_endpoint (device , ib .endpoint_id )
404455 if ib .data .value == clusters .PowerSource .types .BatChargeLevelEnum .OK then
405456 device :emit_event (capabilities .batteryLevel .battery .normal ())
406457 elseif ib .data .value == clusters .PowerSource .types .BatChargeLevelEnum .WARNING then
@@ -411,6 +462,7 @@ local function battery_charge_level_attr_handler(driver, device, ib, response)
411462end
412463
413464local function power_source_attribute_list_handler (driver , device , ib , response )
465+ device = resolve_device_for_endpoint (device , ib .endpoint_id )
414466 for _ , attr in ipairs (ib .data .elements or {}) do
415467 -- Re-profile the device if BatPercentRemaining (Attribute ID 0x0C) is present.
416468 if attr .value == 0x0C then
0 commit comments