Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
hs300 class with child switches
Browse files Browse the repository at this point in the history
  • Loading branch information
adumont committed Jun 10, 2020
1 parent 0feb146 commit 07dd74b
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 0 deletions.
141 changes: 141 additions & 0 deletions lib/hs300.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* @package tplink-cloud-api
* @author Alexandre Dumont <[email protected]>
* @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 hs100 from "./hs100";
import HS300child from "./hs300child";
import TPLink from "./tplink";

export default class HS300 extends hs100 {
children: any[];
tpLink: TPLink;

constructor(tpLink:TPLink, deviceInfo) {
super(tpLink, deviceInfo);
this.tpLink = tpLink;
this.genericType = "switch";
}

async getChildren() {
// const r = await this.getSysInfo();
const r = {
"sw_ver": "1.0.19 Build 200224 Rel.090814",
"hw_ver": "1.0",
"model": "HS300(US)",
"deviceId": "redacted",
"oemId": "5C9E6254BEBAED63B2B6102966D24C17",
"hwId": "redacted",
"rssi": -59,
"longitude_i": -1224174,
"latitude_i": 377759,
"alias": "TP-LINK_Power Strip_2A54",
"status": "new",
"mic_type": "IOT.SMARTPLUGSWITCH",
"feature": "TIM:ENE",
"mac": "redacted",
"updating": 0,
"led_off": 0,
"children": [
{
"id": "8006070807D6C11B3D3B5B61455944531BF7C62200",
"state": 1,
"alias": "Plug 1",
"on_time": 99381,
"next_action": {
"type": -1
}
},
{
"id": "8006070807D6C11B3D3B5B61455944531BF7C62201",
"state": 1,
"alias": "Plug 2",
"on_time": 254051,
"next_action": {
"type": -1
}
},
{
"id": "8006070807D6C11B3D3B5B61455944531BF7C62202",
"state": 1,
"alias": "Plug 3",
"on_time": 254051,
"next_action": {
"type": -1
}
},
{
"id": "8006070807D6C11B3D3B5B61455944531BF7C62203",
"state": 1,
"alias": "Plug 4",
"on_time": 174221,
"next_action": {
"type": -1
}
},
{
"id": "8006070807D6C11B3D3B5B61455944531BF7C62204",
"state": 1,
"alias": "Plug 5",
"on_time": 9718,
"next_action": {
"type": 1,
"schd_sec": 75600,
"action": 0
}
},
{
"id": "8006070807D6C11B3D3B5B61455944531BF7C62205",
"state": 1,
"alias": "Plug 6",
"on_time": 13318,
"next_action": {
"type": 1,
"schd_sec": 72000,
"action": 0
}
}
],
"child_num": 6,
"err_code": 0
}
return(this.children = r.children);
}

findChild(alias: string): any {
let child: any;
if (alias && this.children) {
// we first search a correspondig device by alias
child = this.children.find(d => d.alias === alias);
// we then search a correspondig device by deviceId
if (!child) {
child = this.children.find(d => d.id === alias);
}
}
if (!child) {
throw new Error("invalid alias/id: not found in children list");
}
return child;
}

getChild(alias) {
return new HS300child(this.tpLink, this.device, this.findChild(alias));
}
}
108 changes: 108 additions & 0 deletions lib/hs300child.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* @package tplink-cloud-api
* @author Alexandre Dumont <[email protected]>
* @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;
}
}
6 changes: 6 additions & 0 deletions lib/tplink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import device from "./device";
import hs100 from "./hs100";
import hs110 from "./hs110";
import hs200 from "./hs200";
import hs300 from "./hs300";
import lb100 from "./lb100";
import lb130 from "./lb130";

Expand Down Expand Up @@ -203,6 +204,11 @@ export default class TPLink {
return new hs200(this, this.findDevice(alias));
}

// for an HS300 smart multi-switch
getHS300(alias) {
return new hs300(this, this.findDevice(alias));
}

// for an LB100, LB110 & LB120
getLB100(alias): lb100 {
return new lb100(this, this.findDevice(alias));
Expand Down

0 comments on commit 07dd74b

Please sign in to comment.