Merge pull request #115 from thomasloven/publish-availability
Publish availability of devices
This commit is contained in:
commit
dba9c98097
2 changed files with 22 additions and 0 deletions
|
|
@ -15,6 +15,7 @@ const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`;
|
||||||
const getPath = ({ id, type }) => `${discoveryPrefix}/${type}/${nodeId}/${id}`;
|
const getPath = ({ id, type }) => `${discoveryPrefix}/${type}/${nodeId}/${id}`;
|
||||||
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 getAvailabilityTopic = plug => `${getPath(plug)}/availability`;
|
||||||
const getCommandTopic = (plug) => `${getPath(plug)}/set`;
|
const getCommandTopic = (plug) => `${getPath(plug)}/set`;
|
||||||
const getSceneEventTopic = () => 'plejd/event/scene';
|
const getSceneEventTopic = () => 'plejd/event/scene';
|
||||||
|
|
||||||
|
|
@ -24,6 +25,7 @@ const getDiscoveryPayload = (device) => ({
|
||||||
unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`,
|
unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`,
|
||||||
state_topic: getStateTopic(device),
|
state_topic: getStateTopic(device),
|
||||||
command_topic: getCommandTopic(device),
|
command_topic: getCommandTopic(device),
|
||||||
|
availability_topic: getAvailabilityTopic(device),
|
||||||
optimistic: false,
|
optimistic: false,
|
||||||
brightness: `${device.dimmable}`,
|
brightness: `${device.dimmable}`,
|
||||||
device: {
|
device: {
|
||||||
|
|
@ -125,6 +127,16 @@ class MqttClient extends EventEmitter {
|
||||||
this.client.reconnect();
|
this.client.reconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
disconnect(callback) {
|
||||||
|
this.devices.forEach((device) => {
|
||||||
|
this.client.publish(
|
||||||
|
getAvailabilityTopic(device),
|
||||||
|
"offline"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
this.client.end(callback);
|
||||||
|
}
|
||||||
|
|
||||||
discover(devices) {
|
discover(devices) {
|
||||||
this.devices = devices;
|
this.devices = devices;
|
||||||
|
|
||||||
|
|
@ -142,6 +154,9 @@ class MqttClient extends EventEmitter {
|
||||||
self.deviceMap[device.id] = payload.unique_id;
|
self.deviceMap[device.id] = payload.unique_id;
|
||||||
|
|
||||||
self.client.publish(getConfigPath(device), JSON.stringify(payload));
|
self.client.publish(getConfigPath(device), JSON.stringify(payload));
|
||||||
|
setTimeout(() => {
|
||||||
|
self.client.publish(getAvailabilityTopic(device), "online");
|
||||||
|
}, 2000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,6 +193,7 @@ class MqttClient extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.client.publish(getStateTopic(device), payload);
|
this.client.publish(getStateTopic(device), payload);
|
||||||
|
this.client.publish(getAvailabilityTopic(device), "online");
|
||||||
}
|
}
|
||||||
|
|
||||||
sceneTriggered(scene) {
|
sceneTriggered(scene) {
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,12 @@ async function main() {
|
||||||
);
|
);
|
||||||
const client = new MqttClient(config.mqttBroker, config.mqttUsername, config.mqttPassword);
|
const client = new MqttClient(config.mqttBroker, config.mqttUsername, config.mqttPassword);
|
||||||
|
|
||||||
|
['SIGINT', 'SIGHUP', 'SIGTERM'].forEach(signal => {
|
||||||
|
process.on(signal, () => {
|
||||||
|
client.disconnect(() => process.exit(0));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
plejdApi.login().then(() => {
|
plejdApi.login().then(() => {
|
||||||
// load all sites and find the one that we want (from config)
|
// load all sites and find the one that we want (from config)
|
||||||
plejdApi.getSites().then((site) => {
|
plejdApi.getSites().then((site) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue