Refactor device reconfigure when device comes online and mark online. #966 #775

This commit is contained in:
Koen Kanters 2019-01-31 21:32:18 +01:00
parent 588124b971
commit d5c6e39e7d
4 changed files with 32 additions and 27 deletions

View File

@ -21,6 +21,7 @@ class DeviceAvailabilityHandler {
this.availability_timeout = settings.get().advanced.availability_timeout; this.availability_timeout = settings.get().advanced.availability_timeout;
this.timers = {}; this.timers = {};
this.pending = []; this.pending = [];
this.state = {};
/** /**
* Setup command queue. * Setup command queue.
@ -106,6 +107,7 @@ class DeviceAvailabilityHandler {
} }
publishAvailability(ieeeAddr, available) { publishAvailability(ieeeAddr, available) {
this.state[ieeeAddr] = available;
const deviceSettings = settings.getDevice(ieeeAddr); const deviceSettings = settings.getDevice(ieeeAddr);
const name = deviceSettings ? deviceSettings.friendly_name : ieeeAddr; const name = deviceSettings ? deviceSettings.friendly_name : ieeeAddr;
const topic = `${name}/availability`; const topic = `${name}/availability`;
@ -113,35 +115,17 @@ class DeviceAvailabilityHandler {
this.mqtt.publish(topic, payload, {retain: true, qos: 0}); this.mqtt.publish(topic, payload, {retain: true, qos: 0});
} }
configure(device, mappedDevice) {
const ieeeAddr = device.ieeeAddr;
const friendlyName = settings.getDevice(ieeeAddr) ? settings.getDevice(ieeeAddr).friendly_name : 'unknown';
logger.debug(`Configuring ${friendlyName} (${ieeeAddr}) ...`);
// Call configure function of this device.
mappedDevice.configure(ieeeAddr, this.zigbee.shepherd, this.zigbee.getCoordinator(), (ok, msg) => {
if (ok) {
logger.info(`Succesfully configured ${friendlyName} (${ieeeAddr})`);
} else {
logger.error(`Failed to configure ${friendlyName} (${ieeeAddr}) ('${msg}')`);
}
});
}
onZigbeeMessage(message, device, mappedDevice) { onZigbeeMessage(message, device, mappedDevice) {
// When a zigbee message from a device is received we know the device is still alive. // When a zigbee message from a device is received we know the device is still alive.
// => reset the timer. // => reset the timer.
if (device && this.isPingable(this.zigbee.getDevice(device.ieeeAddr))) { if (device && this.isPingable(this.zigbee.getDevice(device.ieeeAddr))) {
// When a message is received and the device is marked as offline, mark it online.
if (this.state.hasOwnProperty(device.ieeeAddr) && !this.state[device.ieeeAddr]) {
this.publishAvailability(device.ieeeAddr, true);
}
this.setTimer(device.ieeeAddr); this.setTimer(device.ieeeAddr);
} }
if (message.type == 'devIncoming' || message.type == 'endDeviceAnnce') {
logger.info('Announcement: Device incoming...');
//check device for post announce handler
if(device && mappedDevice && mappedDevice.configure) {
//logger.info('Announcement: need to re-configure');
this.configure(device,mappedDevice);
}
}
} }
} }

View File

@ -23,10 +23,31 @@ class DeviceConfigure {
onZigbeeMessage(message, device, mappedDevice) { onZigbeeMessage(message, device, mappedDevice) {
if (device && mappedDevice) { if (device && mappedDevice) {
// endDeviceAnnce is typically send when a device comes online after being
// powered off. In some cases this requires a re-configure of the device.
// Mark this device as not configured.
// https://github.com/Koenkk/zigbee2mqtt/issues/966
if (message.type === 'endDeviceAnnce') {
this.mark(device.ieeeAddr, false);
}
this.configure(device, mappedDevice); this.configure(device, mappedDevice);
} }
} }
mark(ieeeAddr, configured) {
if (configured) {
if (!this.configured.includes(ieeeAddr)) {
this.configured.push(ieeeAddr);
}
} else {
const index = this.configured.indexOf(ieeeAddr);
if (index > -1) {
this.configured.splice(index, 1);
}
}
}
configure(device, mappedDevice) { configure(device, mappedDevice) {
const ieeeAddr = device.ieeeAddr; const ieeeAddr = device.ieeeAddr;
@ -44,7 +65,7 @@ class DeviceConfigure {
}); });
// Mark as configured // Mark as configured
this.configured.push(ieeeAddr); this.mark(ieeeAddr, true);
} }
} }
} }

4
npm-shrinkwrap.json generated
View File

@ -4803,8 +4803,8 @@
} }
}, },
"zigbee-shepherd": { "zigbee-shepherd": {
"version": "git+https://github.com/Koenkk/zigbee-shepherd.git#94debb60dfe8b85632708990dfe97913d1474a53", "version": "git+https://github.com/Koenkk/zigbee-shepherd.git#6f06ba01d7dbe13dc5a3b0652cf4ad01debbf188",
"from": "git+https://github.com/Koenkk/zigbee-shepherd.git#94debb60dfe8b85632708990dfe97913d1474a53", "from": "git+https://github.com/Koenkk/zigbee-shepherd.git#6f06ba01d7dbe13dc5a3b0652cf4ad01debbf188",
"requires": { "requires": {
"areq": "^0.2.0", "areq": "^0.2.0",
"busyman": "^0.3.0", "busyman": "^0.3.0",

View File

@ -45,7 +45,7 @@
"semver": "*", "semver": "*",
"winston": "2.4.2", "winston": "2.4.2",
"ziee": "*", "ziee": "*",
"zigbee-shepherd": "git+https://github.com/Koenkk/zigbee-shepherd.git#94debb60dfe8b85632708990dfe97913d1474a53", "zigbee-shepherd": "git+https://github.com/Koenkk/zigbee-shepherd.git#6f06ba01d7dbe13dc5a3b0652cf4ad01debbf188",
"zigbee-shepherd-converters": "7.0.27", "zigbee-shepherd-converters": "7.0.27",
"zive": "*" "zive": "*"
}, },