Merge pull request #2 from faanskit/feature/id-refactor
Added support for WRT-10 as a Device Automation
This commit is contained in:
commit
39c0658871
8 changed files with 174 additions and 22 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 { import('./types/Mqtt').TopicType } */ topicType,
|
||||
) => `${getBaseTopic(uniqueId, mqttDeviceType)}/${topicType}`;
|
||||
|
||||
const getButtonEventTopic = (deviceId) => `${getTopicName({ uniqueId: `${deviceId}`, type: 'device_automation' }, '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}/#`;
|
||||
|
|
@ -86,6 +88,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,
|
||||
) => ({
|
||||
|
|
@ -306,6 +330,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) => {
|
||||
|
|
@ -401,6 +442,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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
@ -375,30 +376,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);
|
||||
});
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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}`);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ const COMMANDS = {
|
|||
TURN_OFF: 'Turn off',
|
||||
DIM: 'Dim',
|
||||
TRIGGER_SCENE: 'Trigger scene',
|
||||
BUTTON_CLICK: 'Button click',
|
||||
};
|
||||
|
||||
module.exports = { COMMANDS };
|
||||
|
|
|
|||
14
plejd/types/DeviceRegistry.d.ts
vendored
14
plejd/types/DeviceRegistry.d.ts
vendored
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue