From a19cf7f5ede25bc1adc727e761052a5df788a6e5 Mon Sep 17 00:00:00 2001 From: Andreas Holstenson Date: Sat, 27 Jan 2018 15:03:30 +0100 Subject: [PATCH] Adding available methods to miio inspect --- cli/commands/inspect.js | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/cli/commands/inspect.js b/cli/commands/inspect.js index 01b4f61..dd1ab6f 100644 --- a/cli/commands/inspect.js +++ b/cli/commands/inspect.js @@ -4,6 +4,27 @@ const chalk = require('chalk'); const log = require('../log'); const deviceFinder = require('../device-finder'); +const GROUPS = [ + { name: 'Power', tags: [ 'cap:power', 'cap:switchable-power' ] }, + { name: 'Mode', tags: [ 'cap:mode', 'cap:switchable-mode' ] }, + { name: 'Sensor', tags: [ + 'type:sensor', + 'cap:temperature', + 'cap:relativeHumidity', + 'cap:pressure', + 'cap:pm2.5', + 'cap:illuminance', + 'cap:contact', + 'cap:motion', + ] }, + { name: 'Brightness', tags: [ 'cap:brightness', 'cap:dimmable' ] }, + { name: 'Color', tags: [ 'cap:colorable' ] }, + { name: 'LED', tags: [ 'cap:miio:switchable-led', 'cap:miio:led-brightness' ] }, + { name: 'Buzzer', tags: [ 'cap:miio:buzzer' ] }, + { name: 'Children', tags: [ 'cap:children' ] }, + { name: 'miIO', tags: [ 'type:miio' ]} +]; + exports.command = 'inspect '; exports.description = 'Inspect a device'; exports.builder = { @@ -73,6 +94,7 @@ exports.handler = function(argv) { for(const key of keys) { log.plain(' -', key + ':', props[key]); } + log.plain(); } if(mgmt.parent) { @@ -81,6 +103,56 @@ exports.handler = function(argv) { log.device(mgmt.parent); }); } + + log.plain(chalk.bold('Actions:')); + log.group(() => { + const actions = device.metadata.actions; + const handled = new Set(); + for(const group of GROUPS) { + let seenTags = new Set(); + let actionsToPrint = []; + + /* + * Go through all actions and collect those that + * belong to this group. + */ + for(const name of Object.keys(actions)) { + if(handled.has(name)) continue; + const action = actions[name]; + + for(const t of group.tags) { + if(action.tags.indexOf(t) >= 0) { + seenTags.add(t); + actionsToPrint.push(action); + break; + } + } + } + + if(actionsToPrint.length > 0) { + log.plain(chalk.bold(group.name), '-', Array.from(seenTags).join(', ')); + + for(const action of actionsToPrint) { + printAction(action); + handled.add(action.name); + } + + log.plain(); + } + } + + let isFirst = true; + for(const name of Object.keys(actions)) { + if(handled.has(name)) continue; + + if(isFirst) { + log.plain(chalk.bold('Other actions')); + isFirst = false; + } + + printAction(actions[name]); + } + }); }) .catch(err => { log.error('Could inspect device. Error was:', err.message); @@ -102,3 +174,24 @@ exports.handler = function(argv) { setTimeout(doneHandler, 5000); browser.on('done', doneHandler); }; + +function printAction(action) { + log.group(() => { + let args = action.name; + for(const arg of action.arguments) { + args += ' '; + + if(arg.optional) { + args += '['; + } + + args += arg.type; + + if(arg.optional) { + args += ']'; + } + } + + log.plain(args); + }); +}