Skip to content

Commit 02f3c17

Browse files
author
=
committed
add timers driver
1 parent 98aeeaa commit 02f3c17

12 files changed

Lines changed: 1697 additions & 0 deletions

File tree

timers/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: "timers"
2+
packageKey: "timers"
3+
permissions:
4+
lan: {}
5+
discovery: {}

timers/profiles/timer-profile.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: timer.v1
2+
components:
3+
- id: main
4+
capabilities:
5+
- id: switch
6+
version: 1
7+
- id: expected
8+
capabilities:
9+
- id: atmosphericPressureMeasurement
10+
version: 1
11+
- id: actual
12+
capabilities:
13+
- id: atmosphericPressureMeasurement
14+
version: 1
15+
16+
preferences:
17+
- preferenceType: number
18+
name: timeout
19+
title: Seconds
20+
required: true
21+
description: Number of seconds this timer should wait
22+
definition:
23+
min: 0.5
24+
max: 600
25+
default: 1
26+
- preferenceType: boolean
27+
name: interval
28+
title: Interval
29+
description: If the timer should repeat once started
30+
required: true
31+
definition:
32+
default: false
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: timer_bridge.v1
2+
components:
3+
- id: main
4+
capabilities:
5+
- id: honestadmin11679.targetcreate
6+
version: 1
7+
- id: honestadmin11679.targetCount
8+
version: 1
9+
- id: honestadmin11679.currentUrl
10+
version: 1

timers/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Timers

timers/src/disco.lua

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

timers/src/driver_name.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return "timers"

0 commit comments

Comments
 (0)