mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-27 13:10:03 +00:00
Fix error when device is added when device: [] in configuration.yaml (#16629)
* fix for TypeError: The "path" argument must be of type string in writeDevicesOrGroups
* added test case that produces the error when running on old implementation
Summary of all failing tests
FAIL test/settings.test.js
● Settings › Should add devices even when devices exist empty
TypeError: The "path" argument must be of type string. Received undefined
275 |
276 | jest.mock('../../lib/util/data', () => ({
> 277 | joinPath: (file) => require('path').join(mockDir, file),
| ^
278 | getPath: () => mockDir,
279 | }));
280 |
at Object.join [as joinPath] (test/stub/data.js:277:41)
at joinPath (lib/util/settings.ts:245:38)
at writeDevicesOrGroups (lib/util/settings.ts:250:5)
at Object.write [as addDevice] (lib/util/settings.ts:555:5)
at Object.addDevice (test/settings.test.js:128:18)
This commit is contained in:
@@ -230,7 +230,7 @@ function write(): void {
|
||||
|
||||
// Write devices/groups to separate file if required.
|
||||
const writeDevicesOrGroups = (type: 'devices' | 'groups'): void => {
|
||||
if (typeof actual[type] === 'string' || Array.isArray(actual[type])) {
|
||||
if (typeof actual[type] === 'string' || (Array.isArray(actual[type]) && actual[type].length > 0)) {
|
||||
const fileToWrite = Array.isArray(actual[type]) ? actual[type][0] : actual[type];
|
||||
const content = objectAssignDeep({}, settings[type]);
|
||||
|
||||
@@ -361,7 +361,7 @@ function read(): Settings {
|
||||
|
||||
// Read devices/groups configuration from separate file if specified.
|
||||
const readDevicesOrGroups = (type: 'devices' | 'groups'): void => {
|
||||
if (typeof s[type] === 'string' || Array.isArray(s[type])) {
|
||||
if (typeof s[type] === 'string' || (Array.isArray(s[type]) && Array(s[type]).length > 0)) {
|
||||
/* eslint-disable-line */ // @ts-ignore
|
||||
const files: string[] = Array.isArray(s[type]) ? s[type] : [s[type]];
|
||||
s[type] = {};
|
||||
|
||||
+23
-7
@@ -123,6 +123,22 @@ describe('Settings', () => {
|
||||
expect(actual).toStrictEqual(expected);
|
||||
});
|
||||
|
||||
it('Should add devices even when devices exist empty', () => {
|
||||
write(configurationFile, {devices: []});
|
||||
settings.addDevice('0x12345678');
|
||||
|
||||
const actual = read(configurationFile);
|
||||
const expected = {
|
||||
devices: {
|
||||
'0x12345678': {
|
||||
friendly_name: '0x12345678',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
expect(actual).toStrictEqual(expected);
|
||||
});
|
||||
|
||||
it('Should read devices', () => {
|
||||
const content = {
|
||||
devices: {
|
||||
@@ -904,7 +920,7 @@ describe('Settings', () => {
|
||||
|
||||
it('Frontend config', () => {
|
||||
write(configurationFile, {...minimalConfig,
|
||||
frontend: true,
|
||||
frontend: true,
|
||||
});
|
||||
|
||||
settings.reRead();
|
||||
@@ -913,7 +929,7 @@ describe('Settings', () => {
|
||||
|
||||
it('Baudrate config', () => {
|
||||
write(configurationFile, {...minimalConfig,
|
||||
advanced: {baudrate: 20},
|
||||
advanced: {baudrate: 20},
|
||||
});
|
||||
|
||||
settings.reRead();
|
||||
@@ -922,7 +938,7 @@ describe('Settings', () => {
|
||||
|
||||
it('ikea_ota_use_test_url config', () => {
|
||||
write(configurationFile, {...minimalConfig,
|
||||
advanced: {ikea_ota_use_test_url: true},
|
||||
advanced: {ikea_ota_use_test_url: true},
|
||||
});
|
||||
|
||||
settings.reRead();
|
||||
@@ -931,7 +947,7 @@ describe('Settings', () => {
|
||||
|
||||
it('transmit_power config', () => {
|
||||
write(configurationFile, {...minimalConfig,
|
||||
experimental: {transmit_power: 1337},
|
||||
experimental: {transmit_power: 1337},
|
||||
});
|
||||
|
||||
settings.reRead();
|
||||
@@ -940,7 +956,7 @@ describe('Settings', () => {
|
||||
|
||||
it('output config', () => {
|
||||
write(configurationFile, {...minimalConfig,
|
||||
experimental: {output: 'json'},
|
||||
experimental: {output: 'json'},
|
||||
});
|
||||
|
||||
settings.reRead();
|
||||
@@ -949,7 +965,7 @@ describe('Settings', () => {
|
||||
|
||||
it('Baudrartsctste config', () => {
|
||||
write(configurationFile, {...minimalConfig,
|
||||
advanced: {rtscts: true},
|
||||
advanced: {rtscts: true},
|
||||
});
|
||||
|
||||
settings.reRead();
|
||||
@@ -958,7 +974,7 @@ describe('Settings', () => {
|
||||
|
||||
it('Deprecated: Home Assistant config', () => {
|
||||
write(configurationFile, {...minimalConfig,
|
||||
homeassistant: {discovery_topic: 'new'},
|
||||
homeassistant: {discovery_topic: 'new'},
|
||||
advanced: {homeassistant_discovery_topic: 'old', homeassistant_status_topic: 'olds'},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user