Dont' crash when removing non-existing device. https://github.com/Koenkk/zigbee2mqtt/issues/2241

This commit is contained in:
Koen Kanters
2019-11-03 15:02:00 +01:00
parent afbfdda0d7
commit 467c0062bb
2 changed files with 12 additions and 0 deletions
+5
View File
@@ -236,6 +236,11 @@ class BridgeConfig extends BaseExtension {
remove: ['removed', 'Removing', 'remove'],
};
if (!entity) {
logger.error(`Cannot ${lookup[action][2]}, device '${message}' does not exist`);
return;
}
const cleanup = () => {
// Remove from configuration.yaml
settings.removeDevice(entity.settings.ID);
+7
View File
@@ -289,6 +289,13 @@ describe('Bridge config', () => {
expect(settings.get().ban).toStrictEqual(['0x000b57fffec6a5b3']);
});
it('Shouldnt crash when removing non-existing device', async () => {
MQTT.publish.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/config/remove', 'not_existing_123');
await flushPromises();
expect(logger.error).toHaveBeenCalledWith(`Cannot remove, device 'not_existing_123' does not exist`);
});
it('Should handle when remove fails', async () => {
const device = zigbeeHerdsman.devices.bulb_color;
device.removeFromNetwork.mockClear();