Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions timers/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: "timers"
packageKey: "timers"
permissions:
lan: {}
discovery: {}
32 changes: 32 additions & 0 deletions timers/profiles/timer-profile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: timer.v1
components:
- id: main
capabilities:
- id: switch
version: 1
- id: expected
capabilities:
- id: atmosphericPressureMeasurement
version: 1
- id: actual
capabilities:
- id: atmosphericPressureMeasurement
version: 1

preferences:
- preferenceType: number
name: timeout
title: Seconds
required: true
description: Number of seconds this timer should wait
definition:
min: 0.5
max: 600
default: 1
- preferenceType: boolean
name: interval
title: Interval
description: If the timer should repeat once started
required: true
definition:
default: false
10 changes: 10 additions & 0 deletions timers/profiles/timer_bridge-profile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: timer_bridge.v1
components:
- id: main
capabilities:
- id: honestadmin11679.targetcreate
version: 1
- id: honestadmin11679.targetCount
version: 1
- id: honestadmin11679.currentUrl
version: 1
1 change: 1 addition & 0 deletions timers/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Timers
89 changes: 89 additions & 0 deletions timers/src/disco.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
local json = require "dkjson"
local log = require "log"
local utils = require "st.utils"

--- Add a new device to this driver
---
---@param driver Driver The driver instance to use
---@param device_number number|nil If populated this will be used to generate the device name/label if not, `get_device_list`
--- will be called to provide this value
local function add_timer_device(driver, device_number, parent_device_id)
log.trace("add_timer_device")
if device_number == nil then
log.debug("determining current device count")
local device_list = driver.device_api.get_device_list()
device_number = #device_list
end
local device_name = "Timer " .. device_number
log.debug("adding device " .. device_name)
local device_id = utils.generate_uuid_v4() .. tostring(device_number)
local device_info = {
type = "LAN",
deviceNetworkId = device_id,
label = device_name,
parent_device_id = parent_device_id,
profileReference = "timer.v1",
vendorProvidedName = device_name,
}
local device_info_json = json.encode(device_info)
local success, msg = driver.device_api.create_device(device_info_json)
if success then
log.debug("successfully created device")
return device_name, device_id
end
log.error(string.format("unsuccessful create_device (sensor) %s", msg))
return nil, nil, msg
end

local function add_bridge_device(driver)
log.trace("add_bridge_device")
local device_id = utils.generate_uuid_v4()
local device_name = "Timer Bridge"
local device_info = {
type = "LAN",
deviceNetworkId = device_id,
label = device_name,
profileReference = "timer_bridge.v1",
vendorProvidedName = device_name,
}
local device_info_json = json.encode(device_info)
local success, msg = driver.device_api.create_device(device_info_json)
if success then
log.debug("successfully created device")
return device_name, device_id
end
log.error(string.format("unsuccessful create_device (bridge) %s", msg))
return nil, nil, msg
end

--- A discovery pass that will discover exactly 1 device
--- for a driver. I any devices are already associated with
--- this driver, no devices will be discovered
---
---@param driver Driver the driver name to use when discovering a device
---@param opts table the discovery options
---@param cont function function to check if discovery should continue
local function disco_handler(driver, opts, cont)
log.trace("disco")

if cont() then
local device_list = driver:get_devices()
log.trace("starting discovery")
for _idx, device in ipairs(device_list or {}) do
if device:supports_capability_by_id("honestadmin11679.targetcreate") then
return
end
end
local device_name, device_id, err = add_bridge_device(driver)
if err ~= nil then
log.error(err)
return
end
log.info("added new device " .. device_name)
end
end

return {
disco_handler = disco_handler,
add_timer_device = add_timer_device,
}
1 change: 1 addition & 0 deletions timers/src/driver_name.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return "timers"
Loading