From 5a2129036453232f53f781045955ab970448b438 Mon Sep 17 00:00:00 2001 From: faanskit Date: Sat, 1 May 2021 19:41:29 +0200 Subject: [PATCH 1/9] Added support for WRT-10 as a Device Automation --- plejd/DeviceRegistry.js | 46 +++++++++++++++++++++++ plejd/MqttClient.js | 45 +++++++++++++++++++++++ plejd/PlejdAddon.js | 8 ++++ plejd/PlejdApi.js | 61 ++++++++++++++++++++----------- plejd/PlejdBLEHandler.js | 19 ++++++++++ plejd/PlejdDeviceCommunication.js | 2 + plejd/constants.js | 1 + plejd/types/DeviceRegistry.d.ts | 14 +++++++ 8 files changed, 174 insertions(+), 22 deletions(-) diff --git a/plejd/DeviceRegistry.js b/plejd/DeviceRegistry.js index a031752..bd2e0d8 100644 --- a/plejd/DeviceRegistry.js +++ b/plejd/DeviceRegistry.js @@ -22,12 +22,29 @@ class DeviceRegistry { outputDevices = {}; /** @private @type {import('types/DeviceRegistry').OutputDevices} */ sceneDevices = {}; + /** @private @type {import('types/DeviceRegistry').InputDevices} */ + inputDevices = {}; /** @param device {import('./types/ApiSite').Device} */ addPhysicalDevice(device) { this.devices[device.deviceId] = device; } + /** @param inputDevice {import('types/DeviceRegistry').InputDevice} */ + addInputDevice(inputDevice) { + this.inputDevices = { + ...this.inputDevices, + [inputDevice.uniqueId]: inputDevice, + }; + + logger.verbose( + `Added/updated input device: ${JSON.stringify(inputDevice)}. ${ + Object.keys(this.inputDevices).length + } output devices in total.`, + ); + this.outputUniqueIdByBleOutputAddress[`${inputDevice.bleOutputAddress}_${inputDevice.input}`] = inputDevice.uniqueId; + }; + /** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */ addOutputDevice(outputDevice) { if (outputDevice.hiddenFromIntegrations || outputDevice.hiddenFromRoomList) { @@ -84,6 +101,7 @@ class DeviceRegistry { clearPlejdDevices() { this.devices = {}; this.outputDevices = {}; + this.inputDevices = {}; this.outputDeviceUniqueIdsByRoomId = {}; this.outputUniqueIdByBleOutputAddress = {}; } @@ -100,6 +118,13 @@ class DeviceRegistry { return Object.values(this.outputDevices); } + /** + * @returns {import('./types/DeviceRegistry').InputDevice[]} + */ + getAllInputDevices() { + return Object.values(this.inputDevices); + } + /** * @returns {import('./types/DeviceRegistry').OutputDevice[]} */ @@ -119,11 +144,23 @@ class DeviceRegistry { return this.outputDevices[uniqueOutputId]; } + /** + * @param {string} uniqueInputId + */ + getInputDevice(uniqueInputId) { + return this.inputDevices[uniqueInputId]; + } + /** @returns {import('./types/DeviceRegistry').OutputDevice} */ getOutputDeviceByBleOutputAddress(bleOutputAddress) { return this.outputDevices[this.outputUniqueIdByBleOutputAddress[bleOutputAddress]]; } + /** @returns {import('./types/DeviceRegistry').InputDevice} */ + getInputDeviceByBleOutputAddress(bleInputAddress, inputButton) { + return this.inputDevices[this.outputUniqueIdByBleOutputAddress[`${bleInputAddress}_${inputButton}`]]; + } + /** @returns {string[]} */ getOutputDeviceIdsByRoomId(roomId) { return this.outputDeviceUniqueIdsByRoomId[roomId]; @@ -133,6 +170,10 @@ class DeviceRegistry { return (this.outputDevices[uniqueOutputId] || {}).name; } + getInputDeviceName(uniqueInputId) { + return (this.inputDevices[uniqueInputId] || {}).name; + } + /** * @param {string } deviceId The physical device serial number * @return {import('./types/ApiSite').Device} @@ -171,6 +212,11 @@ class DeviceRegistry { return `${deviceId}_${outputIndex}`; } + // eslint-disable-next-line class-methods-use-this + getUniqueInputId(deviceId, inputIndex) { + return `${deviceId}_${inputIndex}`; + } + /** @param apiSite {import('./types/ApiSite').ApiSite} */ setApiSite(apiSite) { this.apiSite = apiSite; diff --git a/plejd/MqttClient.js b/plejd/MqttClient.js index c316798..4f1fb40 100644 --- a/plejd/MqttClient.js +++ b/plejd/MqttClient.js @@ -1,4 +1,5 @@ const EventEmitter = require('events'); +// @ts-ignore const mqtt = require('mqtt'); const Configuration = require('./Configuration'); @@ -35,6 +36,7 @@ const getTopicName = ( /** @type {'config' | 'state' | 'availability' | 'set'} */ topicType, ) => `${getBaseTopic(plug)}/${topicType}`; +const getButtonEventTopic = (deviceId) => `${getTopicName({ uniqueId: `${deviceId}`, type: 'device_automation' }, 'state')}`; const getSceneEventTopic = (sceneId) => `${getTopicName({ uniqueId: `${sceneId}_trigger`, type: 'device_automation' }, 'state')}`; const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`; @@ -89,6 +91,28 @@ const getSceneDiscoveryPayload = ( retain: false, }); +const getInputDeviceTriggerDiscoveryPayload = ( + /** @type {import('./types/DeviceRegistry').InputDevice} */ inputDevice, +) => ({ + automation_type: 'trigger', + payload: `${inputDevice.input}`, + '~': getBaseTopic({ + uniqueId: inputDevice.deviceId, + type: 'device_automation', + }), + qos: 1, + topic: `~/${TOPICS.STATE}`, + type: 'button_short_press', + subtype: `button_${inputDevice.input+1}`, + device: { + identifiers: `${inputDevice.deviceId}`, + manufacturer: 'Plejd', + model: inputDevice.typeName, + name: inputDevice.name, + }, +}); + + const getSceneDeviceTriggerhDiscoveryPayload = ( /** @type {import('./types/DeviceRegistry').OutputDevice} */ sceneDevice, ) => ({ @@ -288,6 +312,23 @@ class MqttClient extends EventEmitter { }, 2000); }); + const allInputDevices = this.deviceRegistry.getAllInputDevices(); + logger.info(`Sending discovery for ${allInputDevices.length} Plejd input devices`); + allInputDevices.forEach((inputDevice) => { + logger.debug(`Sending discovery for ${inputDevice.name}`); + const inputInputPayload = getInputDeviceTriggerDiscoveryPayload(inputDevice); + logger.info( + `Discovered ${inputDevice.typeName} (${inputDevice.type}) named ${inputDevice.name} (${inputDevice.bleOutputAddress} : ${inputDevice.uniqueId}).`, + ); + logger.verbose(`Publishing ${getTopicName(inputDevice, 'config')} with payload ${JSON.stringify(inputInputPayload)}`); + + this.client.publish(getTopicName(inputDevice, 'config'), JSON.stringify(inputInputPayload), { + retain: true, + qos: 1, + }); + + }) + const allSceneDevices = this.deviceRegistry.getAllSceneDevices(); logger.info(`Sending discovery for ${allSceneDevices.length} Plejd scene devices`); allSceneDevices.forEach((sceneDevice) => { @@ -373,6 +414,10 @@ class MqttClient extends EventEmitter { }); } + buttonPressed(data) { + logger.verbose(`Button ${data.deviceInput} pressed for deviceId ${data.deviceId}`); + this.client.publish(getButtonEventTopic(data.deviceId), `${data.deviceInput}`, { qos: 1 }); + } /** * @param {string} sceneId */ diff --git a/plejd/PlejdAddon.js b/plejd/PlejdAddon.js index cdb3d60..d78045b 100644 --- a/plejd/PlejdAddon.js +++ b/plejd/PlejdAddon.js @@ -126,6 +126,14 @@ class PlejdAddon extends EventEmitter { }, ); + this.plejdDeviceCommunication.on(PlejdDeviceCommunication.EVENTS.buttonPressed, (data) => { + try { + this.mqttClient.buttonPressed(data); + } catch (err) { + logger.error('Error in PlejdService.sceneTriggered callback', err); + } + }); + this.plejdDeviceCommunication.on(PlejdDeviceCommunication.EVENTS.sceneTriggered, (sceneId) => { try { this.mqttClient.sceneTriggered(sceneId); diff --git a/plejd/PlejdApi.js b/plejd/PlejdApi.js index 8609bbb..b6368c2 100644 --- a/plejd/PlejdApi.js +++ b/plejd/PlejdApi.js @@ -1,3 +1,4 @@ +// @ts-ignore const axios = require('axios').default; const fs = require('fs'); @@ -261,7 +262,7 @@ class PlejdApi { case 5: return { name: 'LED-10', type: 'light', dimmable: true }; case 6: - return { name: 'WPH-01', type: 'switch', dimmable: false }; + return { name: 'WPH-01', type: 'device_automation', dimmable: false }; case 7: return { name: 'REL-01', type: 'switch', dimmable: false }; case 8: @@ -369,30 +370,46 @@ class PlejdApi { this.deviceRegistry.addOutputDevice(outputDevice); } - } + } else { + // The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01 + // Filter inputSettings for available buttons + const inputSettings = this.siteDetails.inputSettings.filter( + (x) => x.deviceId === device.deviceId && (x.buttonType == 'DirectionUp') || (x.buttonType == 'DirectionDown')); - // What should we do with inputs?! - // if (outputDevice.typeName === 'WPH-01') { - // // WPH-01 is special, it has two buttons which needs to be - // // registered separately. - // const inputs = this.siteDetails.inputAddress[deviceId]; - // const first = inputs[0]; - // const second = inputs[1]; + // For each found button, register the device as an inputDevice + inputSettings.forEach((input) => { - // this.deviceRegistry.addPlejdDevice({ - // ...outputDevice, - // id: first, - // name: `${device.title} left`, - // }); + const bleInputAddress = this.siteDetails.deviceAddress[input.deviceId]; + logger.verbose( + `Found input device (${input.deviceId}), with input ${input.input} having BLE address (${bleInputAddress})`, + ); - // this.deviceRegistry.addPlejdDevice({ - // ...outputDevice, - // id: second, - // name: `${device.title} right`, - // }); - // } else { - // this.deviceRegistry.addPlejdDevice(outputDevice); - // } + const plejdDevice = this.siteDetails.plejdDevices.find( + (x) => x.deviceId === device.deviceId, + ); + + const uniqueInputId = this.deviceRegistry.getUniqueInputId( + device.deviceId, + input.input, + ); + + const { name: typeName, type } = this._getDeviceType(plejdDevice); + + /** @type {import('types/DeviceRegistry').InputDevice} */ + const inputDevice = { + bleOutputAddress: bleInputAddress, + deviceId: device.deviceId, + name: device.title, + input: input.input, + roomId: device.roomId, + type, + typeName, + version: plejdDevice.firmware.version, + uniqueId: uniqueInputId, + }; + this.deviceRegistry.addInputDevice(inputDevice); + }); + }; }); } diff --git a/plejd/PlejdBLEHandler.js b/plejd/PlejdBLEHandler.js index 75eeaf0..4d03865 100644 --- a/plejd/PlejdBLEHandler.js +++ b/plejd/PlejdBLEHandler.js @@ -1,3 +1,4 @@ +// @ts-ignore const dbus = require('dbus-next'); const crypto = require('crypto'); const xor = require('buffer-xor'); @@ -23,6 +24,7 @@ const BLE_CMD_DIM2_CHANGE = 0x0098; const BLE_CMD_STATE_CHANGE = 0x0097; const BLE_CMD_SCENE_TRIG = 0x0021; const BLE_CMD_TIME_UPDATE = 0x001b; +const BLE_CMD_REMOTE_CLICK = 0x0016; const BLE_BROADCAST_DEVICE_ID = 0x01; const BLE_REQUEST_NO_RESPONSE = 0x0110; @@ -906,6 +908,23 @@ class PlejBLEHandler extends EventEmitter { logger.info('Got time response. Plejd clock time in sync with Home Assistant time'); } } + } else if (cmd === BLE_CMD_REMOTE_CLICK) { + const inputBleAddress = state; + const inputButton = decoded.length > 7 ? decoded.readUInt8(6) : 0; + + const sourceDevice = this.deviceRegistry.getInputDeviceByBleOutputAddress(inputBleAddress, inputButton); + if (!sourceDevice) { + logger.warn( + `Scene with BLE address ${inputBleAddress} could not be found, can't process message`, + ); + return; + } + logger.verbose( + `WPH-10 button ${inputButton} at BLE address ${inputBleAddress} was pressed. Unique Id is ${sourceDevice.uniqueId}` + ); + command = COMMANDS.BUTTON_CLICK; + data = { deviceId: sourceDevice.deviceId, deviceInput: sourceDevice.input}; + this.emit(PlejBLEHandler.EVENTS.commandReceived, outputUniqueId, command, data); } else { logger.verbose( `Command ${cmd.toString(16)} unknown. ${decoded.toString( diff --git a/plejd/PlejdDeviceCommunication.js b/plejd/PlejdDeviceCommunication.js index 4321f94..9c218a0 100644 --- a/plejd/PlejdDeviceCommunication.js +++ b/plejd/PlejdDeviceCommunication.js @@ -116,6 +116,8 @@ class PlejdDeviceCommunication extends EventEmitter { }); } else if (command === COMMANDS.TRIGGER_SCENE) { this.emit(PlejdDeviceCommunication.EVENTS.sceneTriggered, data.sceneId); + } else if (command === COMMANDS.BUTTON_CLICK) { + this.emit(PlejdDeviceCommunication.EVENTS.buttonPressed, data); } else { logger.warn(`Unknown ble command ${command}`); } diff --git a/plejd/constants.js b/plejd/constants.js index 0b69b3a..1b97f67 100644 --- a/plejd/constants.js +++ b/plejd/constants.js @@ -3,6 +3,7 @@ const COMMANDS = { TURN_OFF: 'Turn off', DIM: 'Dim', TRIGGER_SCENE: 'Trigger scene', + BUTTON_CLICK: 'Button click', }; module.exports = { COMMANDS }; diff --git a/plejd/types/DeviceRegistry.d.ts b/plejd/types/DeviceRegistry.d.ts index b93c509..4da82a4 100644 --- a/plejd/types/DeviceRegistry.d.ts +++ b/plejd/types/DeviceRegistry.d.ts @@ -19,3 +19,17 @@ export interface OutputDevice { version: string; uniqueId: string; } + +export type InputDevices = { [deviceIdAndOutput: string]: InputDevice }; + +export interface InputDevice { + bleOutputAddress: number; + deviceId: string; + name: string; + input: number; + roomId: string; + type: string; + typeName: string; + version: string; + uniqueId: string; +} From cbcd33fde6dbef3204404497d3cba7df83d5d330 Mon Sep 17 00:00:00 2001 From: faanskit Date: Wed, 5 May 2021 12:33:30 +0200 Subject: [PATCH 2/9] Added support for WRT-01 --- plejd/PlejdApi.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plejd/PlejdApi.js b/plejd/PlejdApi.js index 2aab5f0..d129a29 100644 --- a/plejd/PlejdApi.js +++ b/plejd/PlejdApi.js @@ -270,7 +270,7 @@ class PlejdApi { // Unknown return { name: '-unknown-', type: 'light', dimmable: false }; case 10: - return { name: '-unknown-', type: 'light', dimmable: false }; + return { name: 'WRT-01', type: 'device_automation', dimmable: false }; case 12: // Unknown return { name: '-unknown-', type: 'light', dimmable: false }; @@ -380,7 +380,7 @@ class PlejdApi { // The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01 // Filter inputSettings for available buttons const inputSettings = this.siteDetails.inputSettings.filter( - (x) => x.deviceId === device.deviceId && (x.buttonType == 'DirectionUp') || (x.buttonType == 'DirectionDown')); + (x) => x.deviceId === device.deviceId && (x.buttonType == 'DirectionUp') || (x.buttonType == 'DirectionDown') || (x.buttonType == 'RotateMesh')); // For each found button, register the device as an inputDevice inputSettings.forEach((input) => { From 78e1616b4ee274e8f13c327ecd82899d63b12415 Mon Sep 17 00:00:00 2001 From: faanskit Date: Wed, 5 May 2021 19:02:10 +0200 Subject: [PATCH 3/9] Updated to match the latest developer branch and modified according to feedback from review. --- plejd/DeviceRegistry.js | 4 ++-- plejd/MqttClient.js | 15 ++++++--------- plejd/PlejdApi.js | 5 ++--- plejd/PlejdBLEHandler.js | 3 +-- plejd/types/ApiSite.d.ts | 2 ++ plejd/types/DeviceRegistry.d.ts | 2 +- 6 files changed, 14 insertions(+), 17 deletions(-) diff --git a/plejd/DeviceRegistry.js b/plejd/DeviceRegistry.js index bd2e0d8..a5efba6 100644 --- a/plejd/DeviceRegistry.js +++ b/plejd/DeviceRegistry.js @@ -42,7 +42,7 @@ class DeviceRegistry { Object.keys(this.inputDevices).length } output devices in total.`, ); - this.outputUniqueIdByBleOutputAddress[`${inputDevice.bleOutputAddress}_${inputDevice.input}`] = inputDevice.uniqueId; + this.outputUniqueIdByBleOutputAddress[`${inputDevice.bleInputAddress}_${inputDevice.input}`] = inputDevice.uniqueId; }; /** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */ @@ -157,7 +157,7 @@ class DeviceRegistry { } /** @returns {import('./types/DeviceRegistry').InputDevice} */ - getInputDeviceByBleOutputAddress(bleInputAddress, inputButton) { + getInputDeviceByBleInputAddress(bleInputAddress, inputButton) { return this.inputDevices[this.outputUniqueIdByBleOutputAddress[`${bleInputAddress}_${inputButton}`]]; } diff --git a/plejd/MqttClient.js b/plejd/MqttClient.js index 8d1b2e8..5c9df41 100644 --- a/plejd/MqttClient.js +++ b/plejd/MqttClient.js @@ -36,7 +36,7 @@ const getTopicName = ( /** @type { import('./types/Mqtt').TopicType } */ topicType, ) => `${getBaseTopic(uniqueId, mqttDeviceType)}/${topicType}`; -const getButtonEventTopic = (deviceId) => `${getTopicName({ uniqueId: `${deviceId}`, type: 'device_automation' }, 'state')}`; +const getButtonEventTopic = (/** @type {string} */ deviceId) => `${getTopicName(getTriggerUniqueId(deviceId), MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.STATE)}`; const getTriggerUniqueId = (/** @type { string } */ uniqueId) => `${uniqueId}_trigger`; const getSceneEventTopic = (/** @type {string} */ sceneId) => `${getTopicName(getTriggerUniqueId(sceneId), MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.STATE)}`; const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`; @@ -93,12 +93,9 @@ const getInputDeviceTriggerDiscoveryPayload = ( ) => ({ automation_type: 'trigger', payload: `${inputDevice.input}`, - '~': getBaseTopic({ - uniqueId: inputDevice.deviceId, - type: 'device_automation', - }), + '~': getBaseTopic(inputDevice.deviceId, MQTT_TYPES.DEVICE_AUTOMATION), qos: 1, - topic: `~/${TOPICS.STATE}`, + topic: `~/${TOPIC_TYPES.STATE}`, type: 'button_short_press', subtype: `button_${inputDevice.input+1}`, device: { @@ -336,11 +333,11 @@ class MqttClient extends EventEmitter { logger.debug(`Sending discovery for ${inputDevice.name}`); const inputInputPayload = getInputDeviceTriggerDiscoveryPayload(inputDevice); logger.info( - `Discovered ${inputDevice.typeName} (${inputDevice.type}) named ${inputDevice.name} (${inputDevice.bleOutputAddress} : ${inputDevice.uniqueId}).`, + `Discovered ${inputDevice.typeName} (${inputDevice.type}) named ${inputDevice.name} (${inputDevice.bleInputAddress} : ${inputDevice.uniqueId}).`, ); - logger.verbose(`Publishing ${getTopicName(inputDevice, 'config')} with payload ${JSON.stringify(inputInputPayload)}`); + logger.verbose(`Publishing ${getTopicName(inputDevice.uniqueId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.CONFIG)} with payload ${JSON.stringify(inputInputPayload)}`); - this.client.publish(getTopicName(inputDevice, 'config'), JSON.stringify(inputInputPayload), { + this.client.publish(getTopicName(inputDevice.uniqueId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.CONFIG), JSON.stringify(inputInputPayload), { retain: true, qos: 1, }); diff --git a/plejd/PlejdApi.js b/plejd/PlejdApi.js index d129a29..0b83b9b 100644 --- a/plejd/PlejdApi.js +++ b/plejd/PlejdApi.js @@ -1,4 +1,3 @@ -// @ts-ignore const axios = require('axios').default; const fs = require('fs'); @@ -380,7 +379,7 @@ class PlejdApi { // The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01 // Filter inputSettings for available buttons const inputSettings = this.siteDetails.inputSettings.filter( - (x) => x.deviceId === device.deviceId && (x.buttonType == 'DirectionUp') || (x.buttonType == 'DirectionDown') || (x.buttonType == 'RotateMesh')); + (x) => x.deviceId === device.deviceId && ((x.buttonType == 'DirectionUp') || (x.buttonType == 'DirectionDown') || (x.buttonType == 'RotateMesh'))); // For each found button, register the device as an inputDevice inputSettings.forEach((input) => { @@ -403,7 +402,7 @@ class PlejdApi { /** @type {import('types/DeviceRegistry').InputDevice} */ const inputDevice = { - bleOutputAddress: bleInputAddress, + bleInputAddress: bleInputAddress, deviceId: device.deviceId, name: device.title, input: input.input, diff --git a/plejd/PlejdBLEHandler.js b/plejd/PlejdBLEHandler.js index 4d03865..c81e2f9 100644 --- a/plejd/PlejdBLEHandler.js +++ b/plejd/PlejdBLEHandler.js @@ -1,4 +1,3 @@ -// @ts-ignore const dbus = require('dbus-next'); const crypto = require('crypto'); const xor = require('buffer-xor'); @@ -912,7 +911,7 @@ class PlejBLEHandler extends EventEmitter { const inputBleAddress = state; const inputButton = decoded.length > 7 ? decoded.readUInt8(6) : 0; - const sourceDevice = this.deviceRegistry.getInputDeviceByBleOutputAddress(inputBleAddress, inputButton); + const sourceDevice = this.deviceRegistry.getInputDeviceByBleInputAddress(inputBleAddress, inputButton); if (!sourceDevice) { logger.warn( `Scene with BLE address ${inputBleAddress} could not be found, can't process message`, diff --git a/plejd/types/ApiSite.d.ts b/plejd/types/ApiSite.d.ts index 856d056..4a6e562 100644 --- a/plejd/types/ApiSite.d.ts +++ b/plejd/types/ApiSite.d.ts @@ -280,6 +280,8 @@ export interface InputSetting { export enum ButtonType { PushButton = 'PushButton', + DirectionUp = 'DirectionUp', + DirectionDown = 'DirectionDown', RotateMesh = 'RotateMesh', Scene = 'Scene', } diff --git a/plejd/types/DeviceRegistry.d.ts b/plejd/types/DeviceRegistry.d.ts index 4da82a4..5ee68ae 100644 --- a/plejd/types/DeviceRegistry.d.ts +++ b/plejd/types/DeviceRegistry.d.ts @@ -23,7 +23,7 @@ export interface OutputDevice { export type InputDevices = { [deviceIdAndOutput: string]: InputDevice }; export interface InputDevice { - bleOutputAddress: number; + bleInputAddress: number; deviceId: string; name: string; input: number; From 5190341be6ec37c85e96b3cf05ebfe349ab0845d Mon Sep 17 00:00:00 2001 From: faanskit Date: Wed, 5 May 2021 19:05:52 +0200 Subject: [PATCH 4/9] removed ts-ignore --- plejd/MqttClient.js | 1 - 1 file changed, 1 deletion(-) diff --git a/plejd/MqttClient.js b/plejd/MqttClient.js index 5c9df41..aa803b6 100644 --- a/plejd/MqttClient.js +++ b/plejd/MqttClient.js @@ -1,5 +1,4 @@ const EventEmitter = require('events'); -// @ts-ignore const mqtt = require('mqtt'); const Configuration = require('./Configuration'); From 2610c1eaa4ff85b4dbaac85da650712d22fb3bb7 Mon Sep 17 00:00:00 2001 From: faanskit Date: Wed, 5 May 2021 19:18:43 +0200 Subject: [PATCH 5/9] Incorrect trigger --- plejd/MqttClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plejd/MqttClient.js b/plejd/MqttClient.js index aa803b6..fb1d0ab 100644 --- a/plejd/MqttClient.js +++ b/plejd/MqttClient.js @@ -35,7 +35,7 @@ const getTopicName = ( /** @type { import('./types/Mqtt').TopicType } */ topicType, ) => `${getBaseTopic(uniqueId, mqttDeviceType)}/${topicType}`; -const getButtonEventTopic = (/** @type {string} */ deviceId) => `${getTopicName(getTriggerUniqueId(deviceId), MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.STATE)}`; +const getButtonEventTopic = (/** @type {string} */ deviceId) => `${getTopicName(deviceId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.STATE)}`; const getTriggerUniqueId = (/** @type { string } */ uniqueId) => `${uniqueId}_trigger`; const getSceneEventTopic = (/** @type {string} */ sceneId) => `${getTopicName(getTriggerUniqueId(sceneId), MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.STATE)}`; const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`; From 531aa8ee3e237631292f117eb7cc2245bf104dab Mon Sep 17 00:00:00 2001 From: faanskit Date: Wed, 5 May 2021 19:34:34 +0200 Subject: [PATCH 6/9] Fixed errors from npm run lint:fix --- plejd/DeviceRegistry.js | 10 ++++--- plejd/MqttClient.js | 29 +++++++++++------- plejd/PlejdApi.js | 64 ++++++++++++++++++++-------------------- plejd/PlejdBLEHandler.js | 9 ++++-- 4 files changed, 63 insertions(+), 49 deletions(-) diff --git a/plejd/DeviceRegistry.js b/plejd/DeviceRegistry.js index a5efba6..ebc66ec 100644 --- a/plejd/DeviceRegistry.js +++ b/plejd/DeviceRegistry.js @@ -43,7 +43,7 @@ class DeviceRegistry { } output devices in total.`, ); this.outputUniqueIdByBleOutputAddress[`${inputDevice.bleInputAddress}_${inputDevice.input}`] = inputDevice.uniqueId; - }; + } /** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */ addOutputDevice(outputDevice) { @@ -121,7 +121,7 @@ class DeviceRegistry { /** * @returns {import('./types/DeviceRegistry').InputDevice[]} */ - getAllInputDevices() { + getAllInputDevices() { return Object.values(this.inputDevices); } @@ -147,7 +147,7 @@ class DeviceRegistry { /** * @param {string} uniqueInputId */ - getInputDevice(uniqueInputId) { + getInputDevice(uniqueInputId) { return this.inputDevices[uniqueInputId]; } @@ -158,7 +158,9 @@ class DeviceRegistry { /** @returns {import('./types/DeviceRegistry').InputDevice} */ getInputDeviceByBleInputAddress(bleInputAddress, inputButton) { - return this.inputDevices[this.outputUniqueIdByBleOutputAddress[`${bleInputAddress}_${inputButton}`]]; + return this.inputDevices[ + this.outputUniqueIdByBleOutputAddress[`${bleInputAddress}_${inputButton}`] + ]; } /** @returns {string[]} */ diff --git a/plejd/MqttClient.js b/plejd/MqttClient.js index fb1d0ab..c44f685 100644 --- a/plejd/MqttClient.js +++ b/plejd/MqttClient.js @@ -96,7 +96,7 @@ const getInputDeviceTriggerDiscoveryPayload = ( qos: 1, topic: `~/${TOPIC_TYPES.STATE}`, type: 'button_short_press', - subtype: `button_${inputDevice.input+1}`, + subtype: `button_${inputDevice.input + 1}`, device: { identifiers: `${inputDevice.deviceId}`, manufacturer: 'Plejd', @@ -105,7 +105,6 @@ const getInputDeviceTriggerDiscoveryPayload = ( }, }); - const getSceneDeviceTriggerhDiscoveryPayload = ( /** @type {import('./types/DeviceRegistry').OutputDevice} */ sceneDevice, ) => ({ @@ -334,14 +333,23 @@ class MqttClient extends EventEmitter { logger.info( `Discovered ${inputDevice.typeName} (${inputDevice.type}) named ${inputDevice.name} (${inputDevice.bleInputAddress} : ${inputDevice.uniqueId}).`, ); - logger.verbose(`Publishing ${getTopicName(inputDevice.uniqueId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.CONFIG)} with payload ${JSON.stringify(inputInputPayload)}`); + logger.verbose( + `Publishing ${getTopicName( + inputDevice.uniqueId, + MQTT_TYPES.DEVICE_AUTOMATION, + TOPIC_TYPES.CONFIG, + )} with payload ${JSON.stringify(inputInputPayload)}`, + ); - this.client.publish(getTopicName(inputDevice.uniqueId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.CONFIG), JSON.stringify(inputInputPayload), { - retain: true, - qos: 1, - }); - - }) + this.client.publish( + getTopicName(inputDevice.uniqueId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.CONFIG), + JSON.stringify(inputInputPayload), + { + retain: true, + qos: 1, + }, + ); + }); const allSceneDevices = this.deviceRegistry.getAllSceneDevices(); logger.info(`Sending discovery for ${allSceneDevices.length} Plejd scene devices`); @@ -438,10 +446,11 @@ class MqttClient extends EventEmitter { // ); } - buttonPressed(data) { + buttonPressed(data) { logger.verbose(`Button ${data.deviceInput} pressed for deviceId ${data.deviceId}`); this.client.publish(getButtonEventTopic(data.deviceId), `${data.deviceInput}`, { qos: 1 }); } + /** * @param {string} sceneId */ diff --git a/plejd/PlejdApi.js b/plejd/PlejdApi.js index 0b83b9b..db73ce1 100644 --- a/plejd/PlejdApi.js +++ b/plejd/PlejdApi.js @@ -379,42 +379,42 @@ class PlejdApi { // The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01 // Filter inputSettings for available buttons const inputSettings = this.siteDetails.inputSettings.filter( - (x) => x.deviceId === device.deviceId && ((x.buttonType == 'DirectionUp') || (x.buttonType == 'DirectionDown') || (x.buttonType == 'RotateMesh'))); + (x) => x.deviceId === device.deviceId + && (x.buttonType === 'DirectionUp' + || x.buttonType === 'DirectionDown' + || x.buttonType === 'RotateMesh'), + ); - // For each found button, register the device as an inputDevice - inputSettings.forEach((input) => { + // For each found button, register the device as an inputDevice + inputSettings.forEach((input) => { + const bleInputAddress = this.siteDetails.deviceAddress[input.deviceId]; + logger.verbose( + `Found input device (${input.deviceId}), with input ${input.input} having BLE address (${bleInputAddress})`, + ); - const bleInputAddress = this.siteDetails.deviceAddress[input.deviceId]; - logger.verbose( - `Found input device (${input.deviceId}), with input ${input.input} having BLE address (${bleInputAddress})`, - ); + const plejdDevice = this.siteDetails.plejdDevices.find( + (x) => x.deviceId === device.deviceId, + ); - const plejdDevice = this.siteDetails.plejdDevices.find( - (x) => x.deviceId === device.deviceId, - ); + const uniqueInputId = this.deviceRegistry.getUniqueInputId(device.deviceId, input.input); - const uniqueInputId = this.deviceRegistry.getUniqueInputId( - device.deviceId, - input.input, - ); - - const { name: typeName, type } = this._getDeviceType(plejdDevice); - - /** @type {import('types/DeviceRegistry').InputDevice} */ - const inputDevice = { - bleInputAddress: bleInputAddress, - deviceId: device.deviceId, - name: device.title, - input: input.input, - roomId: device.roomId, - type, - typeName, - version: plejdDevice.firmware.version, - uniqueId: uniqueInputId, - }; - this.deviceRegistry.addInputDevice(inputDevice); - }); - }; + const { name: typeName, type } = this._getDeviceType(plejdDevice); + + /** @type {import('types/DeviceRegistry').InputDevice} */ + const inputDevice = { + bleInputAddress, + deviceId: device.deviceId, + name: device.title, + input: input.input, + roomId: device.roomId, + type, + typeName, + version: plejdDevice.firmware.version, + uniqueId: uniqueInputId, + }; + this.deviceRegistry.addInputDevice(inputDevice); + }); + } }); } diff --git a/plejd/PlejdBLEHandler.js b/plejd/PlejdBLEHandler.js index c81e2f9..7c0c0ad 100644 --- a/plejd/PlejdBLEHandler.js +++ b/plejd/PlejdBLEHandler.js @@ -911,7 +911,10 @@ class PlejBLEHandler extends EventEmitter { const inputBleAddress = state; const inputButton = decoded.length > 7 ? decoded.readUInt8(6) : 0; - const sourceDevice = this.deviceRegistry.getInputDeviceByBleInputAddress(inputBleAddress, inputButton); + const sourceDevice = this.deviceRegistry.getInputDeviceByBleInputAddress( + inputBleAddress, + inputButton, + ); if (!sourceDevice) { logger.warn( `Scene with BLE address ${inputBleAddress} could not be found, can't process message`, @@ -919,10 +922,10 @@ class PlejBLEHandler extends EventEmitter { return; } logger.verbose( - `WPH-10 button ${inputButton} at BLE address ${inputBleAddress} was pressed. Unique Id is ${sourceDevice.uniqueId}` + `WPH-10 button ${inputButton} at BLE address ${inputBleAddress} was pressed. Unique Id is ${sourceDevice.uniqueId}`, ); command = COMMANDS.BUTTON_CLICK; - data = { deviceId: sourceDevice.deviceId, deviceInput: sourceDevice.input}; + data = { deviceId: sourceDevice.deviceId, deviceInput: sourceDevice.input }; this.emit(PlejBLEHandler.EVENTS.commandReceived, outputUniqueId, command, data); } else { logger.verbose( From c8890b8cd73c406c4d25d498d3ef775517c816d3 Mon Sep 17 00:00:00 2001 From: faanskit Date: Thu, 6 May 2021 07:58:06 +0200 Subject: [PATCH 7/9] Updates based om comments from @SweVictor --- plejd/DeviceRegistry.js | 13 ++++++++++--- plejd/MqttClient.js | 10 +++++++--- plejd/PlejdAddon.js | 17 ++++++++++------- plejd/PlejdBLEHandler.js | 2 +- plejd/PlejdDeviceCommunication.js | 2 +- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/plejd/DeviceRegistry.js b/plejd/DeviceRegistry.js index ebc66ec..2bfc63d 100644 --- a/plejd/DeviceRegistry.js +++ b/plejd/DeviceRegistry.js @@ -42,7 +42,9 @@ class DeviceRegistry { Object.keys(this.inputDevices).length } output devices in total.`, ); - this.outputUniqueIdByBleOutputAddress[`${inputDevice.bleInputAddress}_${inputDevice.input}`] = inputDevice.uniqueId; + this.outputUniqueIdByBleOutputAddress[ + this.getUniqueBLEId(inputDevice.bleInputAddress, inputDevice.input) + ] = inputDevice.uniqueId; } /** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */ @@ -159,7 +161,7 @@ class DeviceRegistry { /** @returns {import('./types/DeviceRegistry').InputDevice} */ getInputDeviceByBleInputAddress(bleInputAddress, inputButton) { return this.inputDevices[ - this.outputUniqueIdByBleOutputAddress[`${bleInputAddress}_${inputButton}`] + this.outputUniqueIdByBleOutputAddress[this.getUniqueBLEId(bleInputAddress, inputButton)] ]; } @@ -216,7 +218,12 @@ class DeviceRegistry { // eslint-disable-next-line class-methods-use-this getUniqueInputId(deviceId, inputIndex) { - return `${deviceId}_${inputIndex}`; + return `${deviceId}_I_${inputIndex}`; + } + + // eslint-disable-next-line class-methods-use-this + getUniqueBLEId(bleAdress, inputIndex) { + return `${bleAdress}_${inputIndex}`; } /** @param apiSite {import('./types/ApiSite').ApiSite} */ diff --git a/plejd/MqttClient.js b/plejd/MqttClient.js index c44f685..64586bd 100644 --- a/plejd/MqttClient.js +++ b/plejd/MqttClient.js @@ -446,9 +446,13 @@ class MqttClient extends EventEmitter { // ); } - buttonPressed(data) { - logger.verbose(`Button ${data.deviceInput} pressed for deviceId ${data.deviceId}`); - this.client.publish(getButtonEventTopic(data.deviceId), `${data.deviceInput}`, { qos: 1 }); + /** + * @param {string} deviceId + * @param {string} deviceInput + */ + buttonPressed(deviceId, deviceInput) { + logger.verbose(`Button ${deviceInput} pressed for deviceId ${deviceId}`); + this.client.publish(getButtonEventTopic(deviceId), `${deviceInput}`, { qos: 1 }); } /** diff --git a/plejd/PlejdAddon.js b/plejd/PlejdAddon.js index d78045b..89e8310 100644 --- a/plejd/PlejdAddon.js +++ b/plejd/PlejdAddon.js @@ -126,13 +126,16 @@ class PlejdAddon extends EventEmitter { }, ); - this.plejdDeviceCommunication.on(PlejdDeviceCommunication.EVENTS.buttonPressed, (data) => { - try { - this.mqttClient.buttonPressed(data); - } catch (err) { - logger.error('Error in PlejdService.sceneTriggered callback', err); - } - }); + this.plejdDeviceCommunication.on( + PlejdDeviceCommunication.EVENTS.buttonPressed, + (deviceId, deviceInput) => { + try { + this.mqttClient.buttonPressed(deviceId, deviceInput); + } catch (err) { + logger.error('Error in PlejdService.buttonPressed callback', err); + } + }, + ); this.plejdDeviceCommunication.on(PlejdDeviceCommunication.EVENTS.sceneTriggered, (sceneId) => { try { diff --git a/plejd/PlejdBLEHandler.js b/plejd/PlejdBLEHandler.js index 7c0c0ad..0a69961 100644 --- a/plejd/PlejdBLEHandler.js +++ b/plejd/PlejdBLEHandler.js @@ -922,7 +922,7 @@ class PlejBLEHandler extends EventEmitter { return; } logger.verbose( - `WPH-10 button ${inputButton} at BLE address ${inputBleAddress} was pressed. Unique Id is ${sourceDevice.uniqueId}`, + `A button (eg. WPH-01, WRT-01) ${inputButton} at BLE address ${inputBleAddress} was pressed. Unique Id is ${sourceDevice.uniqueId}`, ); command = COMMANDS.BUTTON_CLICK; data = { deviceId: sourceDevice.deviceId, deviceInput: sourceDevice.input }; diff --git a/plejd/PlejdDeviceCommunication.js b/plejd/PlejdDeviceCommunication.js index 9c218a0..8626b69 100644 --- a/plejd/PlejdDeviceCommunication.js +++ b/plejd/PlejdDeviceCommunication.js @@ -117,7 +117,7 @@ class PlejdDeviceCommunication extends EventEmitter { } else if (command === COMMANDS.TRIGGER_SCENE) { this.emit(PlejdDeviceCommunication.EVENTS.sceneTriggered, data.sceneId); } else if (command === COMMANDS.BUTTON_CLICK) { - this.emit(PlejdDeviceCommunication.EVENTS.buttonPressed, data); + this.emit(PlejdDeviceCommunication.EVENTS.buttonPressed, data.deviceId, data.deviceInput); } else { logger.warn(`Unknown ble command ${command}`); } From 535c8f6c0f2b9f85b43831f05e780e45e414f881 Mon Sep 17 00:00:00 2001 From: faanskit Date: Thu, 6 May 2021 13:28:13 +0200 Subject: [PATCH 8/9] Fixed the issue when a WPH were configured with single click to run a scene. Made code a bit more resilient. --- plejd/PlejdApi.js | 149 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 113 insertions(+), 36 deletions(-) diff --git a/plejd/PlejdApi.js b/plejd/PlejdApi.js index db73ce1..ab5cbf4 100644 --- a/plejd/PlejdApi.js +++ b/plejd/PlejdApi.js @@ -251,44 +251,124 @@ class PlejdApi { switch (parseInt(plejdDevice.hardwareId, 10)) { case 1: case 11: - return { name: 'DIM-01', type: 'light', dimmable: true }; + return { + name: 'DIM-01', + type: 'light', + dimmable: true, + broadcastClicks: false, + }; case 2: - return { name: 'DIM-02', type: 'light', dimmable: true }; + return { + name: 'DIM-02', + type: 'light', + dimmable: true, + broadcastClicks: false, + }; case 3: - return { name: 'CTR-01', type: 'light', dimmable: false }; + return { + name: 'CTR-01', + type: 'light', + dimmable: false, + broadcastClicks: false, + }; case 4: - return { name: 'GWY-01', type: 'sensor', dimmable: false }; + return { + name: 'GWY-01', + type: 'sensor', + dimmable: false, + broadcastClicks: false, + }; case 5: - return { name: 'LED-10', type: 'light', dimmable: true }; + return { + name: 'LED-10', + type: 'light', + dimmable: true, + broadcastClicks: false, + }; case 6: - return { name: 'WPH-01', type: 'device_automation', dimmable: false }; + return { + name: 'WPH-01', + type: 'device_automation', + dimmable: false, + broadcastClicks: true, + }; case 7: - return { name: 'REL-01', type: 'switch', dimmable: false }; + return { + name: 'REL-01', + type: 'switch', + dimmable: false, + broadcastClicks: false, + }; case 8: case 9: // Unknown - return { name: '-unknown-', type: 'light', dimmable: false }; + return { + name: '-unknown-', + type: 'light', + dimmable: false, + broadcastClicks: false, + }; case 10: - return { name: 'WRT-01', type: 'device_automation', dimmable: false }; + return { + name: 'WRT-01', + type: 'device_automation', + dimmable: false, + broadcastClicks: true, + }; case 12: // Unknown - return { name: '-unknown-', type: 'light', dimmable: false }; + return { + name: '-unknown-', + type: 'light', + dimmable: false, + broadcastClicks: false, + }; case 13: - return { name: 'Generic', type: 'light', dimmable: false }; + return { + name: 'Generic', + type: 'light', + dimmable: false, + broadcastClicks: false, + }; case 14: case 15: case 16: // Unknown - return { name: '-unknown-', type: 'light', dimmable: false }; + return { + name: '-unknown-', + type: 'light', + dimmable: false, + broadcastClicks: false, + }; case 17: - return { name: 'REL-01', type: 'switch', dimmable: false }; + return { + name: 'REL-01', + type: 'switch', + dimmable: false, + broadcastClicks: false, + }; case 18: - return { name: 'REL-02', type: 'switch', dimmable: false }; + return { + name: 'REL-02', + type: 'switch', + dimmable: false, + broadcastClicks: false, + }; case 19: // Unknown - return { name: '-unknown-', type: 'light', dimmable: false }; + return { + name: '-unknown-', + type: 'light', + dimmable: false, + broadcastClicks: false, + }; case 20: - return { name: 'SPR-01', type: 'switch', dimmable: false }; + return { + name: 'SPR-01', + type: 'switch', + dimmable: false, + broadcastClicks: false, + }; default: throw new Error(`Unknown device type with id ${plejdDevice.hardwareId}`); } @@ -379,10 +459,7 @@ class PlejdApi { // The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01 // Filter inputSettings for available buttons const inputSettings = this.siteDetails.inputSettings.filter( - (x) => x.deviceId === device.deviceId - && (x.buttonType === 'DirectionUp' - || x.buttonType === 'DirectionDown' - || x.buttonType === 'RotateMesh'), + (x) => x.deviceId === device.deviceId, ); // For each found button, register the device as an inputDevice @@ -397,22 +474,22 @@ class PlejdApi { ); const uniqueInputId = this.deviceRegistry.getUniqueInputId(device.deviceId, input.input); - - const { name: typeName, type } = this._getDeviceType(plejdDevice); - - /** @type {import('types/DeviceRegistry').InputDevice} */ - const inputDevice = { - bleInputAddress, - deviceId: device.deviceId, - name: device.title, - input: input.input, - roomId: device.roomId, - type, - typeName, - version: plejdDevice.firmware.version, - uniqueId: uniqueInputId, - }; - this.deviceRegistry.addInputDevice(inputDevice); + const { name: typeName, type, broadcastClicks } = this._getDeviceType(plejdDevice); + if (broadcastClicks) { + /** @type {import('types/DeviceRegistry').InputDevice} */ + const inputDevice = { + bleInputAddress, + deviceId: device.deviceId, + name: device.title, + input: input.input, + roomId: device.roomId, + type, + typeName, + version: plejdDevice.firmware.version, + uniqueId: uniqueInputId, + }; + this.deviceRegistry.addInputDevice(inputDevice); + } }); } }); From 49024f539206b5ccb75471f10a875a11c724eb89 Mon Sep 17 00:00:00 2001 From: faanskit Date: Thu, 6 May 2021 13:43:51 +0200 Subject: [PATCH 9/9] Updated README.md to include information about WRT-01, tested by @vBrolin --- plejd/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plejd/README.md b/plejd/README.md index 336ea57..e126733 100644 --- a/plejd/README.md +++ b/plejd/README.md @@ -41,7 +41,8 @@ The add-on has been tested on the following platforms: - CTR-01 - REL-01 - REL-02 -- WPH-01 +- WPH-01 (Note: Available as Device Trigger short_button_press, button_1 .. button_4) +- WRT-01 (Note: Available as Device Trigger short_button_press, button_1. Rotation/dimming not offered by the device) ### Easy Installation