node-miio 0.0.0-dev-20250810-f1674d0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +21 -0
- package/.eslintrc +32 -0
- package/.github/workflows/build.yml +50 -0
- package/.github/workflows/codecov.yml +35 -0
- package/.github/workflows/codeql.yml +24 -0
- package/.github/workflows/publish-dev-from-main.yml +92 -0
- package/.github/workflows/publish-release-to-npm.yml +48 -0
- package/CONTRIBUTING.md +104 -0
- package/LICENSE.md +20 -0
- package/README.md +216 -0
- package/cli/commands/configure.js +69 -0
- package/cli/commands/control.js +54 -0
- package/cli/commands/discover.js +36 -0
- package/cli/commands/inspect.js +197 -0
- package/cli/commands/protocol/call.js +66 -0
- package/cli/commands/protocol/json-dump.js +52 -0
- package/cli/commands/protocol/packet.js +29 -0
- package/cli/commands/protocol.js +8 -0
- package/cli/commands/tokens/update.js +58 -0
- package/cli/commands/tokens.js +8 -0
- package/cli/device-finder.js +66 -0
- package/cli/index.js +9 -0
- package/cli/log.js +89 -0
- package/docs/advanced-api.md +103 -0
- package/docs/devices/README.md +111 -0
- package/docs/devices/air-purifier.md +142 -0
- package/docs/devices/controller.md +89 -0
- package/docs/devices/gateway.md +41 -0
- package/docs/devices/humidifier.md +136 -0
- package/docs/devices/light.md +141 -0
- package/docs/devices/power-outlet.md +56 -0
- package/docs/devices/power-plug.md +69 -0
- package/docs/devices/power-strip.md +73 -0
- package/docs/devices/sensor.md +74 -0
- package/docs/devices/vacuum.md +80 -0
- package/docs/devices/wall-switch.md +62 -0
- package/docs/management.md +109 -0
- package/docs/missing-devices.md +48 -0
- package/docs/protocol.md +77 -0
- package/docs/reporting-issues.md +47 -0
- package/example.js +11 -0
- package/lib/checkResult.js +13 -0
- package/lib/connectToDevice.js +62 -0
- package/lib/device.js +290 -0
- package/lib/device_info.js +329 -0
- package/lib/device_info.test.js +337 -0
- package/lib/devices/air-monitor.js +53 -0
- package/lib/devices/air-purifier.js +162 -0
- package/lib/devices/capabilities/battery-level.js +16 -0
- package/lib/devices/capabilities/buzzer.js +78 -0
- package/lib/devices/capabilities/changeable-led-brightness.js +48 -0
- package/lib/devices/capabilities/colorable.js +14 -0
- package/lib/devices/capabilities/dimmable.js +14 -0
- package/lib/devices/capabilities/mode.js +13 -0
- package/lib/devices/capabilities/power.js +14 -0
- package/lib/devices/capabilities/sensor.js +50 -0
- package/lib/devices/capabilities/switchable-led.js +49 -0
- package/lib/devices/chuangmi.plug.v1.js +61 -0
- package/lib/devices/eyecare-lamp2.js +179 -0
- package/lib/devices/gateway/86sw1.js +28 -0
- package/lib/devices/gateway/86sw2.js +44 -0
- package/lib/devices/gateway/ctrl_ln1.js +44 -0
- package/lib/devices/gateway/ctrl_ln2.js +53 -0
- package/lib/devices/gateway/ctrl_neutral1.js +44 -0
- package/lib/devices/gateway/ctrl_neutral2.js +53 -0
- package/lib/devices/gateway/cube.js +55 -0
- package/lib/devices/gateway/developer-api.js +120 -0
- package/lib/devices/gateway/light-channel.js +32 -0
- package/lib/devices/gateway/light-mixin.js +71 -0
- package/lib/devices/gateway/magnet.js +29 -0
- package/lib/devices/gateway/magnet2.js +13 -0
- package/lib/devices/gateway/motion.js +27 -0
- package/lib/devices/gateway/motion2.js +31 -0
- package/lib/devices/gateway/plug.js +45 -0
- package/lib/devices/gateway/sensor_ht.js +17 -0
- package/lib/devices/gateway/subdevice.js +227 -0
- package/lib/devices/gateway/subdevices.js +37 -0
- package/lib/devices/gateway/switch.js +29 -0
- package/lib/devices/gateway/switch2.js +27 -0
- package/lib/devices/gateway/voltage.js +28 -0
- package/lib/devices/gateway/weather.js +22 -0
- package/lib/devices/gateway.js +244 -0
- package/lib/devices/humidifier.js +162 -0
- package/lib/devices/mijiavacuum.js +22 -0
- package/lib/devices/philips-light-bulb.js +73 -0
- package/lib/devices/power-plug.js +26 -0
- package/lib/devices/power-strip.js +38 -0
- package/lib/devices/vacuum.js +412 -0
- package/lib/devices/viomivacuum.js +250 -0
- package/lib/devices/yeelight.color.js +6 -0
- package/lib/devices/yeelight.js +286 -0
- package/lib/devices/yeelight.mono.js +9 -0
- package/lib/discovery.js +192 -0
- package/lib/index.js +40 -0
- package/lib/infoFromHostname.js +24 -0
- package/lib/management.js +102 -0
- package/lib/models.js +77 -0
- package/lib/network.js +234 -0
- package/lib/packet.js +191 -0
- package/lib/placeholder.js +12 -0
- package/lib/safeishJSON.js +14 -0
- package/lib/tokens.js +129 -0
- package/npm-shrinkwrap.json +853 -0
- package/package.json +40 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { ChargingState } = require('abstract-things');
|
|
4
|
+
const { AirMonitor } = require('abstract-things/climate');
|
|
5
|
+
const MiioApi = require('../device');
|
|
6
|
+
|
|
7
|
+
const Power = require('./capabilities/power');
|
|
8
|
+
const BatteryLevel = require('./capabilities/battery-level');
|
|
9
|
+
const { AQI } = require('./capabilities/sensor');
|
|
10
|
+
|
|
11
|
+
module.exports = class extends AirMonitor
|
|
12
|
+
.with(MiioApi, Power, AQI, BatteryLevel, ChargingState)
|
|
13
|
+
{
|
|
14
|
+
|
|
15
|
+
static get type() {
|
|
16
|
+
return 'miio:air-monitor';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
constructor(options) {
|
|
20
|
+
super(options);
|
|
21
|
+
|
|
22
|
+
// Define the power property
|
|
23
|
+
this.defineProperty('power', v => v === 'on');
|
|
24
|
+
|
|
25
|
+
// Sensor value used for AQI (PM2.5) capability
|
|
26
|
+
this.defineProperty('aqi');
|
|
27
|
+
|
|
28
|
+
this.defineProperty('battery', {
|
|
29
|
+
name: 'batteryLevel'
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
this.defineProperty('usb_state', {
|
|
33
|
+
name: 'charging',
|
|
34
|
+
mapper: v => v === 'on'
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
propertyUpdated(key, value, oldValue) {
|
|
39
|
+
if(key === 'charging') {
|
|
40
|
+
this.updateCharging(value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
super.propertyUpdated(key, value, oldValue);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
changePower(power) {
|
|
47
|
+
return this.call('set_power', [ power ? 'on' : 'off' ], {
|
|
48
|
+
refresh: [ 'power', 'mode' ],
|
|
49
|
+
refreshDelay: 200
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { AirPurifier } = require('abstract-things/climate');
|
|
4
|
+
const MiioApi = require('../device');
|
|
5
|
+
|
|
6
|
+
const Power = require('./capabilities/power');
|
|
7
|
+
const Mode = require('./capabilities/mode');
|
|
8
|
+
const SwitchableLED = require('./capabilities/switchable-led');
|
|
9
|
+
const LEDBrightness = require('./capabilities/changeable-led-brightness');
|
|
10
|
+
const Buzzer = require('./capabilities/buzzer');
|
|
11
|
+
const { Temperature, Humidity, AQI } = require('./capabilities/sensor');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Abstraction over a Mi Air Purifier.
|
|
15
|
+
*
|
|
16
|
+
* Air Purifiers have a mode that indicates if is on or not. Changing the mode
|
|
17
|
+
* to `idle` will power off the device, all other modes will power on the
|
|
18
|
+
* device.
|
|
19
|
+
*/
|
|
20
|
+
module.exports = class extends AirPurifier
|
|
21
|
+
.with(MiioApi, Power, Mode, Temperature, Humidity, AQI,
|
|
22
|
+
SwitchableLED, LEDBrightness, Buzzer)
|
|
23
|
+
{
|
|
24
|
+
|
|
25
|
+
static get type() {
|
|
26
|
+
return 'miio:air-purifier';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
constructor(options) {
|
|
30
|
+
super(options);
|
|
31
|
+
|
|
32
|
+
// Define the power property
|
|
33
|
+
this.defineProperty('power', v => v === 'on');
|
|
34
|
+
|
|
35
|
+
// Set the mode property and supported modes
|
|
36
|
+
this.defineProperty('mode');
|
|
37
|
+
this.updateModes([
|
|
38
|
+
'idle',
|
|
39
|
+
|
|
40
|
+
'auto',
|
|
41
|
+
'silent',
|
|
42
|
+
'favorite'
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
// Sensor value for Temperature capability
|
|
46
|
+
this.defineProperty('temp_dec', {
|
|
47
|
+
name: 'temperature',
|
|
48
|
+
mapper: v => v / 10.0
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Sensor value for RelativeHumidity capability
|
|
52
|
+
this.defineProperty('humidity');
|
|
53
|
+
|
|
54
|
+
// Sensor value used for AQI (PM2.5) capability
|
|
55
|
+
this.defineProperty('aqi');
|
|
56
|
+
|
|
57
|
+
// The favorite level
|
|
58
|
+
this.defineProperty('favorite_level', {
|
|
59
|
+
name: 'favoriteLevel'
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Info about usage
|
|
63
|
+
this.defineProperty('filter1_life', {
|
|
64
|
+
name: 'filterLifeRemaining'
|
|
65
|
+
});
|
|
66
|
+
this.defineProperty('f1_hour_used', {
|
|
67
|
+
name: 'filterHoursUsed'
|
|
68
|
+
});
|
|
69
|
+
this.defineProperty('use_time', {
|
|
70
|
+
name: 'useTime'
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// State for SwitchableLED capability
|
|
74
|
+
this.defineProperty('led', {
|
|
75
|
+
mapper: v => v === 'on'
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
this.defineProperty('led_b', {
|
|
79
|
+
name: 'ledBrightness',
|
|
80
|
+
mapper: v => {
|
|
81
|
+
switch(v) {
|
|
82
|
+
case 0:
|
|
83
|
+
return 'bright';
|
|
84
|
+
case 1:
|
|
85
|
+
return 'dim';
|
|
86
|
+
case 2:
|
|
87
|
+
return 'off';
|
|
88
|
+
default:
|
|
89
|
+
return 'unknown';
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Buzzer and beeping
|
|
95
|
+
this.defineProperty('buzzer', {
|
|
96
|
+
mapper: v => v === 'on'
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
changePower(power) {
|
|
101
|
+
return this.call('set_power', [ power ? 'on' : 'off' ], {
|
|
102
|
+
refresh: [ 'power', 'mode' ],
|
|
103
|
+
refreshDelay: 200
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Perform a mode change as requested by `mode(string)` or
|
|
109
|
+
* `setMode(string)`.
|
|
110
|
+
*/
|
|
111
|
+
changeMode(mode) {
|
|
112
|
+
return this.call('set_mode', [ mode ], {
|
|
113
|
+
refresh: [ 'power', 'mode' ],
|
|
114
|
+
refreshDelay: 200
|
|
115
|
+
})
|
|
116
|
+
.then(MiioApi.checkOk)
|
|
117
|
+
.catch(err => {
|
|
118
|
+
throw err.code === -5001 ? new Error('Mode `' + mode + '` not supported') : err;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Get the favorite level used when the mode is `favorite`. Between 0 and 16.
|
|
124
|
+
*/
|
|
125
|
+
favoriteLevel(level=undefined) {
|
|
126
|
+
if(typeof level === 'undefined') {
|
|
127
|
+
return Promise.resolve(this.property('favoriteLevel'));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return this.setFavoriteLevel(level);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Set the favorite level used when the mode is `favorite`, should be
|
|
135
|
+
* between 0 and 16.
|
|
136
|
+
*/
|
|
137
|
+
setFavoriteLevel(level) {
|
|
138
|
+
return this.call('set_level_favorite', [ level ])
|
|
139
|
+
.then(() => null);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Set the LED brightness to either `bright`, `dim` or `off`.
|
|
144
|
+
*/
|
|
145
|
+
changeLEDBrightness(level) {
|
|
146
|
+
switch(level) {
|
|
147
|
+
case 'bright':
|
|
148
|
+
level = 0;
|
|
149
|
+
break;
|
|
150
|
+
case 'dim':
|
|
151
|
+
level = 1;
|
|
152
|
+
break;
|
|
153
|
+
case 'off':
|
|
154
|
+
level = 2;
|
|
155
|
+
break;
|
|
156
|
+
default:
|
|
157
|
+
return Promise.reject(new Error('Invalid LED brigthness: ' + level));
|
|
158
|
+
}
|
|
159
|
+
return this.call('set_led_b', [ level ], { refresh: [ 'ledBrightness' ] })
|
|
160
|
+
.then(() => null);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing, BatteryLevel } = require('abstract-things');
|
|
4
|
+
|
|
5
|
+
module.exports = Thing.mixin(
|
|
6
|
+
(Parent) =>
|
|
7
|
+
class extends Parent.with(BatteryLevel) {
|
|
8
|
+
propertyUpdated(key, value) {
|
|
9
|
+
if (key === 'batteryLevel') {
|
|
10
|
+
this.updateBatteryLevel(value);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
super.propertyUpdated(key, value);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
);
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing, State } = require('abstract-things');
|
|
4
|
+
const { boolean } = require('abstract-things/values');
|
|
5
|
+
|
|
6
|
+
const MiioApi = require('../../device');
|
|
7
|
+
|
|
8
|
+
module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
|
|
9
|
+
static get capability() {
|
|
10
|
+
return 'miio:buzzer';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static availableAPI(builder) {
|
|
14
|
+
builder.event('buzzerChanged')
|
|
15
|
+
.type('boolean')
|
|
16
|
+
.description('Buzzer state has changed')
|
|
17
|
+
.done();
|
|
18
|
+
|
|
19
|
+
builder.action('buzzer')
|
|
20
|
+
.description('Get or set if the buzzer is active')
|
|
21
|
+
.argument('boolean', true, 'If the device should beep')
|
|
22
|
+
.returns('boolean', 'If the buzzer is on')
|
|
23
|
+
.done();
|
|
24
|
+
|
|
25
|
+
builder.action('setBuzzer')
|
|
26
|
+
.description('Set if the buzzer is active')
|
|
27
|
+
.argument('boolean', false, 'If the device should beep')
|
|
28
|
+
.returns('boolean', 'If the buzzer is on')
|
|
29
|
+
.done();
|
|
30
|
+
|
|
31
|
+
builder.action('getBuzzer')
|
|
32
|
+
.description('Get if the buzzer is active')
|
|
33
|
+
.returns('boolean', 'If the buzzer is on')
|
|
34
|
+
.done();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
propertyUpdated(key, value) {
|
|
38
|
+
if(key === 'buzzer') {
|
|
39
|
+
if(this.updateState('buzzer', value)) {
|
|
40
|
+
this.emitEvent('buzzerChanged', value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
super.propertyUpdated(key, value);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get or set if the buzzer is active.
|
|
49
|
+
*
|
|
50
|
+
* @param {boolean} active
|
|
51
|
+
* Optional boolean to switch buzzer to.
|
|
52
|
+
*/
|
|
53
|
+
buzzer(active) {
|
|
54
|
+
if(typeof active === 'undefined') {
|
|
55
|
+
return this.getBuzzer();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return this.setBuzzer(active);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getBuzzer() {
|
|
62
|
+
return this.getState('buzzer');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setBuzzer(active) {
|
|
66
|
+
active = boolean(active);
|
|
67
|
+
|
|
68
|
+
return this.changeBuzzer(active)
|
|
69
|
+
.then(() => this.getBuzzer());
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
changeBuzzer(active) {
|
|
73
|
+
return this.call('set_buzzer', [ active ? 'on' : 'off' ], {
|
|
74
|
+
refresh: [ 'buzzer' ]
|
|
75
|
+
})
|
|
76
|
+
.then(MiioApi.checkOk);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing, State } = require('abstract-things');
|
|
4
|
+
const { string } = require('abstract-things/values');
|
|
5
|
+
|
|
6
|
+
module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
|
|
7
|
+
static get capability() {
|
|
8
|
+
return 'miio:led-brightness';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static availableAPI(builder) {
|
|
12
|
+
builder.action('ledBrightness')
|
|
13
|
+
.description('Get or set the LED brightness')
|
|
14
|
+
.argument('string', true, 'If provided, set the LED brightness to this value')
|
|
15
|
+
.returns('string', 'The LED brightness')
|
|
16
|
+
.done();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
propertyUpdated(key, value) {
|
|
20
|
+
if(key === 'ledBrightness') {
|
|
21
|
+
this.updateState('ledBrightness', value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
super.propertyUpdated(key, value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Get or set if the LED brightness.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} brightness The LED brightness
|
|
31
|
+
*/
|
|
32
|
+
ledBrightness(brightness) {
|
|
33
|
+
if(typeof brightness === 'undefined') {
|
|
34
|
+
return this.getState('ledBrightness');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
brightness = string(brightness);
|
|
38
|
+
return this.changeLEDBrightness(brightness)
|
|
39
|
+
.then(() => this.getState('ledBrightness'));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Set the LED brightness.
|
|
44
|
+
*/
|
|
45
|
+
changeLEDBrightness(brightness) {
|
|
46
|
+
throw new Error('changeLEDBrightness not implemented');
|
|
47
|
+
}
|
|
48
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing } = require('abstract-things');
|
|
4
|
+
const { Colorable } = require('abstract-things/lights');
|
|
5
|
+
|
|
6
|
+
module.exports = Thing.mixin(Parent => class extends Parent.with(Colorable) {
|
|
7
|
+
propertyUpdated(key, value) {
|
|
8
|
+
if(key === 'color') {
|
|
9
|
+
this.updateColor(value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
super.propertyUpdated(key, value);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing } = require('abstract-things');
|
|
4
|
+
const { Dimmable } = require('abstract-things/lights');
|
|
5
|
+
|
|
6
|
+
module.exports = Thing.mixin(Parent => class extends Parent.with(Dimmable) {
|
|
7
|
+
propertyUpdated(key, value) {
|
|
8
|
+
if(key === 'brightness') {
|
|
9
|
+
this.updateBrightness(value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
super.propertyUpdated(key, value);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing, SwitchableMode } = require('abstract-things');
|
|
4
|
+
|
|
5
|
+
module.exports = Thing.mixin(Parent => class extends Parent.with(SwitchableMode) {
|
|
6
|
+
propertyUpdated(key, value) {
|
|
7
|
+
if(key === 'mode') {
|
|
8
|
+
this.updateMode(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
super.propertyUpdated(key, value);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing, SwitchablePower } = require('abstract-things');
|
|
4
|
+
|
|
5
|
+
module.exports = Thing.mixin(Parent => class extends Parent.with(SwitchablePower) {
|
|
6
|
+
propertyUpdated(key, value) {
|
|
7
|
+
if(key === 'power' && value !== undefined) {
|
|
8
|
+
this.updatePower(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
super.propertyUpdated(key, value);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing } = require('abstract-things');
|
|
4
|
+
const {
|
|
5
|
+
Temperature,
|
|
6
|
+
RelativeHumidity,
|
|
7
|
+
PM2_5,
|
|
8
|
+
Illuminance,
|
|
9
|
+
AtmosphericPressure,
|
|
10
|
+
PowerLoad,
|
|
11
|
+
PowerConsumed
|
|
12
|
+
} = require('abstract-things/sensors');
|
|
13
|
+
|
|
14
|
+
function bind(Type, updateName, property) {
|
|
15
|
+
return Thing.mixin(Parent => class extends Parent.with(Type) {
|
|
16
|
+
propertyUpdated(key, value) {
|
|
17
|
+
if(key === property) {
|
|
18
|
+
this[updateName](value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
super.propertyUpdated(key, value);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports.Temperature = bind(Temperature, 'updateTemperature', 'temperature');
|
|
27
|
+
module.exports.Humidity = bind(RelativeHumidity, 'updateRelativeHumidity', 'humidity');
|
|
28
|
+
module.exports.Illuminance = bind(Illuminance, 'updateIlluminance', 'illuminance');
|
|
29
|
+
module.exports.AQI = bind(PM2_5, 'updatePM2_5', 'aqi');
|
|
30
|
+
module.exports.AtmosphericPressure = bind(AtmosphericPressure, 'updateAtmosphericPressure', 'atmosphericPressure');
|
|
31
|
+
module.exports.PowerLoad = bind(PowerLoad, 'updatePowerLoad', 'powerLoad');
|
|
32
|
+
module.exports.PowerConsumed = bind(PowerConsumed, 'updatePowerConsumed', 'poweConsumed');
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Setup sensor support for a device.
|
|
36
|
+
*/
|
|
37
|
+
function mixin(device, options) {
|
|
38
|
+
if(device.capabilities.indexOf('sensor') < 0) {
|
|
39
|
+
device.capabilities.push('sensor');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
device.capabilities.push(options.name);
|
|
43
|
+
Object.defineProperty(device, options.name, {
|
|
44
|
+
get: function() {
|
|
45
|
+
return this.property(options.name);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports.extend = mixin;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing, State } = require('abstract-things');
|
|
4
|
+
const { boolean } = require('abstract-things/values');
|
|
5
|
+
|
|
6
|
+
module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
|
|
7
|
+
static get capability() {
|
|
8
|
+
return 'miio:switchable-led';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static availableAPI(builder) {
|
|
12
|
+
builder.action('led')
|
|
13
|
+
.description('Get or set if the LED should be used')
|
|
14
|
+
.argument('boolean', true, 'If provided, set the LED power to this value')
|
|
15
|
+
.returns('boolean', 'If the LED is on')
|
|
16
|
+
.done();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
propertyUpdated(key, value) {
|
|
20
|
+
if(key === 'led') {
|
|
21
|
+
this.updateState('led', value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
super.propertyUpdated(key, value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Get or set if the LED should be on.
|
|
29
|
+
*
|
|
30
|
+
* @param {boolean} power Optional power to set LED to
|
|
31
|
+
*/
|
|
32
|
+
led(power) {
|
|
33
|
+
if(typeof power === 'undefined') {
|
|
34
|
+
return this.getState('led');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
power = boolean(power);
|
|
38
|
+
return this.changeLED(power)
|
|
39
|
+
.then(() => this.getState('led'));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Set if the LED should be on when the device is running.
|
|
44
|
+
*/
|
|
45
|
+
changeLED(power) {
|
|
46
|
+
return this.call('set_led', [ power ? 'on' : 'off' ], { refresh: [ 'led' ] })
|
|
47
|
+
.then(() => null);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Thing, SwitchablePower } = require('abstract-things');
|
|
4
|
+
const { PowerPlug, PowerOutlet } = require('abstract-things/electrical');
|
|
5
|
+
|
|
6
|
+
const MiioApi = require('../device');
|
|
7
|
+
const MiioPower = require('./capabilities/power');
|
|
8
|
+
|
|
9
|
+
module.exports = class extends Thing.with(PowerPlug, PowerOutlet, MiioApi, MiioPower) {
|
|
10
|
+
static get type() {
|
|
11
|
+
return 'miio:power-plug';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
constructor(options) {
|
|
15
|
+
super(options);
|
|
16
|
+
|
|
17
|
+
this.defineProperty('on', {
|
|
18
|
+
name: 'power'
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
this.addChild(new USBOutlet(this));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
setName(name) {
|
|
25
|
+
return super.setName(name)
|
|
26
|
+
.then(n => {
|
|
27
|
+
this.child('usb').updateName();
|
|
28
|
+
return n;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
propertyUpdated(key, value) {
|
|
33
|
+
switch(key) {
|
|
34
|
+
case 'powerChannelUsb':
|
|
35
|
+
this.child('usb').updatePower(value);
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
class USBOutlet extends PowerOutlet.with(SwitchablePower) {
|
|
42
|
+
|
|
43
|
+
constructor(parent) {
|
|
44
|
+
super();
|
|
45
|
+
|
|
46
|
+
this.id = parent.id + ':usb';
|
|
47
|
+
|
|
48
|
+
this.parent = parent;
|
|
49
|
+
|
|
50
|
+
this.updateName();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
updateName() {
|
|
54
|
+
this.metadata.name = this.parent.name + ' - USB';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
changePower(power) {
|
|
58
|
+
return this.parent.call(power ? 'set_usb_on' : 'set_usb_off', [], { refresh: true });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|