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"
@@ -10,6 +9,12 @@ local clusters = require "st.matter.clusters"
109local MatterDriver = require " st.matter.driver"
1110local utils = require " st.utils"
1211
12+ local IS_PARENT_CHILD_DEVICE = " __is_parent_child_device"
13+ --- If the ASSIGNED_CHILD_KEY field is populated for an endpoint, it should be
14+ --- used as the key in the get_child_by_parent_assigned_key() function. This allows
15+ --- multiple endpoints to associate with the same child device, though right now child
16+ local ASSIGNED_CHILD_KEY = " __assigned_child_key"
17+
1318local battery_support = {
1419 NO_BATTERY = " NO_BATTERY" ,
1520 BATTERY_LEVEL = " BATTERY_LEVEL" ,
@@ -154,6 +159,89 @@ local function component_to_endpoint(device, component_name)
154159 return find_default_endpoint (device , clusters .WindowCovering .ID )
155160end
156161
162+ local function create_feature_table (lift_eps , tilt_eps )
163+ local result = {}
164+ local i , j = 1 , 1
165+ while i <= # lift_eps or j <= # tilt_eps do
166+ local lift_ep , tilt_ep = lift_eps [i ], tilt_eps [j ]
167+ if tilt_ep == nil or (lift_ep ~= nil and lift_ep < tilt_ep ) then
168+ result [# result + 1 ] = { endpoint = lift_ep , has_lift = true , has_tilt = false }
169+ i = i + 1
170+ elseif lift_ep == nil or tilt_ep < lift_ep then
171+ result [# result + 1 ] = { endpoint = tilt_ep , has_lift = false , has_tilt = true }
172+ j = j + 1
173+ else -- lift_ep == tilt_ep
174+ result [# result + 1 ] = { endpoint = lift_ep , has_lift = true , has_tilt = true }
175+ i , j = i + 1 , j + 1
176+ end
177+ end
178+ return result
179+ end
180+
181+ local function update_profile_table (device )
182+ device .log .info_with ({hub_logs = true }, string.format (" !!! update_profile_table !!!" ))
183+ local lift_eps = device :get_endpoints (clusters .WindowCovering .ID , {feature_bitmap = clusters .WindowCovering .types .Feature .LIFT })
184+ local tilt_eps = device :get_endpoints (clusters .WindowCovering .ID , {feature_bitmap = clusters .WindowCovering .types .Feature .TILT })
185+ table.sort (lift_eps )
186+ table.sort (tilt_eps )
187+ local feature_table = create_feature_table (lift_eps , tilt_eps )
188+ local profile_table = {}
189+
190+ for _ , entry in pairs (feature_table ) do
191+ device .log .info_with ({hub_logs = true }, string.format (" !!! endpoint: %s, has_lift: %s, has_tilt: %s !!!" , entry .endpoint , entry .has_lift , entry .has_tilt ))
192+ profile_table [entry .endpoint ] = " window-covering"
193+ if entry .has_tilt then
194+ device .log .info_with ({hub_logs = true }, string.format (" !!! window-covering-tilt !!!" ))
195+ profile_table [entry .endpoint ] = " window-covering-tilt"
196+ if not entry .has_lift then
197+ device .log .info_with ({hub_logs = true }, string.format (" !!! window-covering-tilt-only !!!" ))
198+ profile_table [entry .endpoint ] = " window-covering-tilt-only"
199+ end
200+ end
201+ end
202+ return profile_table
203+ end
204+
205+ local function find_child (parent_device , ep_id )
206+ parent_device .log .info_with ({hub_logs = true }, string.format (" !!! find_child !!!" ))
207+ local assigned_key = parent_device :get_field (string.format (" %s_%d" , ASSIGNED_CHILD_KEY , ep_id )) or ep_id
208+ return parent_device :get_child_by_parent_assigned_key (string.format (" %d" , assigned_key ))
209+ end
210+
211+ local function create_or_update_multi_devices (driver , device , wc_cluster_eps )
212+ device .log .info_with ({hub_logs = true }, string.format (" !!! create_or_update_child_devices !!!" ))
213+ local profile_table = update_profile_table (device )
214+ local default_endpoint_id = find_default_endpoint (device )
215+ table.sort (wc_cluster_eps )
216+ for device_num , ep_id in ipairs (wc_cluster_eps ) do
217+ if ep_id ~= default_endpoint_id then -- don't create a child device that maps to the main endpoint
218+ local label_and_name = string.format (" %s %d" , device .label , device_num )
219+ local child_profile = profile_table [ep_id ] or " window-covering"
220+ local existing_child_device = device :get_field (IS_PARENT_CHILD_DEVICE ) and find_child (device , ep_id )
221+
222+ device .log .info_with ({hub_logs = true }, string.format (" !!! do_create_or_update: endpoint: %s, profile: %s !!!" , ep_id , child_profile ))
223+
224+ if not existing_child_device then
225+ driver :try_create_device ({
226+ type = " EDGE_CHILD" ,
227+ label = label_and_name ,
228+ profile = child_profile ,
229+ parent_device_id = device .id ,
230+ parent_assigned_child_key = string.format (" %d" , ep_id ),
231+ vendor_provided_label = label_and_name
232+ })
233+ else
234+ existing_child_device :try_update_metadata ({profile = child_profile })
235+ end
236+ end
237+ end
238+ -- Persist so that the find_child function is always set on each driver init.
239+ device :set_field (IS_PARENT_CHILD_DEVICE , true , {persist = true })
240+ device :set_find_child (find_child )
241+ -- Update parent device's profile
242+ device :try_update_metadata ({profile = profile_table [default_endpoint_id ] or " window-covering" })
243+ end
244+
157245local function match_profile (device , battery_supported )
158246 local lift_eps = device :get_endpoints (clusters .WindowCovering .ID , {feature_bitmap = clusters .WindowCovering .types .Feature .LIFT })
159247 local tilt_eps = device :get_endpoints (clusters .WindowCovering .ID , {feature_bitmap = clusters .WindowCovering .types .Feature .TILT })
@@ -191,20 +279,25 @@ local function device_init(driver, device)
191279end
192280
193281local function do_configure (driver , device )
194- local battery_feature_eps = device :get_endpoints (clusters .PowerSource .ID , {feature_bitmap = clusters .PowerSource .types .PowerSourceFeature .BATTERY })
195- if # battery_feature_eps > 0 then
196- local attribute_list_read = im .InteractionRequest (im .InteractionRequest .RequestType .READ , {})
197- attribute_list_read :merge (clusters .PowerSource .attributes .AttributeList :read ())
198- device :send (attribute_list_read )
282+ local wc_cluster_eps = device :get_endpoints (clusters .WindowCovering .ID )
283+ if # wc_cluster_eps > 1 then
284+ create_or_update_multi_devices (driver , device , wc_cluster_eps )
199285 else
200- match_profile (device , battery_support .NO_BATTERY )
286+ local battery_feature_eps = device :get_endpoints (clusters .PowerSource .ID , {feature_bitmap = clusters .PowerSource .types .PowerSourceFeature .BATTERY })
287+ if # battery_feature_eps > 0 then
288+ local attribute_list_read = im .InteractionRequest (im .InteractionRequest .RequestType .READ , {})
289+ attribute_list_read :merge (clusters .PowerSource .attributes .AttributeList :read ())
290+ device :send (attribute_list_read )
291+ else
292+ match_profile (device , battery_support .NO_BATTERY )
293+ end
201294 end
202295end
203296
204297local function info_changed (driver , device , event , args )
205298 if device .profile .id ~= args .old_st_store .profile .id then
206299 device :subscribe ()
207- elseif device .matter_version .software ~= args .old_st_store .matter_version .software then
300+ elseif args . old_st_store . matter_version and device .matter_version .software ~= args .old_st_store .matter_version .software then
208301 local battery_feature_eps = device :get_endpoints (clusters .PowerSource .ID , {feature_bitmap = clusters .PowerSource .types .PowerSourceFeature .BATTERY })
209302 if # battery_feature_eps > 0 then
210303 local attribute_list_read = im .InteractionRequest (im .InteractionRequest .RequestType .READ , {})
@@ -217,6 +310,7 @@ local function info_changed(driver, device, event, args)
217310end
218311
219312local function device_added (driver , device )
313+ device :send (clusters .WindowCovering .attributes .OperationalStatus :read ())
220314 device :emit_event (
221315 capabilities .windowShade .supportedWindowShadeCommands ({" open" , " close" , " pause" }, {visibility = {displayed = false }})
222316 )
0 commit comments