aboutsummaryrefslogtreecommitdiff
path: root/src/lib/helpers/action.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/helpers/action.ts
parent3576cb2992b8b637e5325a0fa36ee1954e0bef84 (diff)
[actions] added motion sensor supportHEADmain
Diffstat (limited to 'src/lib/helpers/action.ts')
-rw-r--r--src/lib/helpers/action.ts70
1 files changed, 63 insertions, 7 deletions
diff --git a/src/lib/helpers/action.ts b/src/lib/helpers/action.ts
index 59677ea..87364ba 100644
--- a/src/lib/helpers/action.ts
+++ b/src/lib/helpers/action.ts
@@ -7,14 +7,10 @@ import { sendMessage } from "@lib/mqtt.ts"
import allActions from "@lib/config/actions.ts"
import allDevices from "@lib/config/devices.ts"
-// run on button press
-export function onPress(action) {
-
- console.debug(`-> running ${action.id}`)
+// run actions
+function _runActions(actions) {
- const actionDo = action.do
-
- actionDo.forEach((action) => {
+ actions.forEach((action) => {
const device = allDevices.find((device) => device.id === action.device)
@@ -29,6 +25,66 @@ export function onPress(action) {
})
}
+// run on button press
+export function onPress(action) {
+
+ console.debug(`-> running ${action.id}`)
+
+ _runActions(action.do)
+}
+
+// run on motion
+export function onMotion(action, payload) {
+
+ if (payload.presence === true) {
+
+ console.debug(`-> running ${action.id}`)
+
+ if (!global.activeActions.includes(action.id)) {
+ global.activeActions.push(action.id)
+ }
+
+ if (global.activeUndos.includes(action.id)) {
+ global.activeUndos = global.activeUndos.filter((trigger) => trigger !== action.id)
+ }
+
+ _runActions(action.do)
+
+ } else {
+
+ if (global.activeActions.includes(action.id)) {
+
+ console.debug(`-> undoing ${action.id}`)
+
+ const inverseActions = action.do.map((inverseAction) => {
+ let actionDo
+
+ switch (inverseAction.action) {
+ case "on":
+ actionDo = "off"
+ break
+
+ case "off":
+ actionDo = "on"
+ break
+
+ default:
+ actionDo = inverseAction.action
+ }
+
+ return {
+ ...inverseAction,
+ action: actionDo
+ }
+ })
+
+ global.activeActions = global.activeActions.filter((trigger) => trigger !== action.id)
+
+ _runActions(inverseActions)
+ }
+ }
+}
+
// validate actions config
export function validate() {