From cc092aa0f4f07b7bc19ba028f5976e52da9ae3f3 Mon Sep 17 00:00:00 2001 From: Autumn Date: Tue, 12 May 2026 19:28:23 +0100 Subject: [actions] added basic button-press action handling --- src/lib/init.ts | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/lib/init.ts (limited to 'src/lib/init.ts') diff --git a/src/lib/init.ts b/src/lib/init.ts new file mode 100644 index 0000000..c4ba0ea --- /dev/null +++ b/src/lib/init.ts @@ -0,0 +1,58 @@ +// +// ~~~ homecontrol init +// + +// imports +import { validate, onPress } 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" + +// initialise actions +function _initActions() { + + if (!allActions || allActions.length < 1) return + + allActions.forEach((action) => { + + console.debug(`-> setting up ${action.id}`) + + const onDevice = allDevices.find((device) => device.id === action.on.device) + + if (!onDevice) { + console.error(`!> unable to find ${action.on.device}`) + return + } + + const topic = `${onDevice.mqtt}/action` + const callback = (message) => { + + const messageAction = message.toString() + + switch (messageAction) { + + case "toggle": + if (action.on.action === "press") { // todo: use enum + onPress(action) + } + + } + } + + addListener(topic, callback) + }) +} + +// initialise homecontrol +export default function init() { + + console.debug("=> initialising homecontrol") + + const isActionsValid = validate() + + if (!isActionsValid) { + console.error("!> invalid actions config") + } else { + _initActions() + } +} -- cgit v1.3