Skip to content

Instantly share code, notes, and snippets.

@nayelyzarazua-bluetrail
Created December 7, 2022 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayelyzarazua-bluetrail/22cc4ca711da6704f463f6bfe35177f7 to your computer and use it in GitHub Desktop.
Save nayelyzarazua-bluetrail/22cc4ca711da6704f463f6bfe35177f7 to your computer and use it in GitHub Desktop.
createChildDevice_afterMain
local log = require "log"
local capabilities = require "st.capabilities"
local utils= require "st.utils"
local command_handlers = {}
-- callback to handle an `on` capability command
function command_handlers.switch_on(driver, device, command)
log.debug(string.format("[%s] calling set_power(on)", device.device_network_id))
device:emit_event(capabilities.switch.switch.on())
local metadata = {
type = "EDGE_CHILD",
label = "Child Device",
profile = "childDevice",
manufacturer = "SmartThings",
parent_device_id = device.id,
parent_assigned_child_key = "childDevice1",
vendor_provided_label = nil
}
driver:try_create_device(metadata)
end
-- callback to handle an `off` capability command
function command_handlers.switch_off(driver, device, command)
log.debug(string.format("[%s] calling set_power(off)", device.device_network_id))
device:emit_event(capabilities.switch.switch.off())
end
function command_handlers.switch_level(driver, device, command)
log.debug(string.format("[%s] calling setLevel", device.device_network_id))
device:emit_event(capabilities.switchLevel.level(command.args.level))
local childDev = device:get_child_by_parent_assigned_key("childDevice1")
print(childDev)
end
return command_handlers
-- require st provided libraries
local capabilities = require "st.capabilities"
local Driver = require "st.driver"
local log = require "log"
-- require custom handlers from driver package
local command_handlers = require "command_handlers"
local discovery = require "discovery"
-----------------------------------------------------------------
-- local functions
-----------------------------------------------------------------
-- this is called once a device is added by the cloud and synchronized down to the hub
local function device_added(driver, device)
log.info("[" .. device.id .. "] Adding new device")
-- set a default or queried state for each capability attribute
device:emit_event(capabilities.switch.switch.on())
device:emit_event(capabilities.switchLevel.level(10))
end
-- this is called both when a device is added (but after `added`) and after a hub reboots.
local function device_init(driver, device)
log.info("[" .. device.id .. "] Initializing device")
-- mark device as online so it can be controlled from the app
device:online()
end
-- this is called when a device is removed by the cloud and synchronized down to the hub
local function device_removed(driver, device)
log.info("[" .. device.id .. "] Removing device")
end
-- create the driver object
local childdevone = Driver("childdevone", {
discovery = discovery.handle_discovery,
lifecycle_handlers = {
added = device_added,
init = device_init,
removed = device_removed
},
capability_handlers = {
[capabilities.switch.ID] = {
[capabilities.switch.commands.on.NAME] = command_handlers.switch_on,
[capabilities.switch.commands.off.NAME] = command_handlers.switch_off,
},
[capabilities.switchLevel.ID] = {
[capabilities.switchLevel.commands.setLevel.NAME] = command_handlers.switch_level
}
}
})
-- run the driver
childdevone:run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment