|
| 1 | +local json = require "dkjson" |
| 2 | +local log = require "log" |
| 3 | +local utils = require "st.utils" |
| 4 | + |
| 5 | +--- Add a new device to this driver |
| 6 | +--- |
| 7 | +---@param driver Driver The driver instance to use |
| 8 | +---@param device_number number|nil If populated this will be used to generate the device name/label if not, `get_device_list` |
| 9 | +--- will be called to provide this value |
| 10 | +local function add_timer_device(driver, device_number, parent_device_id) |
| 11 | + log.trace("add_timer_device") |
| 12 | + if device_number == nil then |
| 13 | + log.debug("determining current device count") |
| 14 | + local device_list = driver.device_api.get_device_list() |
| 15 | + device_number = #device_list |
| 16 | + end |
| 17 | + local device_name = "Timer " .. device_number |
| 18 | + log.debug("adding device " .. device_name) |
| 19 | + local device_id = utils.generate_uuid_v4() .. tostring(device_number) |
| 20 | + local device_info = { |
| 21 | + type = "LAN", |
| 22 | + deviceNetworkId = device_id, |
| 23 | + label = device_name, |
| 24 | + parent_device_id = parent_device_id, |
| 25 | + profileReference = "timer.v1", |
| 26 | + vendorProvidedName = device_name, |
| 27 | + } |
| 28 | + local device_info_json = json.encode(device_info) |
| 29 | + local success, msg = driver.device_api.create_device(device_info_json) |
| 30 | + if success then |
| 31 | + log.debug("successfully created device") |
| 32 | + return device_name, device_id |
| 33 | + end |
| 34 | + log.error(string.format("unsuccessful create_device (sensor) %s", msg)) |
| 35 | + return nil, nil, msg |
| 36 | +end |
| 37 | + |
| 38 | +local function add_bridge_device(driver) |
| 39 | + log.trace("add_bridge_device") |
| 40 | + local device_id = utils.generate_uuid_v4() |
| 41 | + local device_name = "Timer Bridge" |
| 42 | + local device_info = { |
| 43 | + type = "LAN", |
| 44 | + deviceNetworkId = device_id, |
| 45 | + label = device_name, |
| 46 | + profileReference = "timer_bridge.v1", |
| 47 | + vendorProvidedName = device_name, |
| 48 | + } |
| 49 | + local device_info_json = json.encode(device_info) |
| 50 | + local success, msg = driver.device_api.create_device(device_info_json) |
| 51 | + if success then |
| 52 | + log.debug("successfully created device") |
| 53 | + return device_name, device_id |
| 54 | + end |
| 55 | + log.error(string.format("unsuccessful create_device (bridge) %s", msg)) |
| 56 | + return nil, nil, msg |
| 57 | +end |
| 58 | + |
| 59 | +--- A discovery pass that will discover exactly 1 device |
| 60 | +--- for a driver. I any devices are already associated with |
| 61 | +--- this driver, no devices will be discovered |
| 62 | +--- |
| 63 | +---@param driver Driver the driver name to use when discovering a device |
| 64 | +---@param opts table the discovery options |
| 65 | +---@param cont function function to check if discovery should continue |
| 66 | +local function disco_handler(driver, opts, cont) |
| 67 | + log.trace("disco") |
| 68 | + |
| 69 | + if cont() then |
| 70 | + local device_list = driver:get_devices() |
| 71 | + log.trace("starting discovery") |
| 72 | + for _idx, device in ipairs(device_list or {}) do |
| 73 | + if device:supports_capability_by_id("honestadmin11679.targetcreate") then |
| 74 | + return |
| 75 | + end |
| 76 | + end |
| 77 | + local device_name, device_id, err = add_bridge_device(driver) |
| 78 | + if err ~= nil then |
| 79 | + log.error(err) |
| 80 | + return |
| 81 | + end |
| 82 | + log.info("added new device " .. device_name) |
| 83 | + end |
| 84 | +end |
| 85 | + |
| 86 | +return { |
| 87 | + disco_handler = disco_handler, |
| 88 | + add_timer_device = add_timer_device, |
| 89 | +} |
0 commit comments