UNPKG

node-miio

Version:

Control Mi Home devices, such as Mi Robot Vacuums, Mi Air Purifiers, Mi Smart Home Gateway (Aqara) and more

67 lines (54 loc) 1.6 kB
'use strict'; const network = require('./network'); const Device = require('./device'); const Placeholder = require('./placeholder'); const models = require('./models'); const Vacuum = require('./devices/vacuums/vacuum'); const ViomiVacuum = require('./devices/vacuums/viomivacuum'); const DreameVacuum = require('./devices/vacuums/dreamevacuum'); module.exports = function(options) { let handle = network.ref(); // Connecting to a device via IP, ask the network if it knows about it return network.findDeviceViaAddress(options) .then(device => { const deviceHandle = { ref: network.ref(), api: device }; // Try to resolve the correct model, otherwise use the generic device let d = models[device.model]; // Hack to accept any vacuum in the form of 'WORD.vacuum.*' if (!d && device.model.match(/^\w+\.vacuum\./)) { d = Vacuum; if (device.model.startsWith('viomi.')) { d = ViomiVacuum; } if (device.model.startsWith('dreame.')) { d = DreameVacuum; } } if(! d) { return new Device(deviceHandle); } else { return new d(deviceHandle); } }) .catch(e => { if((e.code === 'missing-token' || e.code === 'connection-failure') && options.withPlaceholder) { const deviceHandle = { ref: network.ref(), api: e.device }; return new Placeholder(deviceHandle); } // Error handling - make sure to always release the handle handle.release(); e.device = null; throw e; }) .then(device => { // Make sure to release the handle handle.release(); return device.init(); }); };