Catch errors due to unknown device type to avoid addon crashing

This commit is contained in:
swevictor 2022-10-06 09:35:15 +02:00
parent 4ed7b4acb9
commit 4fb0267ad4
2 changed files with 58 additions and 42 deletions

View file

@ -428,34 +428,43 @@ class PlejdApi {
const dimmable = device.traits === TRAITS.DIMMABLE; const dimmable = device.traits === TRAITS.DIMMABLE;
// dimmable = settings.dimCurve !== 'NonDimmable'; // dimmable = settings.dimCurve !== 'NonDimmable';
const { name: typeName, type: deviceType } = this._getDeviceType(plejdDevice); try {
let loadType = deviceType; const { name: typeName, type: deviceType } = this._getDeviceType(plejdDevice);
if (device.outputType === 'RELAY') {
loadType = 'switch'; let loadType = deviceType;
} else if (device.outputType === 'LIGHT') { if (device.outputType === 'RELAY') {
loadType = 'light'; loadType = 'switch';
} else if (device.outputType === 'LIGHT') {
loadType = 'light';
}
const room = this.siteDetails.rooms.find((x) => x.roomId === device.roomId);
const roomTitle = room ? room.title : undefined;
/** @type {import('types/DeviceRegistry').OutputDevice} */
const outputDevice = {
bleOutputAddress,
deviceId: device.deviceId,
dimmable,
name: device.title,
output: deviceOutput,
roomId: device.roomId,
roomName: roomTitle,
state: undefined,
type: loadType,
typeName,
version: plejdDevice.firmware.version,
uniqueId: uniqueOutputId,
};
this.deviceRegistry.addOutputDevice(outputDevice);
} catch (error) {
logger.error(`Error trying to create output device: ${error}`);
logger.warn(`device (from API response) when error happened: ${JSON.stringify(device, null, 2)}`);
logger.warn(`plejdDevice (from API response) when error happened: ${JSON.stringify(plejdDevice, null, 2)}`);
} }
const room = this.siteDetails.rooms.find((x) => x.roomId === device.roomId);
const roomTitle = room ? room.title : undefined;
/** @type {import('types/DeviceRegistry').OutputDevice} */
const outputDevice = {
bleOutputAddress,
deviceId: device.deviceId,
dimmable,
name: device.title,
output: deviceOutput,
roomId: device.roomId,
roomName: roomTitle,
state: undefined,
type: loadType,
typeName,
version: plejdDevice.firmware.version,
uniqueId: uniqueOutputId,
};
this.deviceRegistry.addOutputDevice(outputDevice);
} }
} else { } else {
// The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01 // The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01
@ -476,21 +485,28 @@ class PlejdApi {
); );
const uniqueInputId = this.deviceRegistry.getUniqueInputId(device.deviceId, input.input); const uniqueInputId = this.deviceRegistry.getUniqueInputId(device.deviceId, input.input);
const { name: typeName, type, broadcastClicks } = this._getDeviceType(plejdDevice);
if (broadcastClicks) { try {
/** @type {import('types/DeviceRegistry').InputDevice} */ const { name: typeName, type, broadcastClicks } = this._getDeviceType(plejdDevice);
const inputDevice = { if (broadcastClicks) {
bleInputAddress, /** @type {import('types/DeviceRegistry').InputDevice} */
deviceId: device.deviceId, const inputDevice = {
name: device.title, bleInputAddress,
input: input.input, deviceId: device.deviceId,
roomId: device.roomId, name: device.title,
type, input: input.input,
typeName, roomId: device.roomId,
version: plejdDevice.firmware.version, type,
uniqueId: uniqueInputId, typeName,
}; version: plejdDevice.firmware.version,
this.deviceRegistry.addInputDevice(inputDevice); uniqueId: uniqueInputId,
};
this.deviceRegistry.addInputDevice(inputDevice);
}
} catch (error) {
logger.error(`Error trying to create input device: ${error}`);
logger.warn(`device (from API response) when error happened: ${JSON.stringify(device, null, 2)}`);
logger.warn(`plejdDevice (from API response) when error happened: ${JSON.stringify(plejdDevice, null, 2)}`);
} }
}); });
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "Plejd", "name": "Plejd",
"version": "0.9.1", "version": "0.10.0-alpha",
"slug": "plejd", "slug": "plejd",
"description": "Adds support for the Swedish home automation devices from Plejd.", "description": "Adds support for the Swedish home automation devices from Plejd.",
"url": "https://github.com/icanos/hassio-plejd/", "url": "https://github.com/icanos/hassio-plejd/",