Skip to content

Commit

Permalink
Add MQTT topic to start scene (#1911)
Browse files Browse the repository at this point in the history
  • Loading branch information
callemand authored Oct 16, 2023
1 parent c87e90f commit 33b2264
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions server/lib/scene/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ const SceneManager = function SceneManager(
this.event.on(EVENTS.HOUSE.UPDATED, eventFunctionWrapper(this.dailyUpdate.bind(this)));
this.event.on(EVENTS.HOUSE.DELETED, eventFunctionWrapper(this.dailyUpdate.bind(this)));
this.event.on(EVENTS.CALENDAR.CHECK_IF_EVENT_IS_COMING, eventFunctionWrapper(this.checkCalendarTriggers.bind(this)));

this.event.on(INTENTS.SCENE.START, this.command.bind(this));

this.event.on(EVENTS.SCENE.TRIGGERED, eventFunctionWrapper(this.execute.bind(this)));
};

SceneManager.prototype.addScene = addScene;
Expand Down
6 changes: 5 additions & 1 deletion server/services/mqtt/lib/handler/handleGladysMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const logger = require('../../../../utils/logger');
* @param {string} topic - MQTT topic.
* @param {object} message - The message sent.
* @example
* handleGladysMessage('gladys/device/create', '{ message: "content" }');
* handleGladysMessage('gladys/master/device/create', '{ message: "content" }');
*/
function handleGladysMessage(topic, message) {
const parsedTopic = topic.split('/');
Expand All @@ -34,6 +34,10 @@ function handleGladysMessage(topic, message) {
};
this.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, event);
}
} else if (topic.startsWith('gladys/master/scene/')) {
if (topic.endsWith('/start')) {
this.gladys.event.emit(EVENTS.SCENE.TRIGGERED, parsedTopic[3]);
}
} else {
logger.warn(`MQTT : Gladys topic ${topic} not handled.`);
}
Expand Down
6 changes: 6 additions & 0 deletions server/test/services/mqtt/lib/handleNewMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ describe('Mqtt handle message', () => {
assert.notCalled(gladys.event.emit);
});

it('should execute scene', () => {
mqttHandler.handleNewMessage('gladys/master/scene/test-scene/start', '');

assert.calledWith(gladys.event.emit, EVENTS.SCENE.TRIGGERED, 'test-scene');
});

it('handle strict topic', () => {
mqttHandler.handleNewMessage('gladys/master/random-topic', '{}');

Expand Down

0 comments on commit 33b2264

Please sign in to comment.