From 89a907d3788a73c1cb2bde7c3b3a2de206043345 Mon Sep 17 00:00:00 2001 From: HRS Date: Sun, 31 May 2026 17:53:24 +0200 Subject: [PATCH 1/4] Support new automation scripts --- src/components/automation/MonacoEditor.vue | 43 ++++++++++++---------- src/stores/automation.ts | 6 +-- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/components/automation/MonacoEditor.vue b/src/components/automation/MonacoEditor.vue index 773f1e0..5407e42 100644 --- a/src/components/automation/MonacoEditor.vue +++ b/src/components/automation/MonacoEditor.vue @@ -58,7 +58,9 @@ function editorWillMount(monacoInstance: typeof monaco): void { type DeviceAttributeModifier = 'ro' | 'rw' | 'wo'; type DeviceAttributeType = 'bool' | 'list' | 'str' | 'float' | 'int' | 'range'; type DeviceAttributeValue = string | number | boolean | undefined; -interface DeviceAttribute +declare type DeviceEventType = 'deviceConnected' | 'deviceDisconnected' | 'deviceRefreshed'; + +declare interface DeviceAttribute { name: string; label?: string; @@ -66,27 +68,30 @@ interface DeviceAttribute modifier: DeviceAttributeModifier; value: DeviceAttributeValue; } -interface Device -{ - getDeviceId: string - getAttribute(attrName: string): Promise - setAttribute(attrName: string, value: DeviceAttributeValue): Promise + +declare interface Device { + readonly getDeviceId: string; + readonly getDeviceName: string; + getAttribute(name: string): Promise; + setAttribute(name: string, value: DeviceAttributeValue): Promise; } -enum DeviceEventType { - deviceUpdateReceived = "deviceUpdateReceived", - deviceConnected = "deviceConnected", - deviceDisconnected = "deviceDisconnected", - deviceRefreshed = "deviceRefreshed", + +declare interface DeviceEvent { + readonly type: DeviceEventType; + readonly device: Device; } -type DeviceEvent = { type: DeviceEventType, device: Device } -interface DeviceRepositoryInterface -{ + +declare function onEvent(handler: (event: DeviceEvent) => void | Promise): void; + +declare const devices: { + getById(id: string): Device | null; getAll(): Device[]; - getById(uuid: string): Device|null; -} -declare const event: DeviceEvent; -declare const devices: DeviceRepositoryInterface; -declare const context: { [key: string]: any }; +}; + +declare function getAttribute(deviceId: string, attributeName: string): Promise; +declare function setAttribute(deviceId: string, attributeName: string, value: unknown): Promise; +declare function getDevices(): Array<{ id: string; name: string }>; + declare const console: { log: (...args: any[]) => void; error: (...args: any[]) => void; diff --git a/src/stores/automation.ts b/src/stores/automation.ts index 50378ad..0a4d119 100644 --- a/src/stores/automation.ts +++ b/src/stores/automation.ts @@ -3,7 +3,7 @@ import { ref, computed } from "vue"; import type AutomationScript from "../model/AutomationScript"; import {apiFetch} from "@/utils/apiFetch"; -const defaultCode = `(async () => { +const defaultCode = `onEvent(async (event) => { // Write your automation here. The script will fire on device events: // // - deviceUpdateReceived: A device got updated through the external API @@ -11,9 +11,9 @@ const defaultCode = `(async () => { // - deviceDisconnected: A device was disconnected from SlvCtrl+ // - deviceRefreshed: New data has been pulled from a device // - // You can get information about the event and the device that invoked your script through the global "event" + // You can get information about the event and the device that invoked your script through the "event" // variable. To access other devices you can use the global variable "devices" -> devices.getById(\`{device uuid}\`) -})(); +}); `; export const useAutomationStore = defineStore("automation", () => { From fcf004cab64ab2db2f9ee075d49fef974d5c5b72 Mon Sep 17 00:00:00 2001 From: HRS Date: Sun, 31 May 2026 18:22:32 +0200 Subject: [PATCH 2/4] Fix template --- src/components/automation/MonacoEditor.vue | 2 ++ src/stores/automation.ts | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/components/automation/MonacoEditor.vue b/src/components/automation/MonacoEditor.vue index 5407e42..4988f05 100644 --- a/src/components/automation/MonacoEditor.vue +++ b/src/components/automation/MonacoEditor.vue @@ -82,6 +82,8 @@ declare interface DeviceEvent { } declare function onEvent(handler: (event: DeviceEvent) => void | Promise): void; +declare function onStart(handler: () => void | Promise): void; +declare function onStop(handler: () => void | Promise): void; declare const devices: { getById(id: string): Device | null; diff --git a/src/stores/automation.ts b/src/stores/automation.ts index 0a4d119..21acf6d 100644 --- a/src/stores/automation.ts +++ b/src/stores/automation.ts @@ -3,10 +3,19 @@ import { ref, computed } from "vue"; import type AutomationScript from "../model/AutomationScript"; import {apiFetch} from "@/utils/apiFetch"; -const defaultCode = `onEvent(async (event) => { - // Write your automation here. The script will fire on device events: +const defaultCode = `onStart(async () => { + // This function will fire once when your script starts. + // You can initialize variables or set device attributes here. +}); + +onStop(async () => { + // This function will fire once when your script stops. + // You can clean up any resources or reset device attributes here. +}); + +onEvent(async (event) => { + // This function will be called on any device event. Possible events are: // - // - deviceUpdateReceived: A device got updated through the external API // - deviceConnected: A new device was connected to SlvCtrl+ // - deviceDisconnected: A device was disconnected from SlvCtrl+ // - deviceRefreshed: New data has been pulled from a device From 9441e18718c0ee6fa6d9631446d53a232f6eed32 Mon Sep 17 00:00:00 2001 From: HRS Date: Mon, 1 Jun 2026 06:59:58 +0200 Subject: [PATCH 3/4] Remove outdated type defs --- src/components/automation/MonacoEditor.vue | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/components/automation/MonacoEditor.vue b/src/components/automation/MonacoEditor.vue index 4988f05..dc50c04 100644 --- a/src/components/automation/MonacoEditor.vue +++ b/src/components/automation/MonacoEditor.vue @@ -90,10 +90,6 @@ declare const devices: { getAll(): Device[]; }; -declare function getAttribute(deviceId: string, attributeName: string): Promise; -declare function setAttribute(deviceId: string, attributeName: string, value: unknown): Promise; -declare function getDevices(): Array<{ id: string; name: string }>; - declare const console: { log: (...args: any[]) => void; error: (...args: any[]) => void; From fd90cec0fe781dae1de9f52e2b39f4330c096f14 Mon Sep 17 00:00:00 2001 From: HRS Date: Mon, 1 Jun 2026 07:01:51 +0200 Subject: [PATCH 4/4] Replace backticks --- src/stores/automation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stores/automation.ts b/src/stores/automation.ts index 21acf6d..6501ea9 100644 --- a/src/stores/automation.ts +++ b/src/stores/automation.ts @@ -21,7 +21,7 @@ onEvent(async (event) => { // - deviceRefreshed: New data has been pulled from a device // // You can get information about the event and the device that invoked your script through the "event" - // variable. To access other devices you can use the global variable "devices" -> devices.getById(\`{device uuid}\`) + // variable. To access other devices you can use the global variable "devices" -> devices.getById("{device uuid}") }); `;