2018-10-02 12:15:12 -07:00
|
|
|
const utils = require('../util/utils');
|
|
|
|
const interval = utils.secondsToMilliseconds(60);
|
2019-03-24 06:46:38 -07:00
|
|
|
const logger = require('../util/logger');
|
|
|
|
const settings = require('../util/settings');
|
2018-10-02 12:15:12 -07:00
|
|
|
|
|
|
|
/**
|
2019-02-13 12:55:14 -07:00
|
|
|
* This extensions handles Xiaomi devices.
|
|
|
|
* - Marks Xiaomi devices as online.
|
|
|
|
* - Polls Xiaomi routers to keep them awake.
|
2018-10-02 12:15:12 -07:00
|
|
|
*/
|
2019-02-13 12:55:14 -07:00
|
|
|
class Xiaomi {
|
2019-02-04 10:36:49 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
2018-10-02 12:15:12 -07:00
|
|
|
this.zigbee = zigbee;
|
|
|
|
this.timer = null;
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2018-10-02 12:15:12 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onZigbeeStarted() {
|
2019-02-13 12:55:14 -07:00
|
|
|
// Set all Xiaomi devices to be online, so shepherd won't try
|
|
|
|
// to query info from devices (which would fail because they go to sleep).
|
|
|
|
const devices = this.zigbee.getAllClients();
|
|
|
|
devices.forEach((d) => {
|
|
|
|
if (utils.isXiaomiDevice(d)) {
|
|
|
|
const device = this.zigbee.shepherd.find(d.ieeeAddr, 1);
|
|
|
|
if (device) {
|
|
|
|
device.getDevice().update({
|
|
|
|
status: 'online',
|
|
|
|
joinTime: Math.floor(Date.now() / 1000),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-24 06:46:38 -07:00
|
|
|
/**
|
|
|
|
* Device availability already pings Xiaomi devices.
|
|
|
|
*/
|
|
|
|
if (!settings.get().advanced.availability_timeout) {
|
|
|
|
this.startTimer();
|
|
|
|
} else {
|
|
|
|
logger.debug('Disabled pinging of Xiaomi devices in Xiaomi extension because availability is enabled');
|
|
|
|
}
|
2018-10-02 12:15:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
startTimer() {
|
|
|
|
this.clearTimer();
|
|
|
|
this.timer = setInterval(() => this.handleInterval(), interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimer() {
|
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
this.clearTimer();
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:19:20 -07:00
|
|
|
ping(ieeeAddr) {
|
2019-03-23 09:45:28 -07:00
|
|
|
this.zigbee.ping(ieeeAddr, 'error', null, 'basic');
|
2019-01-26 13:19:20 -07:00
|
|
|
}
|
|
|
|
|
2018-10-02 12:15:12 -07:00
|
|
|
handleInterval() {
|
|
|
|
this.zigbee.getAllClients()
|
2019-03-24 06:38:08 -07:00
|
|
|
.filter((d) => utils.isXiaomiDevice(d) && utils.isRouter(d) && !utils.isBatteryPowered(d))
|
|
|
|
.forEach((d) => this.ping(d.ieeeAddr));
|
2018-10-02 12:15:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:55:14 -07:00
|
|
|
module.exports = Xiaomi;
|