2018-05-25 07:42:29 -07:00
|
|
|
const yaml = require('js-yaml');
|
2018-04-25 10:29:03 -07:00
|
|
|
const fs = require('fs');
|
2018-05-11 20:04:15 -07:00
|
|
|
const data = require('./data');
|
2018-05-12 01:58:06 -07:00
|
|
|
const file = data.joinPath('configuration.yaml');
|
2018-09-18 08:51:34 -07:00
|
|
|
const objectAssignDeep = require(`object-assign-deep`);
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const defaults = {
|
2018-09-22 15:07:31 -07:00
|
|
|
permit_join: false,
|
2018-11-16 12:23:11 -07:00
|
|
|
mqtt: {
|
|
|
|
include_device_information: false,
|
|
|
|
},
|
2018-12-21 16:07:53 -07:00
|
|
|
groups: {},
|
2018-12-19 09:33:02 -07:00
|
|
|
device_options: {},
|
2018-09-18 08:51:34 -07:00
|
|
|
advanced: {
|
|
|
|
log_directory: path.join(data.getPath(), 'log', '%TIMESTAMP%'),
|
|
|
|
log_level: process.env.DEBUG ? 'debug' : 'info',
|
2018-10-02 12:15:12 -07:00
|
|
|
soft_reset_timeout: 0,
|
2018-11-16 12:23:11 -07:00
|
|
|
pan_id: 0x1a62,
|
|
|
|
channel: 11,
|
|
|
|
baudrate: 115200,
|
|
|
|
rtscts: true,
|
|
|
|
|
2019-01-29 12:17:56 -07:00
|
|
|
// Availability timeout in seconds, disabled by default.
|
|
|
|
availability_timeout: 0,
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
/**
|
|
|
|
* Home Assistant requires ALL attributes to be present in ALL MQTT messages send by the device.
|
|
|
|
* https://community.home-assistant.io/t/missing-value-with-mqtt-only-last-data-set-is-shown/47070/9
|
|
|
|
*
|
|
|
|
* Therefore zigbee2mqtt BY DEFAULT caches all values and resend it with every message.
|
|
|
|
* advanced.cache_state in configuration.yaml allows to configure this.
|
2018-12-14 11:37:57 -07:00
|
|
|
* https://koenkk.github.io/zigbee2mqtt/configuration/configuration.html
|
2018-11-16 12:23:11 -07:00
|
|
|
*/
|
|
|
|
cache_state: true,
|
2018-12-24 08:29:06 -07:00
|
|
|
|
2019-01-18 12:31:55 -07:00
|
|
|
/**
|
|
|
|
* Add a last_seen attribute to mqtt messages, contains date/time of zigbee message arrival
|
|
|
|
* "ISO_8601": ISO 8601 format
|
|
|
|
* "epoch": milliseconds elapsed since the UNIX epoch
|
|
|
|
* "disable": no last_seen attribute (default)
|
|
|
|
*/
|
|
|
|
last_seen: 'disable',
|
|
|
|
|
2019-01-22 12:02:34 -07:00
|
|
|
// Optional: Add an elapsed attribute to MQTT messages, contains milliseconds since the previous msg
|
|
|
|
elapsed: false,
|
|
|
|
|
2018-12-24 08:29:06 -07:00
|
|
|
/**
|
|
|
|
* https://github.com/Koenkk/zigbee2mqtt/issues/685#issuecomment-449112250
|
|
|
|
*
|
|
|
|
* Network key will serve as the encryption key of your network.
|
|
|
|
* Changing this will require you to repair your devices.
|
|
|
|
*/
|
|
|
|
network_key: [1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 13],
|
2018-09-18 08:51:34 -07:00
|
|
|
},
|
|
|
|
};
|
2018-05-12 01:58:06 -07:00
|
|
|
|
2018-05-25 07:42:29 -07:00
|
|
|
let settings = read();
|
2018-04-18 09:25:40 -07:00
|
|
|
|
2018-05-17 08:20:46 -07:00
|
|
|
function writeRead() {
|
2018-05-12 06:54:02 -07:00
|
|
|
write();
|
2018-05-25 07:42:29 -07:00
|
|
|
settings = read();
|
2018-05-12 06:54:02 -07:00
|
|
|
}
|
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
function write() {
|
2018-05-25 07:42:29 -07:00
|
|
|
fs.writeFileSync(file, yaml.safeDump(settings));
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function read() {
|
2018-05-25 07:42:29 -07:00
|
|
|
return yaml.safeLoad(fs.readFileSync(file, 'utf8'));
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
function addDevice(ieeeAddr) {
|
2018-05-25 07:42:29 -07:00
|
|
|
if (!settings.devices) {
|
|
|
|
settings.devices = {};
|
2018-04-25 10:29:03 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
settings.devices[ieeeAddr] = {friendly_name: ieeeAddr, retain: false};
|
2018-05-17 08:20:46 -07:00
|
|
|
writeRead();
|
2018-04-25 10:29:03 -07:00
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
function removeDevice(ieeeAddr) {
|
|
|
|
if (settings.devices && settings.devices[ieeeAddr]) {
|
|
|
|
delete settings.devices[ieeeAddr];
|
2018-06-07 10:41:11 -07:00
|
|
|
writeRead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
function getIeeeAddrByFriendlyName(friendlyName) {
|
2018-06-09 03:27:04 -07:00
|
|
|
if (!settings.devices) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
return Object.keys(settings.devices).find((ieeeAddr) =>
|
|
|
|
settings.devices[ieeeAddr].friendly_name === friendlyName
|
2018-06-09 03:27:04 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-21 16:07:53 -07:00
|
|
|
function getGroupIDByFriendlyName(friendlyName) {
|
|
|
|
if (!settings.groups) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(settings.groups).find((ID) =>
|
|
|
|
settings.groups[ID].friendly_name === friendlyName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-24 09:25:16 -07:00
|
|
|
function changeFriendlyName(old, new_) {
|
2018-11-16 12:23:11 -07:00
|
|
|
const ieeeAddr = getIeeeAddrByFriendlyName(old);
|
2018-07-24 09:25:16 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
if (!ieeeAddr) {
|
2018-07-24 09:25:16 -07:00
|
|
|
return false;
|
2018-07-21 09:15:56 -07:00
|
|
|
}
|
2018-07-24 09:25:16 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
settings.devices[ieeeAddr].friendly_name = new_;
|
2018-07-21 09:15:56 -07:00
|
|
|
writeRead();
|
2018-07-24 09:25:16 -07:00
|
|
|
return true;
|
2018-07-21 09:15:56 -07:00
|
|
|
}
|
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
module.exports = {
|
2018-09-18 08:51:34 -07:00
|
|
|
get: () => objectAssignDeep.noMutate(defaults, settings),
|
2018-04-18 11:53:22 -07:00
|
|
|
write: () => write(),
|
2018-11-16 12:23:11 -07:00
|
|
|
|
|
|
|
getDevice: (ieeeAddr) => settings.devices ? settings.devices[ieeeAddr] : null,
|
|
|
|
addDevice: (ieeeAddr) => addDevice(ieeeAddr),
|
|
|
|
removeDevice: (ieeeAddr) => removeDevice(ieeeAddr),
|
|
|
|
|
|
|
|
getIeeeAddrByFriendlyName: (friendlyName) => getIeeeAddrByFriendlyName(friendlyName),
|
2018-12-21 16:07:53 -07:00
|
|
|
getGroupIDByFriendlyName: (friendlyName) => getGroupIDByFriendlyName(friendlyName),
|
2018-07-24 09:25:16 -07:00
|
|
|
changeFriendlyName: (old, new_) => changeFriendlyName(old, new_),
|
2018-05-17 08:20:46 -07:00
|
|
|
};
|