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}`); }