()
| 204 | } |
| 205 | |
| 206 | export function write(): void { |
| 207 | const settings = getPersistedSettings(); |
| 208 | const toWrite: KeyValue = objectAssignDeep({}, settings); |
| 209 | |
| 210 | // Read settings to check if we have to split devices/groups into separate file. |
| 211 | const actual = yaml.read(CONFIG_FILE_PATH); |
| 212 | |
| 213 | // In case the setting is defined in a separate file (e.g. !secret network_key) update it there. |
| 214 | for (const [ns, key] of [ |
| 215 | ["mqtt", "server"], |
| 216 | ["mqtt", "user"], |
| 217 | ["mqtt", "password"], |
| 218 | ["advanced", "network_key"], |
| 219 | ["frontend", "auth_token"], |
| 220 | ]) { |
| 221 | if (actual[ns]?.[key]) { |
| 222 | const ref = parseValueRef(actual[ns][key]); |
| 223 | if (ref) { |
| 224 | yaml.updateIfChanged(data.joinPath(ref.filename), ref.key, toWrite[ns][key]); |
| 225 | toWrite[ns][key] = actual[ns][key]; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Write devices/groups to separate file if required. |
| 231 | const writeDevicesOrGroups = (type: "devices" | "groups"): void => { |
| 232 | if (typeof actual[type] === "string" || (Array.isArray(actual[type]) && actual[type].length > 0)) { |
| 233 | const fileToWrite = Array.isArray(actual[type]) ? actual[type][0] : actual[type]; |
| 234 | const content = objectAssignDeep({}, settings[type]); |
| 235 | |
| 236 | // If an array, only write to first file and only devices which are not in the other files. |
| 237 | if (Array.isArray(actual[type])) { |
| 238 | // skip i==0 |
| 239 | for (let i = 1; i < actual[type].length; i++) { |
| 240 | for (const key in yaml.readIfExists(data.joinPath(actual[type][i]))) { |
| 241 | delete content[key]; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | yaml.writeIfChanged(data.joinPath(fileToWrite), content); |
| 247 | toWrite[type] = actual[type]; |
| 248 | } |
| 249 | }; |
| 250 | |
| 251 | writeDevicesOrGroups("devices"); |
| 252 | writeDevicesOrGroups("groups"); |
| 253 | |
| 254 | applyEnvironmentVariables(toWrite); |
| 255 | |
| 256 | yaml.writeIfChanged(CONFIG_FILE_PATH, toWrite); |
| 257 | |
| 258 | _settings = read(); |
| 259 | |
| 260 | loadSettingsWithDefaults(); |
| 261 | } |
| 262 | |
| 263 | export function validate(): string[] { |
no test coverage detected