Skip to content

Commit

Permalink
Merge branch 'master' into sonoff-missing-devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Mar 9, 2020
2 parents 7270ffd + ebadd61 commit a8ddf8c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
3 changes: 0 additions & 3 deletions .codecov.yml
@@ -1,9 +1,6 @@
coverage:
status:
project:
front:
target: 90%
flags: front
server:
target: 90%
flags: server
Expand Down
3 changes: 3 additions & 0 deletions server/lib/scene/scene.addScene.js
@@ -1,5 +1,6 @@
const schedule = require('node-schedule');
const cloneDeep = require('lodash.clonedeep');
const uuid = require('uuid');

const { BadParameters } = require('../../utils/coreErrors');
const { EVENTS } = require('../../utils/constants');
Expand Down Expand Up @@ -33,6 +34,8 @@ function addScene(sceneRaw) {
// Foreach triggger, we schedule jobs for triggers that need to be scheduled
if (scene.triggers) {
scene.triggers.forEach((trigger) => {
// First, we had a trigger key, import to uniquely identify this trigger
trigger.key = uuid.v4();
if (trigger.type === EVENTS.TIME.CHANGED && trigger.scheduler_type !== 'interval') {
const rule = new schedule.RecurrenceRule();
switch (trigger.scheduler_type) {
Expand Down
2 changes: 1 addition & 1 deletion server/lib/scene/scene.triggers.js
Expand Up @@ -10,7 +10,7 @@ const triggersFunc = {
// we compare the value with the expected value
return compare(trigger.operator, event.last_value, trigger.value);
},
[EVENTS.TIME.CHANGED]: (event, trigger) => true,
[EVENTS.TIME.CHANGED]: (event, trigger) => event.key === trigger.key,
};

module.exports = {
Expand Down
8 changes: 4 additions & 4 deletions server/lib/state/Store.js
@@ -1,5 +1,5 @@
const Store = function Store() {
this.state = {};
this.state = null;
};

/**
Expand All @@ -13,10 +13,10 @@ const Store = function Store() {
function setState(update) {
if (typeof update === 'string') {
this.state = update;
} else if (this.state === null) {
this.state = update;
} else {
Object.keys(update).forEach((key) => {
this.state[key] = update[key];
});
Object.assign(this.state, update);
}
}
/**
Expand Down
89 changes: 88 additions & 1 deletion server/test/lib/scene/scene.checkTrigger.test.js
Expand Up @@ -56,7 +56,7 @@ describe('scene.checkTrigger', () => {
setValue: fake.resolves(null),
};
const sceneManager = new SceneManager(stateManager, event, device);
sceneManager.addScene({
const addedScene = sceneManager.addScene({
selector: 'my-scene',
actions: [
[
Expand All @@ -77,6 +77,7 @@ describe('scene.checkTrigger', () => {
});
sceneManager.checkTrigger({
type: EVENTS.TIME.CHANGED,
key: addedScene.triggers[0].key,
});
return new Promise((resolve, reject) => {
sceneManager.queue.start(() => {
Expand Down Expand Up @@ -185,4 +186,90 @@ describe('scene.checkTrigger', () => {
});
}).to.throw(Error, 'Trigger type "one-unknown-event" has no checker function.');
});
it('should execute scene, event & key matching', async () => {
const stateManager = new StateManager();
const device = {
setValue: fake.resolves(null),
};
const sceneManager = new SceneManager(stateManager, event, device);
const addedScene = sceneManager.addScene({
selector: 'my-scene',
actions: [
[
{
type: ACTIONS.LIGHT.TURN_ON,
devices: ['light-1'],
},
],
],
triggers: [
{
type: EVENTS.TIME.CHANGED,
scheduler_type: 'interval',
interval: 10,
unit: 'hour',
},
],
});
sceneManager.checkTrigger({
type: EVENTS.TIME.CHANGED,
key: addedScene.triggers[0].key,
scheduler_type: 'interval',
interval: 10,
unit: 'hour',
});
return new Promise((resolve, reject) => {
sceneManager.queue.start(() => {
try {
assert.calledOnce(device.setValue);
resolve();
} catch (e) {
reject(e);
}
});
});
});
it('should not execute scene, key not matching', async () => {
const stateManager = new StateManager();
const device = {
setValue: fake.resolves(null),
};
const sceneManager = new SceneManager(stateManager, event, device);
sceneManager.addScene({
selector: 'my-scene',
actions: [
[
{
type: ACTIONS.LIGHT.TURN_ON,
devices: ['light-1'],
},
],
],
triggers: [
{
type: EVENTS.TIME.CHANGED,
scheduler_type: 'interval',
interval: 10,
unit: 'hour',
},
],
});
sceneManager.checkTrigger({
type: EVENTS.TIME.CHANGED,
key: 'not-the-same-key',
scheduler_type: 'interval',
interval: 10,
unit: 'hour',
});
return new Promise((resolve, reject) => {
sceneManager.queue.start(() => {
try {
assert.notCalled(device.setValue);
resolve();
} catch (e) {
reject(e);
}
});
});
});
});

0 comments on commit a8ddf8c

Please sign in to comment.