MCPcopy
hub / github.com/Koenkk/zigbee2mqtt / write

Function write

lib/util/settings.ts:206–261  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

204}
205
206export 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
263export function validate(): string[] {

Callers 10

setOnboardingFunction · 0.70
setFunction · 0.70
applyFunction · 0.70
addDeviceFunction · 0.70
blockDeviceFunction · 0.70
removeDeviceFunction · 0.70
addGroupFunction · 0.70
removeGroupFunction · 0.70
changeEntityOptionsFunction · 0.70
changeFriendlyNameFunction · 0.70

Calls 6

getPersistedSettingsFunction · 0.85
parseValueRefFunction · 0.85
writeDevicesOrGroupsFunction · 0.85
loadSettingsWithDefaultsFunction · 0.85
readFunction · 0.70

Tested by

no test coverage detected