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;