Skip to content

Commit b53457f

Browse files
general cleanup
1 parent 249966c commit b53457f

File tree

5 files changed

+68
-86
lines changed

5 files changed

+68
-86
lines changed

drivers/SmartThings/matter-switch/src/button-utils.lua

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
-- limitations under the License.
1414

1515
local capabilities = require "st.capabilities"
16-
local common_utils = require "common-utils"
1716
local clusters = require "st.matter.clusters"
17+
local common_utils = require "common-utils"
1818
local log = require "log"
1919
local lua_socket = require "socket"
2020

@@ -39,7 +39,7 @@ button_utils.STATIC_BUTTON_PROFILE_SUPPORTED = {1, 2, 3, 4, 5, 6, 7, 8}
3939
local function create_multi_press_values_list(size, supportsHeld)
4040
local list = {"pushed", "double"}
4141
if supportsHeld then table.insert(list, "held") end
42-
-- add multi press values of 3 or greater to the list
42+
-- Add multi press values of 3 or greater to the list
4343
for i = 3, size do
4444
table.insert(list, string.format("pushed_%dx", i))
4545
end
@@ -72,7 +72,7 @@ function button_utils.configure_buttons(device)
7272
if device.profile.components[common_utils.endpoint_to_component(device, ep)] then
7373
device.log.info_with({hub_logs=true}, string.format("Configuring Supported Values for generic switch endpoint %d", ep))
7474
local supportedButtonValues_event
75-
-- this ordering is important, since MSM & MSL devices must also support MSR
75+
-- This ordering is important, since MSM & MSL devices must also support MSR
7676
if common_utils.tbl_contains(msm_eps, ep) then
7777
supportedButtonValues_event = nil -- deferred to the max press handler
7878
device:send(clusters.Switch.attributes.MultiPressMax:read(device, ep))
@@ -97,7 +97,7 @@ function button_utils.configure_buttons(device)
9797
end
9898

9999
function button_utils.build_button_component_map(device, main_endpoint, button_eps)
100-
-- create component mapping on the main profile button endpoints
100+
-- Create component mapping on the main profile button endpoints
101101
table.sort(button_eps)
102102
local component_map = {}
103103
component_map["main"] = main_endpoint
@@ -133,13 +133,13 @@ function button_utils.initial_press_event_handler(driver, device, ib, response)
133133
elseif common_utils.get_field_for_endpoint(device, INITIAL_PRESS_ONLY, ib.endpoint_id) then
134134
device:emit_event_for_endpoint(ib.endpoint_id, capabilities.button.button.pushed({state_change = true}))
135135
elseif common_utils.get_field_for_endpoint(device, EMULATE_HELD, ib.endpoint_id) then
136-
-- if our button doesn't differentiate between short and long holds, do it in code by keeping track of the press down time
136+
-- If our button doesn't differentiate between short and long holds, do it in code by keeping track of the press down time
137137
init_press(device, ib.endpoint_id)
138138
end
139139
end
140140

141-
-- if the device distinguishes a long press event, it will always be a "held"
142-
-- there's also a "long release" event, but this event is required to come first
141+
-- If the device distinguishes a long press event, it will always be a "held".
142+
-- There's also a "long release" event, but this event is required to come first.
143143
function button_utils.long_press_event_handler(driver, device, ib, response)
144144
device:emit_event_for_endpoint(ib.endpoint_id, capabilities.button.button.held({state_change = true}))
145145
if common_utils.get_field_for_endpoint(device, SUPPORTS_MULTI_PRESS, ib.endpoint_id) then
@@ -159,12 +159,9 @@ function button_utils.short_release_event_handler(driver, device, ib, response)
159159
end
160160

161161
function button_utils.multi_press_complete_event_handler(driver, device, ib, response)
162-
-- in the case of multiple button presses
163-
-- emit number of times, multiple presses have been completed
164162
if ib.data and not common_utils.get_field_for_endpoint(device, IGNORE_NEXT_MPC, ib.endpoint_id) then
165163
local press_value = ib.data.elements.total_number_of_presses_counted.value
166-
--capability only supports up to 6 presses
167-
if press_value < 7 then
164+
if press_value < 7 then -- button capability only supports up to 6 presses
168165
local button_event = capabilities.button.button.pushed({state_change = true})
169166
if press_value == 2 then
170167
button_event = capabilities.button.button.double({state_change = true})
@@ -180,15 +177,13 @@ function button_utils.multi_press_complete_event_handler(driver, device, ib, res
180177
end
181178

182179
function button_utils.max_press_handler(driver, device, ib, response)
183-
local max = ib.data.value or 1 -- get max number of presses
184-
device.log.debug("Device supports "..max.." presses")
185-
-- capability only supports up to 6 presses
186-
if max > 6 then
180+
local max = ib.data.value or 1
181+
if max > 6 then -- button capability only supports up to 6 presses
187182
log.info("Device supports more than 6 presses")
188183
max = 6
189184
end
190-
local MSL = device:get_endpoints(clusters.Switch.ID, {feature_bitmap=clusters.Switch.types.SwitchFeature.MOMENTARY_SWITCH_LONG_PRESS})
191-
local supportsHeld = common_utils.tbl_contains(MSL, ib.endpoint_id)
185+
local msl_eps = device:get_endpoints(clusters.Switch.ID, {feature_bitmap=clusters.Switch.types.SwitchFeature.MOMENTARY_SWITCH_LONG_PRESS})
186+
local supportsHeld = common_utils.tbl_contains(msl_eps, ib.endpoint_id)
192187
local values = create_multi_press_values_list(max, supportsHeld)
193188
device:emit_event_for_endpoint(ib.endpoint_id, capabilities.button.supportedButtonValues(values, {visibility = {displayed = false}}))
194189
end

drivers/SmartThings/matter-switch/src/common-utils.lua

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ end
203203
function common_utils.is_eve_energy_products(opts, driver, device)
204204
-- this sub driver does not support child devices
205205
if device.network_type == device_lib.NETWORK_TYPE_MATTER and
206-
device.manufacturer_info.vendor_id == EVE_MANUFACTURER_ID then
206+
device.manufacturer_info.vendor_id == EVE_MANUFACTURER_ID then
207207
return true
208208
end
209209
log.info("Using Eve Energy sub driver")
@@ -212,8 +212,8 @@ end
212212

213213
function common_utils.is_third_reality_mk1(opts, driver, device)
214214
if device.network_type == device_lib.NETWORK_TYPE_MATTER and
215-
device.manufacturer_info.vendor_id == THIRD_REALITY_MK1_FINGERPRINT.vendor_id and
216-
device.manufacturer_info.product_id == THIRD_REALITY_MK1_FINGERPRINT.product_id then
215+
device.manufacturer_info.vendor_id == THIRD_REALITY_MK1_FINGERPRINT.vendor_id and
216+
device.manufacturer_info.product_id == THIRD_REALITY_MK1_FINGERPRINT.product_id then
217217
log.info("Using Third Reality MK1 sub driver")
218218
return true
219219
end
@@ -222,9 +222,9 @@ end
222222

223223
function common_utils.is_static_profile_device(opts, driver, device)
224224
if not common_utils.is_aqara_cube(opts, driver, device) and
225-
not common_utils.is_eve_energy_products(opts, driver, device) and
226-
not common_utils.is_third_reality_mk1(opts, driver, device) and
227-
not common_utils.supports_modular_profile(device) then
225+
not common_utils.is_eve_energy_products(opts, driver, device) and
226+
not common_utils.is_third_reality_mk1(opts, driver, device) and
227+
not common_utils.supports_modular_profile(device) then
228228
log.info("Using Static Profile sub driver")
229229
return true
230230
end
@@ -384,7 +384,6 @@ end
384384

385385
local function assign_child_profile(device, child_ep)
386386
local profile
387-
388387
for _, ep in ipairs(device.endpoints) do
389388
if ep.endpoint_id == child_ep then
390389
-- Some devices report multiple device types which are a subset of
@@ -400,7 +399,6 @@ local function assign_child_profile(device, child_ep)
400399
break
401400
end
402401
end
403-
404402
-- Check if device has an overridden child profile that differs from the profile that would match
405403
-- the child's device type for the following two cases:
406404
-- 1. To add Electrical Sensor only to the first EDGE_CHILD (light-power-energy-powerConsumption)
@@ -416,8 +414,7 @@ local function assign_child_profile(device, child_ep)
416414
end
417415
end
418416
end
419-
420-
-- default to "switch-binary" if no profile is found
417+
-- Default to "switch-binary" if no profile is found
421418
return profile or "switch-binary"
422419
end
423420

@@ -444,20 +441,18 @@ function common_utils.build_child_switch_profiles(driver, device, main_endpoint)
444441
)
445442
parent_child_device = true
446443
if _ == 1 and string.find(child_profile, "energy") then
447-
-- when energy management is defined in the root endpoint(0), replace it with the first switch endpoint and process it.
444+
-- When energy management is defined in the root endpoint(0), replace it with
445+
-- the first switch endpoint and process it.
448446
device:set_field(common_utils.ENERGY_MANAGEMENT_ENDPOINT, ep, {persist = true})
449447
end
450448
end
451449
end
452450
end
453-
454-
-- If the device is a parent child device, set the find_child function on init. This is persisted because initialize_buttons_and_switches
455-
-- is only run once, but find_child function should be set on each driver init.
451+
-- If the device is a parent child device, set the field and persist because the
452+
-- find_child function should be set on each driver init.
456453
if parent_child_device then
457454
device:set_field(common_utils.IS_PARENT_CHILD_DEVICE, true, {persist = true})
458455
end
459-
460-
-- this is needed in initialize_buttons_and_switches
461456
return num_switch_server_eps
462457
end
463458

drivers/SmartThings/matter-switch/src/init.lua

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414

1515
local button_utils = require "button-utils"
1616
local capabilities = require "st.capabilities"
17+
local clusters = require "st.matter.clusters"
1718
local color_utils = require "color-utils"
1819
local common_utils = require "common-utils"
19-
local log = require "log"
20-
local clusters = require "st.matter.clusters"
20+
local device_lib = require "st.device"
21+
local embedded_cluster_utils = require "embedded-cluster-utils"
2122
local im = require "st.matter.interaction_model"
23+
local log = require "log"
2224
local MatterDriver = require "st.matter.driver"
2325
local modular_profiles_utils = require "modular-profiles-utils"
2426
local utils = require "st.utils"
25-
local device_lib = require "st.device"
26-
local embedded_cluster_utils = require "embedded-cluster-utils"
2727
local version = require "version"
2828

2929
-- Include driver-side definitions when lua libs api version is < 11
@@ -55,6 +55,9 @@ local LEVEL_BOUND_RECEIVED = "__level_bound_received"
5555
local LEVEL_MIN = "__level_min"
5656
local LEVEL_MAX = "__level_max"
5757
local COLOR_MODE = "__color_mode"
58+
local TEMP_BOUND_RECEIVED = "__temp_bound_received"
59+
local TEMP_MIN = "__temp_min"
60+
local TEMP_MAX = "__temp_max"
5861

5962
local HUE_SAT_COLOR_MODE = clusters.ColorControl.types.ColorMode.CURRENT_HUE_AND_CURRENT_SATURATION
6063
local X_Y_COLOR_MODE = clusters.ColorControl.types.ColorMode.CURRENTX_AND_CURRENTY
@@ -127,7 +130,7 @@ end
127130
local function set_poll_report_timer_and_schedule(device, is_cumulative_report)
128131
local cumul_eps = embedded_cluster_utils.get_endpoints(device,
129132
clusters.ElectricalEnergyMeasurement.ID,
130-
{feature_bitmap = clusters.ElectricalEnergyMeasurement.types.Feature.CUMULATIVE_ENERGY })
133+
{feature_bitmap = clusters.ElectricalEnergyMeasurement.types.Feature.CUMULATIVE_ENERGY})
131134
if #cumul_eps == 0 then
132135
device:set_field(CUMULATIVE_REPORTS_NOT_SUPPORTED, true, {persist = true})
133136
end
@@ -152,10 +155,6 @@ local function set_poll_report_timer_and_schedule(device, is_cumulative_report)
152155
end
153156
end
154157

155-
local TEMP_BOUND_RECEIVED = "__temp_bound_received"
156-
local TEMP_MIN = "__temp_min"
157-
local TEMP_MAX = "__temp_max"
158-
159158
local function convert_huesat_st_to_matter(val)
160159
return utils.clamp_value(math.floor((val * 0xFE) / 100.0 + 0.5), CURRENT_HUESAT_ATTR_MIN, CURRENT_HUESAT_ATTR_MAX)
161160
end
@@ -385,15 +384,15 @@ local function level_attr_handler(driver, device, ib, response)
385384
end
386385

387386
local function hue_attr_handler(driver, device, ib, response)
388-
if device:get_field(COLOR_MODE) == X_Y_COLOR_MODE or ib.data.value == nil then
387+
if device:get_field(COLOR_MODE) == X_Y_COLOR_MODE or ib.data.value == nil then
389388
return
390389
end
391390
local hue = math.floor((ib.data.value / 0xFE * 100) + 0.5)
392391
device:emit_event_for_endpoint(ib.endpoint_id, capabilities.colorControl.hue(hue))
393392
end
394393

395394
local function sat_attr_handler(driver, device, ib, response)
396-
if device:get_field(COLOR_MODE) == X_Y_COLOR_MODE or ib.data.value == nil then
395+
if device:get_field(COLOR_MODE) == X_Y_COLOR_MODE or ib.data.value == nil then
397396
return
398397
end
399398
local sat = math.floor((ib.data.value / 0xFE * 100) + 0.5)

drivers/SmartThings/matter-switch/src/modular-profiles-utils.lua

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
local button_utils = require "button-utils"
1616
local capabilities = require "st.capabilities"
17-
local common_utils = require "common-utils"
1817
local clusters = require "st.matter.clusters"
18+
local common_utils = require "common-utils"
1919

2020
local modular_profiles_utils = {}
2121

@@ -104,19 +104,6 @@ local function supports_capability_by_id_modular(device, capability, component)
104104
return false
105105
end
106106

107-
local function add_energy_and_power_capabilities(component_capabilities, num_energy_eps, num_power_eps)
108-
if num_energy_eps > 0 and num_power_eps > 0 then
109-
table.insert(component_capabilities, capabilities.powerMeter.ID)
110-
table.insert(component_capabilities, capabilities.energyMeter.ID)
111-
table.insert(component_capabilities, capabilities.powerConsumptionReport.ID)
112-
elseif num_energy_eps > 0 then
113-
table.insert(component_capabilities, capabilities.energyMeter.ID)
114-
table.insert(component_capabilities, capabilities.powerConsumptionReport.ID)
115-
elseif num_power_eps > 0 then
116-
table.insert(component_capabilities, capabilities.powerMeter.ID)
117-
end
118-
end
119-
120107
local function add_battery_capability(component_capabilities, battery_attr_support)
121108
if battery_attr_support == common_utils.battery_support.BATTERY_PERCENTAGE then
122109
table.insert(component_capabilities, capabilities.battery.ID)
@@ -198,14 +185,14 @@ end
198185
local function match_modular_profile(driver, device, battery_attr_support)
199186
local main_endpoint = common_utils.find_default_endpoint(device)
200187
local color_hs_eps = device:get_endpoints(clusters.ColorControl.ID, {feature_bitmap = clusters.ColorControl.types.Feature.HS})
201-
local color_temp_eps = device:get_endpoints(clusters.ColorControl.ID, {feature_bitmap = clusters.ColorControl.types.Feature.CT})
202188
local color_xy_eps = device:get_endpoints(clusters.ColorControl.ID, {feature_bitmap = clusters.ColorControl.types.Feature.XY})
203-
local energy_eps = device:get_endpoints(clusters.ElectricalEnergyMeasurement.ID)
204-
local fan_eps = device:get_endpoints(clusters.FanControl.ID)
205-
local humidity_eps = device:get_endpoints(clusters.RelativeHumidityMeasurement.ID)
189+
local color_temp_eps = device:get_endpoints(clusters.ColorControl.ID, {feature_bitmap = clusters.ColorControl.types.Feature.CT})
206190
local level_eps = device:get_endpoints(clusters.LevelControl.ID)
207191
local power_eps = device:get_endpoints(clusters.ElectricalPowerMeasurement.ID)
192+
local energy_eps = device:get_endpoints(clusters.ElectricalEnergyMeasurement.ID)
208193
local switch_eps = device:get_endpoints(clusters.OnOff.ID)
194+
local fan_eps = device:get_endpoints(clusters.FanControl.ID)
195+
local humidity_eps = device:get_endpoints(clusters.RelativeHumidityMeasurement.ID)
209196
local temperature_eps = device:get_endpoints(clusters.TemperatureMeasurement.ID)
210197
local valve_eps = device:get_endpoints(clusters.ValveConfigurationAndControl.ID)
211198

@@ -215,8 +202,7 @@ local function match_modular_profile(driver, device, battery_attr_support)
215202
local main_component_capabilities = {}
216203
local extra_component_capabilities = {}
217204

218-
local MAIN_COMPONENT_IDX = 1
219-
local CAPABILITIES_LIST_IDX = 2
205+
local MAIN_COMPONENT_IDX, CAPABILITIES_LIST_IDX = 1, 2
220206

221207
add_button_capabilities(device, category, main_endpoint, main_component_capabilities, extra_component_capabilities, battery_attr_support)
222208

@@ -234,17 +220,15 @@ local function match_modular_profile(driver, device, battery_attr_support)
234220
table.insert(main_component_capabilities, capabilities.switchLevel.ID)
235221
end
236222

237-
if #fan_eps > 0 then
238-
table.insert(main_component_capabilities, capabilities.fanMode.ID)
239-
table.insert(main_component_capabilities, capabilities.fanSpeedPercent.ID)
223+
if #power_eps > 0 then
224+
table.insert(main_component_capabilities, capabilities.powerMeter.ID)
240225
end
241226

242-
if #humidity_eps > 0 then
243-
table.insert(main_component_capabilities, capabilities.relativeHumidityMeasurement.ID)
227+
if #energy_eps > 0 then
228+
table.insert(main_component_capabilities, capabilities.energyMeter.ID)
229+
table.insert(main_component_capabilities, capabilities.powerConsumptionReport.ID)
244230
end
245231

246-
add_energy_and_power_capabilities(main_component_capabilities, #energy_eps, #power_eps)
247-
248232
if #switch_eps > 0 then
249233
-- If the device is a Button or Water Valve, add the switch capability since
250234
-- it is not a mandatory capability for these device types.
@@ -261,6 +245,15 @@ local function match_modular_profile(driver, device, battery_attr_support)
261245
end
262246
end
263247

248+
if #fan_eps > 0 then
249+
table.insert(main_component_capabilities, capabilities.fanMode.ID)
250+
table.insert(main_component_capabilities, capabilities.fanSpeedPercent.ID)
251+
end
252+
253+
if #humidity_eps > 0 then
254+
table.insert(main_component_capabilities, capabilities.relativeHumidityMeasurement.ID)
255+
end
256+
264257
if #temperature_eps > 0 then
265258
table.insert(main_component_capabilities, capabilities.temperatureMeasurement.ID)
266259
end
@@ -278,19 +271,18 @@ local function match_modular_profile(driver, device, battery_attr_support)
278271
table.insert(optional_supported_component_capabilities, component_capability)
279272
end
280273

281-
local total_supported_capabilities = optional_supported_component_capabilities
282-
283274
device:try_update_metadata({profile = profile_name_and_mandatory_capability_per_device_category[category].profile_name,
284275
optional_component_capabilities = optional_supported_component_capabilities})
285276

286-
-- add mandatory capabilities for subscription
277+
local total_supported_capabilities = optional_supported_component_capabilities
278+
-- Add mandatory capabilities for subscription
287279
table.insert(total_supported_capabilities[MAIN_COMPONENT_IDX][CAPABILITIES_LIST_IDX],
288280
profile_name_and_mandatory_capability_per_device_category[category].mandatory_capability)
289281
table.insert(total_supported_capabilities[MAIN_COMPONENT_IDX][CAPABILITIES_LIST_IDX], capabilities.refresh.ID)
290282
table.insert(total_supported_capabilities[MAIN_COMPONENT_IDX][CAPABILITIES_LIST_IDX], capabilities.firmwareUpdate.ID)
291283

292284
device:set_field(modular_profiles_utils.SUPPORTED_COMPONENT_CAPABILITIES, total_supported_capabilities, {persist = true})
293-
-- re-up subscription with new capabilities using the modular supports_capability override
285+
-- Re-up subscription with new capabilities using the modular supports_capability override
294286
device:extend_device("supports_capability_by_id", supports_capability_by_id_modular)
295287
end
296288

0 commit comments

Comments
 (0)