From 2eadf30199fdb55ecd9ac7d24f57afad151852b9 Mon Sep 17 00:00:00 2001 From: Tami <11450358+zeroflag0@users.noreply.github.com> Date: Mon, 13 Feb 2023 19:41:49 +0100 Subject: [PATCH] Fix error when device is added when `device: []` in `configuration.yaml` (#16629) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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) --- lib/util/settings.ts | 4 ++-- test/settings.test.js | 30 +++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/util/settings.ts b/lib/util/settings.ts index ae03b898b..c1becab7f 100644 --- a/lib/util/settings.ts +++ b/lib/util/settings.ts @@ -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] = {}; diff --git a/test/settings.test.js b/test/settings.test.js index 68a05e1f0..55d794368 100644 --- a/test/settings.test.js +++ b/test/settings.test.js @@ -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'}, });