From 457b41cb3c12e766517352acefbb757d238ad625 Mon Sep 17 00:00:00 2001 From: Marcus Westin Date: Tue, 10 Dec 2019 22:01:12 +0100 Subject: [PATCH] added support for DIM-02 and to detect dim func --- api.js | 35 +++++++++++++++++++++++++++++------ mqtt.js | 14 ++++++++++++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/api.js b/api.js index d230bf9..03709c6 100644 --- a/api.js +++ b/api.js @@ -90,14 +90,37 @@ class PlejdApi extends EventEmitter { } for (let i = 0; i < this.site.devices.length; i++) { - let device = this.site.devices[i]; - let deviceId = device.deviceId; + const device = this.site.devices[i]; + const deviceId = device.deviceId; - devices.push({ - id: this.site.deviceAddress[deviceId], + const settings = this.site.outputSettings.find(x => x.deviceParseId == device.objectId); + let deviceNum = this.site.deviceAddress[deviceId]; + + if (settings) { + const outputs = this.site.outputAddress[deviceId]; + deviceNum = outputs[settings.output]; + } + + // check if device is dimmable + let dimmable = false; + if (device.hardware) { + dimmable = device.hardware.name == 'DIM-01'; + } + + if (settings) { + dimmable = settings.dimCurve != 'NonDimmable'; + } + + const newDevice = { + id: deviceNum, name: device.title, - type: 'light' - }); + type: 'light', + supportsDim: dimmable + }; + + logger(JSON.stringify(newDevice)); + + devices.push(newDevice); } return devices; diff --git a/mqtt.js b/mqtt.js index 7a54209..fc25de5 100644 --- a/mqtt.js +++ b/mqtt.js @@ -33,7 +33,7 @@ const getBrightnessCommandTopic = plug => `${getPath(plug)}/setBrightness`; const getBrightnessTopic = plug => `${getPath(plug)}/brightness`; const getCommandTopic = plug => `${getPath(plug)}/set`; -const getDiscoveryPayload = device => ({ +const getDiscoveryDimmablePayload = device => ({ name: device.name, unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`, state_topic: getStateTopic(device), @@ -45,6 +45,16 @@ const getDiscoveryPayload = device => ({ optimistic: false }); +const getDiscoveryPayload = device => ({ + name: device.name, + unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`, + state_topic: getStateTopic(device), + command_topic: getCommandTopic(device), + payload_on: 1, + payload_off: 0, + optimistic: false +}); + // #endregion class MqttClient extends EventEmitter { @@ -124,7 +134,7 @@ class MqttClient extends EventEmitter { devices.forEach((device) => { logger(`sending discovery for ${device.name}`); - const payload = getDiscoveryPayload(device); + const payload = device.supportsDim ? getDiscoveryDimmablePayload(device) : getDiscoveryPayload(device); self.deviceMap[device.id] = payload.unique_id; self.client.publish(