homebridge-nature-remo-air-purifier 1.0.0 → 1.0.1
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/index.js +57 -29
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -4,8 +4,12 @@ let exec = require("child_process").exec;
|
|
|
4
4
|
module.exports = function(homebridge) {
|
|
5
5
|
Service = homebridge.hap.Service;
|
|
6
6
|
Characteristic = homebridge.hap.Characteristic;
|
|
7
|
-
homebridge.registerAccessory(
|
|
8
|
-
|
|
7
|
+
homebridge.registerAccessory(
|
|
8
|
+
"homebridge-nature-remo-air-purifier",
|
|
9
|
+
"NatureRemoAirPurifier",
|
|
10
|
+
AirPurifier
|
|
11
|
+
);
|
|
12
|
+
};
|
|
9
13
|
|
|
10
14
|
function AirPurifier(log, config) {
|
|
11
15
|
this.log = log;
|
|
@@ -17,45 +21,69 @@ function AirPurifier(log, config) {
|
|
|
17
21
|
|
|
18
22
|
this.state = { power: false };
|
|
19
23
|
|
|
20
|
-
this.informationService = new Service.AccessoryInformation()
|
|
21
|
-
this.informationService
|
|
24
|
+
this.informationService = new Service.AccessoryInformation()
|
|
22
25
|
.setCharacteristic(Characteristic.Manufacturer, "Homebridge")
|
|
23
26
|
.setCharacteristic(Characteristic.Model, "NatureRemoAirPurifier")
|
|
24
27
|
.setCharacteristic(Characteristic.SerialNumber, "NRAP-" + this.name);
|
|
25
28
|
|
|
26
29
|
this.airPurifierService = new Service.AirPurifier(this.name);
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
|
|
31
|
+
// Active (ON / OFF)
|
|
32
|
+
this.airPurifierService
|
|
33
|
+
.getCharacteristic(Characteristic.Active)
|
|
34
|
+
.on("set", this.setPower.bind(this));
|
|
35
|
+
|
|
36
|
+
// 初期状態
|
|
37
|
+
this.airPurifierService
|
|
38
|
+
.setCharacteristic(
|
|
39
|
+
Characteristic.CurrentAirPurifierState,
|
|
40
|
+
Characteristic.CurrentAirPurifierState.INACTIVE
|
|
41
|
+
);
|
|
29
42
|
}
|
|
30
43
|
|
|
31
44
|
AirPurifier.prototype.getServices = function() {
|
|
32
45
|
return [this.informationService, this.airPurifierService];
|
|
33
|
-
}
|
|
46
|
+
};
|
|
34
47
|
|
|
35
48
|
AirPurifier.prototype.setPower = function(value, callback) {
|
|
36
|
-
if (this.state.power
|
|
37
|
-
this.state.power = value;
|
|
38
|
-
this.log('Setting air purifier power to ' + value);
|
|
39
|
-
|
|
40
|
-
const signalID = value ? this.signal_ID_on : this.signal_ID_off;
|
|
41
|
-
|
|
42
|
-
this.cmdRequest(signalID, function(error, stdout, stderr) {
|
|
43
|
-
if (error) {
|
|
44
|
-
this.log('Failed to set power: ' + error);
|
|
45
|
-
callback(error);
|
|
46
|
-
} else {
|
|
47
|
-
callback();
|
|
48
|
-
}
|
|
49
|
-
}.bind(this));
|
|
50
|
-
} else {
|
|
49
|
+
if (this.state.power === value) {
|
|
51
50
|
callback();
|
|
51
|
+
return;
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
|
|
54
|
+
this.state.power = value;
|
|
55
|
+
this.log("Setting air purifier power to " + value);
|
|
56
|
+
|
|
57
|
+
const signalID = value ? this.signal_ID_on : this.signal_ID_off;
|
|
58
|
+
|
|
59
|
+
this.cmdRequest(signalID, (error) => {
|
|
60
|
+
if (error) {
|
|
61
|
+
this.log("Failed to set power: " + error);
|
|
62
|
+
callback(error);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ★ HomeKitに「状態確定」を通知するのが重要
|
|
67
|
+
this.airPurifierService.setCharacteristic(
|
|
68
|
+
Characteristic.CurrentAirPurifierState,
|
|
69
|
+
value
|
|
70
|
+
? Characteristic.CurrentAirPurifierState.PURIFYING
|
|
71
|
+
: Characteristic.CurrentAirPurifierState.INACTIVE
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
callback();
|
|
75
|
+
});
|
|
76
|
+
};
|
|
54
77
|
|
|
55
78
|
AirPurifier.prototype.cmdRequest = function(signalID, callback) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
79
|
+
const cmd =
|
|
80
|
+
'curl -X POST "https://api.nature.global/1/signals/' +
|
|
81
|
+
signalID +
|
|
82
|
+
'/send" ' +
|
|
83
|
+
'-H "accept: application/json" ' +
|
|
84
|
+
'-H "Authorization: Bearer ' +
|
|
85
|
+
this.access_token +
|
|
86
|
+
'"';
|
|
87
|
+
|
|
88
|
+
exec(cmd, (error) => callback(error));
|
|
89
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "homebridge-nature-remo-air-purifier",
|
|
3
3
|
"displayName": "Homebridge Nature Remo Air Purifier",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"description": "Homebridge Plugin to send power on and off to an air purifier using Nature Remo",
|
|
6
6
|
"author": "Sasuki Sho",
|
|
7
7
|
"license": "MIT",
|