mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-24 11:39:53 +00:00
Lazy load settings on first use
This commit is contained in:
+32
-7
@@ -70,15 +70,18 @@ const defaults = {
|
||||
},
|
||||
};
|
||||
|
||||
let settings = read();
|
||||
let _settings;
|
||||
|
||||
const getSettings = () => _settings||(_settings = read());
|
||||
|
||||
function writeRead() {
|
||||
write();
|
||||
settings = read();
|
||||
_settings = read();
|
||||
onChangeHandlers.forEach((handler) => handler());
|
||||
}
|
||||
|
||||
function write() {
|
||||
const settings = getSettings();
|
||||
const toWrite = objectAssignDeep.noMutate(settings);
|
||||
|
||||
// Read settings to check if we have to split devices/groups into seperate file.
|
||||
@@ -122,7 +125,7 @@ function read() {
|
||||
}
|
||||
|
||||
function set(path, value) {
|
||||
let obj = settings;
|
||||
let obj = getSettings();
|
||||
|
||||
for (let i = 0; i < path.length; i++) {
|
||||
const key = path[i];
|
||||
@@ -140,7 +143,23 @@ function set(path, value) {
|
||||
writeRead();
|
||||
}
|
||||
|
||||
const getDevice = (ieeeAddr) => {
|
||||
const settings = getSettings();
|
||||
return settings.devices ? settings.devices[ieeeAddr] : null;
|
||||
};
|
||||
|
||||
const getGroup = (ID) => {
|
||||
const settings = getSettings();
|
||||
return settings.groups ? settings.groups[ID]: null;
|
||||
};
|
||||
|
||||
const getDevices = () => {
|
||||
const settings = getSettings();
|
||||
return settings.devices ? settings.devices : [];
|
||||
};
|
||||
|
||||
function addDevice(ieeeAddr) {
|
||||
const settings = getSettings();
|
||||
if (!settings.devices) {
|
||||
settings.devices = {};
|
||||
}
|
||||
@@ -150,6 +169,7 @@ function addDevice(ieeeAddr) {
|
||||
}
|
||||
|
||||
function removeDevice(ieeeAddr) {
|
||||
const settings = getSettings();
|
||||
if (settings.devices && settings.devices[ieeeAddr]) {
|
||||
delete settings.devices[ieeeAddr];
|
||||
writeRead();
|
||||
@@ -157,6 +177,7 @@ function removeDevice(ieeeAddr) {
|
||||
}
|
||||
|
||||
function getIeeeAddrByFriendlyName(friendlyName) {
|
||||
const settings = getSettings();
|
||||
if (!settings.devices) {
|
||||
return null;
|
||||
}
|
||||
@@ -167,6 +188,7 @@ function getIeeeAddrByFriendlyName(friendlyName) {
|
||||
}
|
||||
|
||||
function getGroupIDByFriendlyName(friendlyName) {
|
||||
const settings = getSettings();
|
||||
if (!settings.groups) {
|
||||
return null;
|
||||
}
|
||||
@@ -177,6 +199,7 @@ function getGroupIDByFriendlyName(friendlyName) {
|
||||
}
|
||||
|
||||
function changeDeviceOptions(ieeeAddr, newOptions) {
|
||||
const settings = getSettings();
|
||||
const currentOptions = settings.devices[ieeeAddr];
|
||||
|
||||
if (!currentOptions) {
|
||||
@@ -193,6 +216,7 @@ function changeDeviceOptions(ieeeAddr, newOptions) {
|
||||
}
|
||||
|
||||
function changeFriendlyName(old, new_) {
|
||||
const settings = getSettings();
|
||||
const ieeeAddr = getIeeeAddrByFriendlyName(old);
|
||||
|
||||
if (!ieeeAddr) {
|
||||
@@ -234,13 +258,13 @@ function resolveEntity(ID) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get: () => objectAssignDeep.noMutate(defaults, settings),
|
||||
get: () => objectAssignDeep.noMutate(defaults, getSettings()),
|
||||
write: () => write(),
|
||||
set: (path, value) => set(path, value),
|
||||
|
||||
getDevice: (ieeeAddr) => settings.devices ? settings.devices[ieeeAddr] : null,
|
||||
getGroup: (ID) => settings.groups ? settings.groups[ID]: null,
|
||||
getDevices: () => settings.devices ? settings.devices : [],
|
||||
getDevice,
|
||||
getGroup,
|
||||
getDevices,
|
||||
addDevice: (ieeeAddr) => addDevice(ieeeAddr),
|
||||
removeDevice: (ieeeAddr) => removeDevice(ieeeAddr),
|
||||
|
||||
@@ -256,4 +280,5 @@ module.exports = {
|
||||
_getDefaults: () => {
|
||||
return objectAssignDeep.noMutate(defaults);
|
||||
},
|
||||
_clear: () => _settings = undefined,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user