Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rooms take advantage of synapse better #10

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 16 additions & 9 deletions src/extensions/room.extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CronExpression,
eachSeries,
FIRST,
InternalError,
is,
TServiceParams,
Expand Down Expand Up @@ -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<PICK_ENTITY<"sensor">>;

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,
Expand All @@ -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<ROOM>;
const current = (scenes[
currentScene.storage.get("current_option") as SCENES
] ?? {}) as RoomScene<ROOM>;
const definition = current.definition;
if (entity_id in definition) {
const state = definition[entity_id] as SceneLightState;
Expand Down Expand Up @@ -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);
}

Expand All @@ -209,7 +216,7 @@ export function Room({
const out = new Proxy({} as RoomDefinition<SCENES>, {
get: (_, property: keyof RoomDefinition<SCENES>) => {
if (property === "scene") {
return currentScene.storage.get("state");
return currentScene.storage.get("current_option");
}
if (property === "sceneId") {
return (scene: SCENES) => {
Expand All @@ -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;
},
Expand Down
Loading