The myq-api lib I was using is still not updated to the new API, and as a result doesn't work. This lib is recent and seems to be powering an active project. I'm sure this isn't exactly how it needs to be done long-term, but this gets things functional for me right now and I can iterate on it as I go.
78 lines
2.2 KiB
JavaScript
78 lines
2.2 KiB
JavaScript
/* Magic Mirror
|
|
* Module: MMM-MyQ
|
|
*
|
|
* By parnic https://github.com/parnic/MMM-MyQ
|
|
* MIT Licensed.
|
|
*/
|
|
|
|
const NodeHelper = require('node_helper');
|
|
const MyQ = require('@hjdhjd/myq');
|
|
|
|
module.exports = NodeHelper.create({
|
|
_customActions: {
|
|
open: 'open',
|
|
close: 'close'
|
|
},
|
|
|
|
start() {
|
|
console.log(`Starting module helper: ${this.name}`);
|
|
},
|
|
|
|
resetUpdates() {
|
|
if (this.intervalId) {
|
|
clearInterval(this.intervalId);
|
|
}
|
|
|
|
this.intervalId = setInterval(() => {
|
|
this.getData();
|
|
}, this.config.updateInterval);
|
|
},
|
|
|
|
socketNotificationReceived(notification, payload) {
|
|
if (notification === 'MYQ_CONFIG') {
|
|
this.config = payload;
|
|
this.account = new MyQ.myQApi(this.config.email, this.config.password);
|
|
this.sendSocketNotification('MYQ_LOGGED_IN', this._customActions);
|
|
this.getData();
|
|
} else if (this.config) {
|
|
if (notification === 'MYQ_TOGGLE') {
|
|
const {device, action} = payload;
|
|
this.execute(device, action);
|
|
} else if (notification === 'MYQ_UPDATE') {
|
|
this.getData();
|
|
}
|
|
}
|
|
},
|
|
|
|
getData() {
|
|
this.resetUpdates();
|
|
this.refreshDevices();
|
|
},
|
|
|
|
async refreshDevices() {
|
|
let result = await this.account.refreshDevices();
|
|
if (!result) {
|
|
this.handleError(result, 'refreshDevices returned false');
|
|
return;
|
|
}
|
|
|
|
let devices = this.account.devices.filter((device) => this.config.types.includes(device.device_type));
|
|
this.sendSocketNotification('MYQ_DEVICES_FOUND', devices);
|
|
},
|
|
|
|
async execute(device, action) {
|
|
let result = await this.account.execute(device, action);
|
|
if (result) {
|
|
this.sendSocketNotification('MYQ_TOGGLE_COMPLETE', result);
|
|
} else {
|
|
this.handleError(error, 'execute');
|
|
}
|
|
},
|
|
|
|
handleError(error, context) {
|
|
let err = `${context} failed with code ${error.code}`;
|
|
this.sendSocketNotification('MYQ_ERROR', {context, err});
|
|
console.error(`${this.name} ${context} error: ${err}`);
|
|
}
|
|
});
|