Skip to content

Commit

Permalink
Driver tests for lazy subdriver loading
Browse files Browse the repository at this point in the history
This utilizes a new method of loading subdrivers that allows
the sub drivers to be dynamically loaded when needed rather
than having every subdriver loaded at initialization.
  • Loading branch information
ctowns committed Oct 11, 2023
1 parent e672912 commit 48e57dc
Show file tree
Hide file tree
Showing 26 changed files with 140 additions and 47 deletions.
3 changes: 1 addition & 2 deletions drivers/SmartThings/matter-button/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ local function find_default_endpoint(device, component)
table.sort(eps)
for _, v in ipairs(eps) do
if v ~= 0 then --0 is the matter RootNode endpoint
res = v
break
return v
end
end
device.log.warn(string.format("Did not find default endpoint, will use endpoint %d instead", device.MATTER_DEFAULT_ENDPOINT))
Expand Down
6 changes: 4 additions & 2 deletions drivers/SmartThings/matter-switch/src/eve-energy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ local REPORT_TIMEOUT = (15 * 60) -- Report the value each 15 minutes

local function is_eve_energy_products(opts, driver, device)
if device.manufacturer_info.vendor_id == EVE_MANUFACTURER_ID then
return true
local subdriver = require("eve-energy")
return true, subdriver
end

return false
end

Expand Down Expand Up @@ -217,13 +217,15 @@ end
-------------------------------------------------------------------------------------

local function watt_attr_handler(driver, device, ib, zb_rx)
print("watt_attr_handler")
if ib.data.value then
local wattValue = ib.data.value
device:emit_event(capabilities.powerMeter.power({ value = wattValue, unit = "W" }))
end
end

local function watt_accumulated_attr_handler(driver, device, ib, zb_rx)
print("watt_accumlated_attr_handler")
if ib.data.value then
local totalConsumptionRawValue = ib.data.value
local totalConsumptionWh = utils.round(1000 * totalConsumptionRawValue)
Expand Down
4 changes: 3 additions & 1 deletion drivers/SmartThings/matter-switch/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local capabilities = require "st.capabilities"
local log = require "log"
local clusters = require "st.matter.clusters"
local MatterDriver = require "st.matter.driver"
local Driver = require "st.driver"
local utils = require "st.utils"

local MOST_RECENT_TEMP = "mostRecentTemp"
Expand Down Expand Up @@ -370,7 +371,8 @@ local matter_driver_template = {
capabilities.colorTemperature,
},
sub_drivers = {
require("eve-energy")
MatterDriver.lazy_load_sub_driver(require("eve-energy")),
-- MatterDriver.lazy_load_sub_driver(require("lazy-load-test"))
}
}

Expand Down
50 changes: 50 additions & 0 deletions drivers/SmartThings/matter-switch/src/lazy-load-test/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- Copyright 2023 SmartThings
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-------------------------------------------------------------------------------------
-- Definitions
-------------------------------------------------------------------------------------

local capabilities = require "st.capabilities"
local log = require "log"
local clusters = require "st.matter.clusters"
local cluster_base = require "st.matter.cluster_base"
local utils = require "st.utils"
local data_types = require "st.matter.data_types"

local NANOLEAF_MANUFACTURER_ID = 0x115A

local function can_handle_nanoleaf(opts, driver, device)
if device.manufacturer_info.vendor_id == NANOLEAF_MANUFACTURER_ID then
local subdriver = require("lazy-load-test")
return true, subdriver
end
return false
end

local function handle_refresh(self, device)
device.log("Lazy loaded refresh for nanoleaf")
end

local eve_energy_handler = {
NAME = "Nanoleaf Lazy Load Handler",
capability_handlers = {
[capabilities.refresh.ID] = {
[capabilities.refresh.commands.refresh.NAME] = handle_refresh,
}
},
can_handle = can_handle_nanoleaf
}

return eve_energy_handler
3 changes: 2 additions & 1 deletion drivers/SmartThings/zigbee-button/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local ZigbeeDriver = require "st.zigbee"
local defaults = require "st.zigbee.defaults"
local constants = require "st.zigbee.constants"
local IASZone = (require "st.zigbee.zcl.clusters").IASZone
local Driver = require "st.driver"

local generate_event_from_zone_status = function(driver, device, zone_status, zb_rx)
local event
Expand Down Expand Up @@ -95,7 +96,7 @@ local zigbee_button_driver_template = {
require("zigbee-multi-button"),
require("dimming-remote"),
require("iris"),
require("samjin"),
Driver.lazy_load_sub_driver(require("samjin")),
require("ewelink"),
require("thirdreality")
},
Expand Down
17 changes: 14 additions & 3 deletions drivers/SmartThings/zigbee-button/src/samjin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,29 @@ battery_config.reportable_change = 0x10
battery_config.data_type = zcl_clusters.PowerConfiguration.attributes.BatteryVoltage.base_type

local function init_handler(self, device)
device.log.info("lazy loaded zigbee button init")
device:add_configured_attribute(battery_config)
device:add_monitored_attribute(battery_config)
end

local function can_handle_samjin(opts, driver, device, ...)
device.log.info("can_handle samjin called")

if device:get_manufacturer() == "Samjin" and device:get_model() == "button" then
local subdriver = require("samjin")
device.log.info("can_handle samjin called - true")

return true, subdriver
end
return false
end

local samjin_button = {
NAME = "Samjin Button Handler",
lifecycle_handlers = {
init = init_handler
},
can_handle = function(opts, driver, device, ...)
return device:get_manufacturer() == "Samjin" and device:get_model() == "button"
end
can_handle = can_handle_samjin
}

return samjin_button
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ local POWER_UNIT_WATT = "W"
local function can_handle_aeon_smart_strip(opts, driver, device, ...)
for _, fingerprint in ipairs(AEON_SMART_STRIP_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("aeon-smart-strip")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ local FINGERPRINTS = {

local function can_handle(opts, driver, device, ...)
for _, fingerprint in ipairs(FINGERPRINTS) do
if device:id_match(fingerprint.mfr, nil, fingerprint.prodId) then return true end
if device:id_match(fingerprint.mfr, nil, fingerprint.prodId) then
local subdriver = require("aeotec-smart-switch")
return true, subdriver
end
end
return false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ local DAWON_SMART_PLUG_FINGERPRINTS = {
local function can_handle_dawon_smart_plug(opts, driver, device, ...)
for _, fingerprint in ipairs(DAWON_SMART_PLUG_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("dawon-smart-plug")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ local DAWON_WALL_SMART_SWITCH_FINGERPRINTS = {
local function can_handle_dawon_wall_smart_switch(opts, driver, device, ...)
for _, fingerprint in ipairs(DAWON_WALL_SMART_SWITCH_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("dawon-wall-smart-switch")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ end
local function can_handle_eaton_5_scene_keypad(opts, driver, device, ...)
for _, fingerprint in ipairs(EATON_5_SCENE_KEYPAD_FINGERPRINT) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("eaton-5-scene-keypad")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ local EATON_ACCESSORY_DIMMER_FINGERPRINTS = {
local function can_handle_eaton_accessory_dimmer(opts, driver, device, ...)
for _, fingerprint in ipairs(EATON_ACCESSORY_DIMMER_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("eaton-accessory-dimmer")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ local EATON_ANYPLACE_SWITCH_FINGERPRINTS = {
local function can_handle_eaton_anyplace_switch(opts, driver, device, ...)
for _, fingerprint in ipairs(EATON_ANYPLACE_SWITCH_FINGERPRINTS) do
if device:id_match(fingerprint.manufacturerId, fingerprint.productType, fingerprint.productId) then
return true
local subdriver = require("eaton-anyplace-switch")
return true, subdriver
end
end
return false
Expand Down
3 changes: 2 additions & 1 deletion drivers/SmartThings/zwave-switch/src/ecolink-switch/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ local ECOLINK_FINGERPRINTS = {
local function can_handle_ecolink(opts, driver, device, ...)
for _, fingerprint in ipairs(ECOLINK_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("ecolink-switch")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ local FIBARO_DOUBLE_SWITCH_FINGERPRINTS = {
local function can_handle_fibaro_double_switch(opts, driver, device, ...)
for _, fingerprint in ipairs(FIBARO_DOUBLE_SWITCH_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("fibaro-double-switch")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ local FIBARO_SINGLE_SWITCH_FINGERPRINTS = {
local function can_handle_fibaro_single_switch(opts, driver, device, ...)
for _, fingerprint in ipairs(FIBARO_SINGLE_SWITCH_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("fibaro-single-switch")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ local FIBARO_WALL_PLUG_FINGERPRINTS = {
local function can_handle_fibaro_wall_plug(opts, driver, device, ...)
for _, fingerprint in ipairs(FIBARO_WALL_PLUG_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("fibaro-wall-plug-us")
return true, subdriver
end
end
return false
Expand Down
39 changes: 20 additions & 19 deletions drivers/SmartThings/zwave-switch/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local defaults = require "st.zwave.defaults"
local st_device = require "st.device"
--- @type st.zwave.Driver
local ZwaveDriver = require "st.zwave.driver"
local Driver = require "st.driver"
--- @type st.zwave.CommandClass
local cc = require "st.zwave.CommandClass"
--- @type st.zwave.CommandClass.Configuration
Expand Down Expand Up @@ -130,25 +131,25 @@ local driver_template = {
}
},
sub_drivers = {
require("eaton-accessory-dimmer"),
require("inovelli-LED"),
require("dawon-smart-plug"),
require("inovelli-2-channel-smart-plug"),
require("zwave-dual-switch"),
require("eaton-anyplace-switch"),
require("fibaro-wall-plug-us"),
require("dawon-wall-smart-switch"),
require("zooz-power-strip"),
require("aeon-smart-strip"),
require("qubino-switches"),
require("fibaro-double-switch"),
require("fibaro-single-switch"),
require("eaton-5-scene-keypad"),
require("ecolink-switch"),
require("multi-metering-switch"),
require("zooz-zen-30-dimmer-relay"),
require("multichannel-device"),
require("aeotec-smart-switch")
ZwaveDriver.lazy_load_sub_driver(require("eaton-accessory-dimmer")),
ZwaveDriver.lazy_load_sub_driver(require("inovelli-LED")),
ZwaveDriver.lazy_load_sub_driver(require("dawon-smart-plug")),
ZwaveDriver.lazy_load_sub_driver(require("inovelli-2-channel-smart-plug")),
ZwaveDriver.lazy_load_sub_driver(require("zwave-dual-switch")),
ZwaveDriver.lazy_load_sub_driver(require("eaton-anyplace-switch")),
ZwaveDriver.lazy_load_sub_driver(require("fibaro-wall-plug-us")),
ZwaveDriver.lazy_load_sub_driver(require("dawon-wall-smart-switch")),
ZwaveDriver.lazy_load_sub_driver(require("zooz-power-strip")),
ZwaveDriver.lazy_load_sub_driver(require("aeon-smart-strip")),
ZwaveDriver.lazy_load_sub_driver(require("qubino-switches")),
ZwaveDriver.lazy_load_sub_driver(require("fibaro-double-switch")),
ZwaveDriver.lazy_load_sub_driver(require("fibaro-single-switch")),
ZwaveDriver.lazy_load_sub_driver(require("eaton-5-scene-keypad")),
ZwaveDriver.lazy_load_sub_driver(require("ecolink-switch")),
ZwaveDriver.lazy_load_sub_driver(require("multi-metering-switch")),
ZwaveDriver.lazy_load_sub_driver(require("zooz-zen-30-dimmer-relay")),
ZwaveDriver.lazy_load_sub_driver(require("multichannel-device")),
ZwaveDriver.lazy_load_sub_driver(require("aeotec-smart-switch"))
},
lifecycle_handlers = {
init = device_init,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ local INOVELLI_2_CHANNEL_SMART_PLUG_FINGERPRINTS = {
local function can_handle_inovelli_2_channel_smart_plug(opts, driver, device, ...)
for _, fingerprint in ipairs(INOVELLI_2_CHANNEL_SMART_PLUG_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("inovelli-2-channel-smart-plug")
return true, subdriver
end
end
return false
Expand Down
3 changes: 2 additions & 1 deletion drivers/SmartThings/zwave-switch/src/inovelli-LED/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ local function can_handle_inovelli_led(opts, driver, device, ...)
{INOVELLI_LZW31SN_PRODUCT_TYPE, INOVELLI_LZW31_PRODUCT_TYPE},
INOVELLI_DIMMER_PRODUCT_ID
) then
return true
local subdriver = require("inovelli-LED")
return true, subdriver
end
return false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ local MULTI_METERING_SWITCH_FINGERPRINTS = {
local function can_handle_multi_metering_switch(opts, driver, device, ...)
for _, fingerprint in ipairs(MULTI_METERING_SWITCH_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("multi-metering-switch")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ local map_device_class_to_profile = {
}

local function can_handle_multichannel_device(opts, driver, device, ...)
return device:supports_capability(capabilities.zwMultichannel)
if device:supports_capability(capabilities.zwMultichannel) then
local subdriver = require("multichannel-device")
return true, subdriver
end
return false
end

local function find_child(device, src_channel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ local function getDeviceProfile(device, isTemperatureSensorOnboard)
end

local function can_handle_qubino_flush_relay(opts, driver, device, cmd, ...)
return device:id_match(constants.QUBINO_MFR)
if device:id_match(constants.QUBINO_MFR) then
local subdriver = require("qubino-switches")
return true, subdriver
end
return false
end

local function add_temperature_sensor_if_needed(device)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ local ZOOZ_POWER_STRIP_FINGERPRINTS = {
local function can_handle_zooz_power_strip(opts, driver, device, ...)
for _, fingerprint in ipairs(ZOOZ_POWER_STRIP_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("zooz-power-strip")
return true, subdriver
end
end
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ local ZOOZ_ZEN_30_DIMMER_RELAY_FINGERPRINTS = {
local function can_handle_zooz_zen_30_dimmer_relay_double_switch(opts, driver, device, ...)
for _, fingerprint in ipairs(ZOOZ_ZEN_30_DIMMER_RELAY_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
return true
local subdriver = require("zooz-zen-30-dimmer-relay")
return true, subdriver
end
end
return false
Expand Down
Loading

0 comments on commit 48e57dc

Please sign in to comment.