Minor fixes
This commit is contained in:
parent
7de1238c12
commit
dbc0e02f11
4 changed files with 30 additions and 25 deletions
|
|
@ -13,10 +13,8 @@ const logger = Logger.getLogger('plejd-mqtt');
|
||||||
const discoveryPrefix = 'homeassistant';
|
const discoveryPrefix = 'homeassistant';
|
||||||
const nodeId = 'plejd';
|
const nodeId = 'plejd';
|
||||||
|
|
||||||
const getMqttUniqueId = (/** @type {string} */ uniqueId) => `${nodeId}.${uniqueId}`;
|
|
||||||
|
|
||||||
const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`;
|
const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`;
|
||||||
const getBaseTopic = (/** @type {{ uniqueId: string; type: string; }} */ plug) => `${discoveryPrefix}/${plug.type}/${nodeId}/${getMqttUniqueId(plug.uniqueId)}`;
|
const getBaseTopic = (/** @type {{ uniqueId: string; type: string; }} */ plug) => `${discoveryPrefix}/${plug.type}/${nodeId}/${plug.uniqueId}`;
|
||||||
|
|
||||||
const getTopicName = (
|
const getTopicName = (
|
||||||
/** @type {{ uniqueId: string; type: string; }} */ plug,
|
/** @type {{ uniqueId: string; type: string; }} */ plug,
|
||||||
|
|
@ -48,7 +46,7 @@ const getLightDiscoveryPayload = (
|
||||||
) => ({
|
) => ({
|
||||||
schema: 'json',
|
schema: 'json',
|
||||||
name: device.name,
|
name: device.name,
|
||||||
unique_id: getMqttUniqueId(device.uniqueId),
|
unique_id: device.uniqueId,
|
||||||
'~': getBaseTopic(device),
|
'~': getBaseTopic(device),
|
||||||
state_topic: `~/${TOPICS.STATE}`,
|
state_topic: `~/${TOPICS.STATE}`,
|
||||||
command_topic: `~/${TOPICS.COMMAND}`,
|
command_topic: `~/${TOPICS.COMMAND}`,
|
||||||
|
|
|
||||||
|
|
@ -320,6 +320,16 @@ class PlejdApi {
|
||||||
(x) => x.deviceParseId === device.objectId,
|
(x) => x.deviceParseId === device.objectId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!outputSettings) {
|
||||||
|
logger.verbose(
|
||||||
|
`No outputSettings found for ${device.title} (${device.deviceId}), assuming output 0`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const bleOutputAddress = this.siteDetails.outputAddress[device.deviceId][
|
||||||
|
outputSettings ? outputSettings.output : 0
|
||||||
|
];
|
||||||
|
|
||||||
if (device.traits === TRAITS.NO_LOAD) {
|
if (device.traits === TRAITS.NO_LOAD) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`Device ${device.title} (${device.deviceId}) has no load configured and will be excluded`,
|
`Device ${device.title} (${device.deviceId}) has no load configured and will be excluded`,
|
||||||
|
|
@ -330,10 +340,6 @@ class PlejdApi {
|
||||||
outputSettings.output,
|
outputSettings.output,
|
||||||
);
|
);
|
||||||
|
|
||||||
const bleOutputAddress = this.siteDetails.outputAddress[device.deviceId][
|
|
||||||
outputSettings.output
|
|
||||||
];
|
|
||||||
|
|
||||||
const plejdDevice = this.siteDetails.plejdDevices.find(
|
const plejdDevice = this.siteDetails.plejdDevices.find(
|
||||||
(x) => x.deviceId === device.deviceId,
|
(x) => x.deviceId === device.deviceId,
|
||||||
);
|
);
|
||||||
|
|
@ -361,13 +367,6 @@ class PlejdApi {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.deviceRegistry.addOutputDevice(outputDevice);
|
this.deviceRegistry.addOutputDevice(outputDevice);
|
||||||
} else {
|
|
||||||
logger.warn(
|
|
||||||
`No outputSettings found for ${device.title} (${device.deviceId}), device will not be included`,
|
|
||||||
);
|
|
||||||
logger.verbose(
|
|
||||||
'Fallback cound potentially be implemented by assuming default deviceSettings[deviceId]',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// What should we do with inputs?!
|
// What should we do with inputs?!
|
||||||
|
|
|
||||||
|
|
@ -154,21 +154,26 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
logger.info('BLE init done, waiting for devices.');
|
logger.info('BLE init done, waiting for devices.');
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendCommand(command, uniqueOutputId, data) {
|
/**
|
||||||
|
* @param {string} command
|
||||||
|
* @param {number} bleOutputAddress
|
||||||
|
* @param {number} data
|
||||||
|
*/
|
||||||
|
async sendCommand(command, bleOutputAddress, data) {
|
||||||
let payload;
|
let payload;
|
||||||
let brightnessVal;
|
let brightnessVal;
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case COMMANDS.TURN_ON:
|
case COMMANDS.TURN_ON:
|
||||||
payload = this._createHexPayload(uniqueOutputId, BLE_CMD_STATE_CHANGE, '01');
|
payload = this._createHexPayload(bleOutputAddress, BLE_CMD_STATE_CHANGE, '01');
|
||||||
break;
|
break;
|
||||||
case COMMANDS.TURN_OFF:
|
case COMMANDS.TURN_OFF:
|
||||||
payload = this._createHexPayload(uniqueOutputId, BLE_CMD_STATE_CHANGE, '00');
|
payload = this._createHexPayload(bleOutputAddress, BLE_CMD_STATE_CHANGE, '00');
|
||||||
break;
|
break;
|
||||||
case COMMANDS.DIM:
|
case COMMANDS.DIM:
|
||||||
// eslint-disable-next-line no-bitwise
|
// eslint-disable-next-line no-bitwise
|
||||||
brightnessVal = (data << 8) | data;
|
brightnessVal = (data << 8) | data;
|
||||||
payload = this._createHexPayload(
|
payload = this._createHexPayload(
|
||||||
uniqueOutputId,
|
bleOutputAddress,
|
||||||
BLE_CMD_DIM2_CHANGE,
|
BLE_CMD_DIM2_CHANGE,
|
||||||
`01${brightnessVal.toString(16).padStart(4, '0')}`,
|
`01${brightnessVal.toString(16).padStart(4, '0')}`,
|
||||||
);
|
);
|
||||||
|
|
@ -820,7 +825,9 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
// decoded.toString() could potentially be expensive
|
// decoded.toString() could potentially be expensive
|
||||||
logger.verbose(`Raw event received: ${decoded.toString('hex')}`);
|
logger.verbose(`Raw event received: ${decoded.toString('hex')}`);
|
||||||
logger.verbose(
|
logger.verbose(
|
||||||
`Decoded: Device ${outputUniqueId}, cmd ${cmd.toString(16)}, state ${state}, dim ${dim}`,
|
`Decoded: Device ${outputUniqueId} (BLE address ${bleOutputAddress}), cmd ${cmd.toString(
|
||||||
|
16,
|
||||||
|
)}, state ${state}, dim ${dim}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -255,9 +255,10 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const queueItem = this.writeQueue.pop();
|
const queueItem = this.writeQueue.pop();
|
||||||
const deviceName = this.deviceRegistry.getOutputDeviceName(queueItem.uniqueOutputId);
|
const device = this.deviceRegistry.getOutputDevice(queueItem.uniqueOutputId);
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Write queue: Processing ${deviceName} (${queueItem.uniqueOutputId}). Command ${
|
`Write queue: Processing ${device.name} (${queueItem.uniqueOutputId}). Command ${
|
||||||
queueItem.command
|
queueItem.command
|
||||||
}${queueItem.data ? ` ${queueItem.data}` : ''}. Total queue length: ${
|
}${queueItem.data ? ` ${queueItem.data}` : ''}. Total queue length: ${
|
||||||
this.writeQueue.length
|
this.writeQueue.length
|
||||||
|
|
@ -266,7 +267,7 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
|
|
||||||
if (this.writeQueue.some((item) => item.uniqueOutputId === queueItem.uniqueOutputId)) {
|
if (this.writeQueue.some((item) => item.uniqueOutputId === queueItem.uniqueOutputId)) {
|
||||||
logger.verbose(
|
logger.verbose(
|
||||||
`Skipping ${deviceName} (${queueItem.uniqueOutputId}) `
|
`Skipping ${device.name} (${queueItem.uniqueOutputId}) `
|
||||||
+ `${queueItem.command} due to more recent command in queue.`,
|
+ `${queueItem.command} due to more recent command in queue.`,
|
||||||
);
|
);
|
||||||
// Skip commands if new ones exist for the same uniqueOutputId
|
// Skip commands if new ones exist for the same uniqueOutputId
|
||||||
|
|
@ -276,7 +277,7 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
try {
|
try {
|
||||||
await this.plejdBleHandler.sendCommand(
|
await this.plejdBleHandler.sendCommand(
|
||||||
queueItem.command,
|
queueItem.command,
|
||||||
queueItem.uniqueOutputId,
|
device.bleOutputAddress,
|
||||||
queueItem.data,
|
queueItem.data,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
@ -287,7 +288,7 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
this.writeQueue.push(queueItem); // Add back to top of queue to be processed next;
|
this.writeQueue.push(queueItem); // Add back to top of queue to be processed next;
|
||||||
} else {
|
} else {
|
||||||
logger.error(
|
logger.error(
|
||||||
`Write queue: Exceeed max retry count (${MAX_RETRY_COUNT}) for ${deviceName} (${queueItem.uniqueOutputId}). Command ${queueItem.command} failed.`,
|
`Write queue: Exceeed max retry count (${MAX_RETRY_COUNT}) for ${device.name} (${queueItem.uniqueOutputId}). Command ${queueItem.command} failed.`,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue