Skip to content

Commit

Permalink
Adding available methods to miio inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jan 27, 2018
1 parent 1a05154 commit a19cf7f
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions cli/commands/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <idOrIp>';
exports.description = 'Inspect a device';
exports.builder = {
Expand Down Expand Up @@ -73,6 +94,7 @@ exports.handler = function(argv) {
for(const key of keys) {
log.plain(' -', key + ':', props[key]);
}
log.plain();
}

if(mgmt.parent) {
Expand All @@ -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);
Expand All @@ -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);
});
}

0 comments on commit a19cf7f

Please sign in to comment.