From 2465a354ee96db248c7a6c8cf911f8d13628f322 Mon Sep 17 00:00:00 2001 From: callemand Date: Fri, 13 Oct 2023 13:16:58 +0200 Subject: [PATCH 1/3] Add 'gladys start' scene trigger --- front/src/config/i18n/en.json | 3 ++ front/src/config/i18n/fr.json | 3 ++ .../routes/scene/edit-scene/TriggerCard.jsx | 9 +++++ .../triggers/ChooseTriggerTypeCard.jsx | 3 +- .../triggers/GladysStartTrigger.jsx | 16 +++++++++ server/lib/index.js | 5 +++ server/lib/scene/scene.triggers.js | 1 + .../test/lib/scene/scene.checkTrigger.test.js | 35 +++++++++++++++++++ server/utils/constants.js | 1 + 9 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 front/src/routes/scene/edit-scene/triggers/GladysStartTrigger.jsx diff --git a/front/src/config/i18n/en.json b/front/src/config/i18n/en.json index 0d3d91c4d5..f21d8a1967 100644 --- a/front/src/config/i18n/en.json +++ b/front/src/config/i18n/en.json @@ -1572,6 +1572,9 @@ }, "calendar": { "event-is-coming": "Calendar event is coming" + }, + "system": { + "start": "Gladys starting" } }, "triggersCard": { diff --git a/front/src/config/i18n/fr.json b/front/src/config/i18n/fr.json index 3aa1e4f29d..35aaea1ca3 100644 --- a/front/src/config/i18n/fr.json +++ b/front/src/config/i18n/fr.json @@ -1574,6 +1574,9 @@ }, "calendar": { "event-is-coming": "Un évènement dans le calendrier arrive" + }, + "system": { + "start": "Gladys démarre" } }, "triggersCard": { diff --git a/front/src/routes/scene/edit-scene/TriggerCard.jsx b/front/src/routes/scene/edit-scene/TriggerCard.jsx index 32d3384543..3c43c1eba1 100644 --- a/front/src/routes/scene/edit-scene/TriggerCard.jsx +++ b/front/src/routes/scene/edit-scene/TriggerCard.jsx @@ -12,6 +12,7 @@ import UserEnteredOrLeftArea from './triggers/UserEnteredOrLeftArea'; import CalendarEventIsComing from './triggers/CalendarEventIsComing'; import { EVENTS } from '../../../../../server/utils/constants'; +import GladysStartTrigger from './triggers/GladysStartTrigger'; const deleteTriggerFromList = (deleteTrigger, index) => () => { deleteTrigger(index); @@ -20,6 +21,7 @@ const deleteTriggerFromList = (deleteTrigger, index) => () => { const TriggerCard = ({ children, ...props }) => (
+ {props.trigger.type === EVENTS.SYSTEM.START && } {props.trigger.type === EVENTS.DEVICE.NEW_STATE && } {props.trigger.type === EVENTS.TIME.CHANGED && } {props.trigger.type === EVENTS.TIME.SUNSET && } @@ -131,6 +133,13 @@ const TriggerCard = ({ children, ...props }) => ( setVariablesTrigger={props.setVariablesTrigger} /> )} + {props.trigger.type === EVENTS.SYSTEM.START && ( + + )}
); diff --git a/front/src/routes/scene/edit-scene/triggers/ChooseTriggerTypeCard.jsx b/front/src/routes/scene/edit-scene/triggers/ChooseTriggerTypeCard.jsx index 07684edae5..4ef4bf8b2c 100644 --- a/front/src/routes/scene/edit-scene/triggers/ChooseTriggerTypeCard.jsx +++ b/front/src/routes/scene/edit-scene/triggers/ChooseTriggerTypeCard.jsx @@ -18,7 +18,8 @@ const TRIGGER_LIST = [ EVENTS.HOUSE.NO_LONGER_EMPTY, EVENTS.AREA.USER_ENTERED, EVENTS.AREA.USER_LEFT, - EVENTS.CALENDAR.EVENT_IS_COMING + EVENTS.CALENDAR.EVENT_IS_COMING, + EVENTS.SYSTEM.START ]; class ChooseTriggerType extends Component { diff --git a/front/src/routes/scene/edit-scene/triggers/GladysStartTrigger.jsx b/front/src/routes/scene/edit-scene/triggers/GladysStartTrigger.jsx new file mode 100644 index 0000000000..25000f5c71 --- /dev/null +++ b/front/src/routes/scene/edit-scene/triggers/GladysStartTrigger.jsx @@ -0,0 +1,16 @@ +import { Component } from 'preact'; +import { connect } from 'unistore/preact'; + +class SunriseSunsetTrigger extends Component { + render({}, {}) { + return ( +
+
+
COUCOU //TODO
+
+
+ ); + } +} + +export default connect('', {})(SunriseSunsetTrigger); diff --git a/server/lib/index.js b/server/lib/index.js index dd83c1cbfe..d66017741b 100644 --- a/server/lib/index.js +++ b/server/lib/index.js @@ -25,6 +25,7 @@ const System = require('./system'); const Variable = require('./variable'); const services = require('../services'); const Weather = require('./weather'); +const { EVENTS } = require('../utils/constants'); /** * @description Start a new Gladys instance. @@ -163,6 +164,10 @@ function Gladys(params = {}) { scheduler.init(); } gateway.init(); + + event.emit(EVENTS.TRIGGERS.CHECK, { + type: EVENTS.SYSTEM.START, + }); }, }; diff --git a/server/lib/scene/scene.triggers.js b/server/lib/scene/scene.triggers.js index ed8134ef9d..b8b5ea91e8 100644 --- a/server/lib/scene/scene.triggers.js +++ b/server/lib/scene/scene.triggers.js @@ -25,6 +25,7 @@ const triggersFunc = { [EVENTS.HOUSE.NO_LONGER_EMPTY]: (event, trigger) => event.house === trigger.house, [EVENTS.AREA.USER_ENTERED]: (event, trigger) => event.user === trigger.user && event.area === trigger.area, [EVENTS.AREA.USER_LEFT]: (event, trigger) => event.user === trigger.user && event.area === trigger.area, + [EVENTS.SYSTEM.START]: () => true, }; module.exports = { diff --git a/server/test/lib/scene/scene.checkTrigger.test.js b/server/test/lib/scene/scene.checkTrigger.test.js index 458586be1e..f0c84025c3 100644 --- a/server/test/lib/scene/scene.checkTrigger.test.js +++ b/server/test/lib/scene/scene.checkTrigger.test.js @@ -446,6 +446,41 @@ describe('scene.checkTrigger', () => { }); }); + it('should execute scene with system start trigger', async () => { + sceneManager.addScene({ + selector: 'my-scene', + active: true, + actions: [ + [ + { + type: ACTIONS.LIGHT.TURN_OFF, + devices: ['light-1'], + }, + ], + ], + triggers: [ + { + type: EVENTS.SYSTEM.START, + area: 'area-1', + user: 'tony', + }, + ], + }); + sceneManager.checkTrigger({ + type: EVENTS.SYSTEM.START, + }); + return new Promise((resolve, reject) => { + sceneManager.queue.start(() => { + try { + assert.calledOnce(device.setValue); + resolve(); + } catch (e) { + reject(e); + } + }); + }); + }); + it('should not execute scene, condition not verified', async () => { sceneManager.addScene({ selector: 'my-scene', diff --git a/server/utils/constants.js b/server/utils/constants.js index 0775f33bc3..4cfef7dbb7 100644 --- a/server/utils/constants.js +++ b/server/utils/constants.js @@ -202,6 +202,7 @@ const EVENTS = { CHECK_UPGRADE: 'system.check-upgrade', TIMEZONE_CHANGED: 'system.timezone-changed', VACUUM: 'system.vacuum', + START: 'system.start', }, WEBSOCKET: { SEND: 'websocket.send', From e9263bb47414469ee4b615daefda09888e0bd019 Mon Sep 17 00:00:00 2001 From: callemand Date: Thu, 19 Oct 2023 09:26:26 +0200 Subject: [PATCH 2/3] Fix return review --- front/src/config/i18n/en.json | 3 +++ front/src/config/i18n/fr.json | 3 +++ .../scene/edit-scene/triggers/GladysStartTrigger.jsx | 9 ++++++--- server/test/lib/scene/scene.checkTrigger.test.js | 2 -- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/front/src/config/i18n/en.json b/front/src/config/i18n/en.json index f21d8a1967..1a1115b968 100644 --- a/front/src/config/i18n/en.json +++ b/front/src/config/i18n/en.json @@ -1659,6 +1659,9 @@ "minute": "minutes", "hour": "hours", "day": "days" + }, + "gladysStart": { + "description": "This scene will be executed each time Gladys starts" } } }, diff --git a/front/src/config/i18n/fr.json b/front/src/config/i18n/fr.json index 35aaea1ca3..428cf65a5a 100644 --- a/front/src/config/i18n/fr.json +++ b/front/src/config/i18n/fr.json @@ -1661,6 +1661,9 @@ "minute": "minutes", "hour": "heures", "day": "jours" + }, + "gladysStart": { + "description": "Cette scène sera executé a chaque démarage de Gladys" } } }, diff --git a/front/src/routes/scene/edit-scene/triggers/GladysStartTrigger.jsx b/front/src/routes/scene/edit-scene/triggers/GladysStartTrigger.jsx index 25000f5c71..10068a3530 100644 --- a/front/src/routes/scene/edit-scene/triggers/GladysStartTrigger.jsx +++ b/front/src/routes/scene/edit-scene/triggers/GladysStartTrigger.jsx @@ -1,16 +1,19 @@ import { Component } from 'preact'; import { connect } from 'unistore/preact'; +import { Text } from 'preact-i18n'; -class SunriseSunsetTrigger extends Component { +class GladysStartTrigger extends Component { render({}, {}) { return (
-
COUCOU //TODO
+
+ +
); } } -export default connect('', {})(SunriseSunsetTrigger); +export default connect('', {})(GladysStartTrigger); diff --git a/server/test/lib/scene/scene.checkTrigger.test.js b/server/test/lib/scene/scene.checkTrigger.test.js index f0c84025c3..63ec03f99c 100644 --- a/server/test/lib/scene/scene.checkTrigger.test.js +++ b/server/test/lib/scene/scene.checkTrigger.test.js @@ -461,8 +461,6 @@ describe('scene.checkTrigger', () => { triggers: [ { type: EVENTS.SYSTEM.START, - area: 'area-1', - user: 'tony', }, ], }); From d6bec4891d2f18fb8090bb6814a49e175812406e Mon Sep 17 00:00:00 2001 From: callemand Date: Mon, 23 Oct 2023 08:23:41 +0200 Subject: [PATCH 3/3] Fix typo --- front/src/config/i18n/fr.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/src/config/i18n/fr.json b/front/src/config/i18n/fr.json index 428cf65a5a..e2a5b3316c 100644 --- a/front/src/config/i18n/fr.json +++ b/front/src/config/i18n/fr.json @@ -1663,7 +1663,7 @@ "day": "jours" }, "gladysStart": { - "description": "Cette scène sera executé a chaque démarage de Gladys" + "description": "Cette scène sera exécutée à chaque démarrage de Gladys" } } },