aboutsummaryrefslogtreecommitdiff
path: root/src/lib/init.ts
diff options
context:
space:
mode:
authorAutumn <git@autumnfo.rest>2026-05-16 10:29:27 +0100
committerAutumn <git@autumnfo.rest>2026-05-16 10:29:27 +0100
commit59ab1beb9bc1dc3b2d448560531b88d47fb57613 (patch)
treebfebdd95ee1f0fa7c41f5ad0f636105fa4b43fbc /src/lib/init.ts
parent3576cb2992b8b637e5325a0fa36ee1954e0bef84 (diff)
[actions] added motion sensor supportHEADmain
Diffstat (limited to 'src/lib/init.ts')
-rw-r--r--src/lib/init.ts35
1 files changed, 33 insertions, 2 deletions
diff --git a/src/lib/init.ts b/src/lib/init.ts
index 03c9b63..99730e9 100644
--- a/src/lib/init.ts
+++ b/src/lib/init.ts
@@ -3,7 +3,7 @@
//
// imports
-import { validate, onPress } from "@lib/helpers/action.ts"
+import { validate, onPress, onMotion } from "@lib/helpers/action.ts"
import { addListener } from "@lib/mqtt.ts"
import allActions, { ACTION_ON_ACTION } from "@lib/config/actions.ts"
import allDevices from "@lib/config/devices.ts"
@@ -11,6 +11,9 @@ import allDevices from "@lib/config/devices.ts"
// initialise actions
function _initActions() {
+ global.activeActions = []
+ global.activeUndos = []
+
if (!allActions || allActions.length < 1) return
allActions.forEach((action) => {
@@ -24,7 +27,21 @@ function _initActions() {
return
}
- const topic = `${onDevice.mqtt}/action`
+ // todo: use a single callback / listener & just subscribe to each topic
+ // first param on return is the topic so case / switch that
+
+ let topic
+
+ if (["on", "off", "toggle"].includes(action.on)) {
+
+ topic = `${onDevice.mqtt}/action`
+
+ } else {
+
+ topic = onDevice.mqtt
+
+ }
+
const callback = (message) => {
const messageAction = message.toString()
@@ -52,6 +69,20 @@ function _initActions() {
break
+ default:
+
+ // probbaly a json string
+ const jsonMessageAction = JSON.parse(messageAction)
+
+ if (jsonMessageAction?.presence !== undefined && action.on.action === "motion") {
+
+ onMotion(action, jsonMessageAction)
+
+ } else {
+
+ console.warn(`?> unknown action: ${messageAction} from ${topic}`)
+
+ }
}
}