From 29219e72b3757bae95f30979ace9f1614f8a3609 Mon Sep 17 00:00:00 2001 From: Zoe Codez Date: Sun, 2 Jun 2024 09:42:48 -0500 Subject: [PATCH 1/2] better entity utilization --- src/extensions/room.extension.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/extensions/room.extension.ts b/src/extensions/room.extension.ts index ad9294b..814dff8 100644 --- a/src/extensions/room.extension.ts +++ b/src/extensions/room.extension.ts @@ -1,6 +1,7 @@ import { CronExpression, eachSeries, + FIRST, InternalError, is, TServiceParams, @@ -65,19 +66,24 @@ export function Room({ const SCENE_LIST = Object.keys(scenes) as SCENES[]; const sensorName = `${name} current scene`; - const currentScene = synapse.sensor({ + + const currentScene = synapse.select({ context, + current_option: SCENE_LIST[FIRST], + managed: false, name: sensorName, + options: SCENE_LIST, + select_option: async ({ option }) => await setScene(option as SCENES), }); - const sensor = currentScene.getEntity() as ByIdProxy>; scheduler.cron({ exec: async () => { + const current = currentScene.storage.get("current_option") as SCENES; await automation.aggressive.validateRoomScene({ context, - name: sensor.state, + name: current, room: name, - scene: scenes[sensor.state as SCENES], + scene: scenes[current], }); }, schedule: CronExpression.EVERY_30_SECONDS, @@ -99,8 +105,9 @@ export function Room({ if (!is.empty(target) && target !== "on") { return false; } - const current = (scenes[currentScene.storage.get("state") as SCENES] ?? - {}) as RoomScene; + const current = (scenes[ + currentScene.storage.get("current_option") as SCENES + ] ?? {}) as RoomScene; const definition = current.definition; if (entity_id in definition) { const state = definition[entity_id] as SceneLightState; @@ -190,7 +197,7 @@ export function Room({ ); } logger.info({ name }, `set scene {%s}`, sceneName); - currentScene.storage.set("state", sceneName); + currentScene.storage.set("current_option", sceneName); await sceneApply(sceneName); } @@ -209,7 +216,7 @@ export function Room({ const out = new Proxy({} as RoomDefinition, { get: (_, property: keyof RoomDefinition) => { if (property === "scene") { - return currentScene.storage.get("state"); + return currentScene.storage.get("current_option"); } if (property === "sceneId") { return (scene: SCENES) => { @@ -223,7 +230,7 @@ export function Room({ return currentScene.getEntity(); } if (property === "currentSceneDefinition") { - return scenes[currentScene.storage.get("state") as SCENES]; + return scenes[currentScene.storage.get("current_option") as SCENES]; } return undefined; }, From de40a091f1cb179fb9b01435a13e39b533c0f65e Mon Sep 17 00:00:00 2001 From: Zoe Codez Date: Sun, 2 Jun 2024 10:37:43 -0500 Subject: [PATCH 2/2] bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 99953a4..4d007b4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@digital-alchemy/automation", "repository": "https://github.com/Digital-Alchemy-TS/automation", "homepage": "https://docs.digital-alchemy.app/Automation", - "version": "0.3.8", + "version": "0.3.9", "scripts": { "build": "rm -rf dist/; tsc", "lint": "eslint src",