added scene event to mqtt

This commit is contained in:
icanos 2019-12-22 17:48:16 +00:00
parent ec624c6773
commit 3aa69d0930
2 changed files with 12 additions and 2 deletions

View file

@ -32,6 +32,10 @@ async function main() {
client.updateState(deviceId, command); client.updateState(deviceId, command);
}); });
plejd.on('sceneTriggered', (scene) => {
client.sceneTriggered(scene);
});
// subscribe to changes from HA // subscribe to changes from HA
client.on('stateChanged', (deviceId, command) => { client.on('stateChanged', (deviceId, command) => {
if (command.state === 'ON') { if (command.state === 'ON') {

View file

@ -29,6 +29,7 @@ const getPath = ({ id, type }) =>
const getConfigPath = plug => `${getPath(plug)}/config`; const getConfigPath = plug => `${getPath(plug)}/config`;
const getStateTopic = plug => `${getPath(plug)}/state`; const getStateTopic = plug => `${getPath(plug)}/state`;
const getCommandTopic = plug => `${getPath(plug)}/set`; const getCommandTopic = plug => `${getPath(plug)}/set`;
const getSceneEventTopic = () => `plejd/event/scene`;
const getDiscoveryPayload = device => ({ const getDiscoveryPayload = device => ({
schema: 'json', schema: 'json',
@ -147,13 +148,18 @@ class MqttClient extends EventEmitter {
} }
} }
logger(JSON.stringify(payload));
this.client.publish( this.client.publish(
getStateTopic(device), getStateTopic(device),
JSON.stringify(payload) JSON.stringify(payload)
); );
} }
sceneTriggered(scene) {
this.client.publish(
getSceneEventTopic(),
JSON.stringify({ scene: scene })
);
}
} }
module.exports = { MqttClient }; module.exports = { MqttClient };