Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added xiaomi smart plug chuangmi.plug.212a01 support #306

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cli/commands/protocol/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ exports.handler = function(argv) {
log.info('Device found, making call');
log.plain();

const parsedArgs = argv.params ? JSON.parse(argv.params) : [];
let parsedArgs = [];
if (argv.params) {
try {
parsedArgs = JSON.parse(argv.params);
// TODO check it's an array
} catch(e) {
log.error('params should be a valid JSON-string of array');
process.exit(0); // eslint-disable-line
}
}


device.miioCall(argv.method, parsedArgs)
.then(result => {
log.info('Got result:');
Expand Down
43 changes: 43 additions & 0 deletions lib/devices/power-plug212a01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const { Thing } = require('abstract-things');
const { PowerPlug, PowerOutlet } = require('abstract-things/electrical');
const MiioApi = require('../device');
const Power = require('./capabilities/power');

module.exports = class extends Thing
.with(PowerPlug, PowerOutlet, MiioApi, Power)
{
static get type() {
return 'miio:power-plug212a01';
}

constructor(options) {
super(options);

this.defineProperty('power');
}

changePower(power) {
return this.call(
'set_properties',
[{ did: 'MYDID', siid: 2, piid: 1, value: Boolean(power) }],
{ refresh: true }
);
}

propertyUpdated(key, value) {
if (key === 'power') {
this.changePower(value);
}
}

loadProperties(props) {
return this.call('get_properties', [{ did: 'MYDID', siid: 2, piid: 1 }])
.then(result => {
const mapped = {};
this._pushProperty(mapped, 'power', result[0].value);
return mapped;
});
}
};
2 changes: 2 additions & 0 deletions lib/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Gateway = require('./devices/gateway');
const Vacuum = require('./devices/vacuum');

const PowerPlug = require('./devices/power-plug');
const PowerPlug212a01 = require('./devices/power-plug212a01');
const PowerStrip = require('./devices/power-strip');

const Humidifier = require('./devices/humidifier');
Expand Down Expand Up @@ -38,6 +39,7 @@ module.exports = {
'chuangmi.plug.m1': PowerPlug,
'chuangmi.plug.v1': require('./devices/chuangmi.plug.v1'),
'chuangmi.plug.v2': PowerPlug,
'chuangmi.plug.212a01': PowerPlug212a01,

'rockrobo.vacuum.v1': Vacuum,
'roborock.vacuum.s5': Vacuum,
Expand Down