Skip to content

Commit

Permalink
Scenes: Add "Gladys starts" scene trigger (#1912)
Browse files Browse the repository at this point in the history
  • Loading branch information
callemand authored Oct 23, 2023
1 parent cc4f4d9 commit c4766f5
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 1 deletion.
6 changes: 6 additions & 0 deletions front/src/config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,9 @@
},
"calendar": {
"event-is-coming": "Calendar event is coming"
},
"system": {
"start": "Gladys starting"
}
},
"triggersCard": {
Expand Down Expand Up @@ -1656,6 +1659,9 @@
"minute": "minutes",
"hour": "hours",
"day": "days"
},
"gladysStart": {
"description": "This scene will be executed each time Gladys starts"
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions front/src/config/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,9 @@
},
"calendar": {
"event-is-coming": "Un évènement dans le calendrier arrive"
},
"system": {
"start": "Gladys démarre"
}
},
"triggersCard": {
Expand Down Expand Up @@ -1658,6 +1661,9 @@
"minute": "minutes",
"hour": "heures",
"day": "jours"
},
"gladysStart": {
"description": "Cette scène sera exécutée à chaque démarrage de Gladys"
}
}
},
Expand Down
9 changes: 9 additions & 0 deletions front/src/routes/scene/edit-scene/TriggerCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -20,6 +21,7 @@ const deleteTriggerFromList = (deleteTrigger, index) => () => {
const TriggerCard = ({ children, ...props }) => (
<div class="card">
<div class="card-header">
{props.trigger.type === EVENTS.SYSTEM.START && <i class="fe fe-activity" />}
{props.trigger.type === EVENTS.DEVICE.NEW_STATE && <i class="fe fe-activity" />}
{props.trigger.type === EVENTS.TIME.CHANGED && <i class="fe fe-watch" />}
{props.trigger.type === EVENTS.TIME.SUNSET && <i class="fe fe-sunset" />}
Expand Down Expand Up @@ -131,6 +133,13 @@ const TriggerCard = ({ children, ...props }) => (
setVariablesTrigger={props.setVariablesTrigger}
/>
)}
{props.trigger.type === EVENTS.SYSTEM.START && (
<GladysStartTrigger
updateTriggerProperty={props.updateTriggerProperty}
index={props.index}
trigger={props.trigger}
/>
)}
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions front/src/routes/scene/edit-scene/triggers/GladysStartTrigger.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component } from 'preact';
import { connect } from 'unistore/preact';
import { Text } from 'preact-i18n';

class GladysStartTrigger extends Component {
render({}, {}) {
return (
<div>
<div class="row">
<div class="col-sm-12">
<Text id="editScene.triggersCard.gladysStart.description" />
</div>
</div>
</div>
);
}
}

export default connect('', {})(GladysStartTrigger);
5 changes: 5 additions & 0 deletions server/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -163,6 +164,10 @@ function Gladys(params = {}) {
scheduler.init();
}
gateway.init();

event.emit(EVENTS.TRIGGERS.CHECK, {
type: EVENTS.SYSTEM.START,
});
},
};

Expand Down
1 change: 1 addition & 0 deletions server/lib/scene/scene.triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
33 changes: 33 additions & 0 deletions server/test/lib/scene/scene.checkTrigger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,39 @@ 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,
},
],
});
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',
Expand Down
1 change: 1 addition & 0 deletions server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit c4766f5

Please sign in to comment.