Fix crash when removing device from group which has no devices in settings. #3185

This commit is contained in:
Koen Kanters 2020-03-23 22:24:35 +01:00
parent dfc897af1a
commit 5cd1af569e
2 changed files with 28 additions and 0 deletions

View File

@ -591,6 +591,9 @@ function removeDeviceFromGroup(groupIDorName, keys) {
const groupID = getGroupThrowIfNotExists(groupIDorName).ID; const groupID = getGroupThrowIfNotExists(groupIDorName).ID;
const settings = get(); const settings = get();
const group = settings.groups[groupID]; const group = settings.groups[groupID];
if (!group.devices) {
group.devices = [];
}
const key = groupHasDevice(group, keys); const key = groupHasDevice(group, keys);
if (key) { if (key) {

View File

@ -434,6 +434,31 @@ describe('Settings', () => {
expect(settings.get().groups).toStrictEqual(expected); expect(settings.get().groups).toStrictEqual(expected);
}); });
it('Should crash when removing device from group when group has no devices', () => {
write(configurationFile, {
devices: {
'0x123': {
friendly_name: 'bulb',
retain: true,
}
},
groups: {
'1': {
friendly_name: 'test123',
}
}
});
settings.removeDeviceFromGroup('test123', ['0x123']);
const expected = {
'1': {
friendly_name: 'test123',
},
};
expect(settings.get().groups).toStrictEqual(expected);
});
it('Should throw when adding device to non-existing group', () => { it('Should throw when adding device to non-existing group', () => {
write(configurationFile, { write(configurationFile, {
devices: { devices: {