From 2bca5deb66bc45d393b51db9229d7b32661a72e8 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Wed, 7 Sep 2022 10:07:16 +0200 Subject: [PATCH] Fix setting devices/groups to file via environment variables not working. #7807 --- lib/util/settings.ts | 2 +- test/settings.test.js | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/util/settings.ts b/lib/util/settings.ts index 841d521e6..98346b8df 100644 --- a/lib/util/settings.ts +++ b/lib/util/settings.ts @@ -309,6 +309,7 @@ export function validate(): string[] { function read(): Settings { const s = yaml.read(file) as Settings; + applyEnvironmentVariables(s); // Read !secret MQTT username and password if set // eslint-disable-next-line @@ -416,7 +417,6 @@ function applyEnvironmentVariables(settings: Partial): void { function getInternalSettings(): Partial { if (!_settings) { _settings = read(); - applyEnvironmentVariables(_settings); } return _settings; diff --git a/test/settings.test.js b/test/settings.test.js index 9ce1d4c2b..e7dbaffb3 100644 --- a/test/settings.test.js +++ b/test/settings.test.js @@ -67,21 +67,41 @@ describe('Settings', () => { it('Should apply environment variables', () => { process.env['ZIGBEE2MQTT_CONFIG_SERIAL_DISABLE_LED'] = 'true'; process.env['ZIGBEE2MQTT_CONFIG_ADVANCED_SOFT_RESET_TIMEOUT'] = 1; - process.env['ZIGBEE2MQTT_CONFIG_ADVANCED_OUTPUT'] = 'csvtest'; + process.env['ZIGBEE2MQTT_CONFIG_ADVANCED_OUTPUT'] = 'attribute_and_json'; + process.env['ZIGBEE2MQTT_CONFIG_ADVANCED_LOG_OUTPUT'] = '["console"]'; process.env['ZIGBEE2MQTT_CONFIG_MAP_OPTIONS_GRAPHVIZ_COLORS_FILL'] = '{"enddevice": "#ff0000", "coordinator": "#00ff00", "router": "#0000ff"}'; process.env['ZIGBEE2MQTT_CONFIG_MQTT_BASE_TOPIC'] = 'testtopic'; + process.env['ZIGBEE2MQTT_CONFIG_MQTT_SERVER'] = 'testserver'; process.env['ZIGBEE2MQTT_CONFIG_ADVANCED_NETWORK_KEY'] = 'GENERATE'; + process.env['ZIGBEE2MQTT_CONFIG_DEVICES'] = 'devices.yaml'; + + const contentDevices = { + '0x00158d00018255df': { + friendly_name: '0x00158d00018255df', + retain: false, + }, + }; write(configurationFile, {}); + write(devicesFile, contentDevices); + expect(settings.validate()).toStrictEqual([]); + const s = settings.get(); const expected = objectAssignDeep.noMutate({}, settings.testing.defaults); - expected.devices = {}; + expected.devices = { + '0x00158d00018255df': { + friendly_name: '0x00158d00018255df', + retain: false, + }, + }; expected.groups = {}; expected.serial.disable_led = true; expected.advanced.soft_reset_timeout = 1; - expected.advanced.output = 'csvtest'; + expected.advanced.log_output = ["console"]; + expected.advanced.output = 'attribute_and_json'; expected.map_options.graphviz.colors.fill = {enddevice: '#ff0000', coordinator: '#00ff00', router: '#0000ff'}; expected.mqtt.base_topic = 'testtopic'; + expected.mqtt.server = 'testserver'; expected.advanced.network_key = 'GENERATE'; expect(s).toStrictEqual(expected);