This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
hs300child.ts
108 lines (89 loc) · 2.8 KB
/
hs300child.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* @package tplink-cloud-api
* @author Alexandre Dumont <adumont@gmail.com>
* @copyright (C) 2017 - Alexandre Dumont
* @license https://www.gnu.org/licenses/gpl-3.0.txt
* @link http://itnerd.space
*/
/* This file is part of tplink-cloud-api.
tplink-cloud-api is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
tplink-cloud-api is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
tplink-cloud-api. If not, see http://www.gnu.org/licenses/. */
import device from "./device";
import HS300 from "./hs300";
export default class HS300child extends device {
private child;
constructor(tpLink, deviceInfo, child) {
super(tpLink, deviceInfo);
this.genericType = "plug";
this.child = child;
}
async powerOn() {
return await this.setRelayState(1);
}
async powerOff() {
return await this.setRelayState(0);
}
async setRelayState(state) {
return await super.passthroughRequest({
context: { child_ids:[ this.child.id ] },
system: { set_relay_state: { state } }
});
}
async set_relay_state(state) {
// TODO remove
return this.setRelayState(state);
}
async getScheduleRules() {
return await super.passthroughRequest({
context: { child_ids:[ this.child.id ] },
schedule: { get_rules: {} }
});
}
async editScheduleRule(rule) {
return await super.passthroughRequest({
context: { child_ids:[ this.child.id ] },
schedule: { edit_rule: rule }
});
}
async setLedState(on: boolean) {
// weird api in tplink: we send 0 to set on and 1 to set off
const offState = on ? 0 : 1;
return await super.passthroughRequest({
context: { child_ids:[ this.child.id ] },
"system": { "set_led_off": { "off": offState } }
});
}
async isOn() {
return (await this.getRelayState()) === 1;
}
async isOff() {
return !(await this.isOn());
}
async toggle() {
const s = await this.getRelayState();
return await this.setRelayState(s === 0 ? 1 : 0);
}
async get_relay_state() {
// TODO remove
return this.getRelayState();
}
async getRelayState() {
const r = await this.getSysInfo();
return r.relay_state;
}
async getSysInfo() {
const r = await super.passthroughRequest({
context: { child_ids:[ this.child.id ] },
system: { get_sysinfo: null },
emeter: { get_realtime: null }
});
return r.system.get_sysinfo;
}
}