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
remove dummy data
  • Loading branch information
adumont committed Oct 25, 2020
1 parent 9cf0ccd commit e52f75d
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lib/hs300.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @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 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();
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 <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;
}
}
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 lb120 from "./lb120";
import lb130 from "./lb130";
Expand Down Expand Up @@ -215,6 +216,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, KL110, KL50, KL60 lightbulb (dimmable)
getLB100(alias): lb100 {
return new lb100(this, this.findDevice(alias));
Expand Down

0 comments on commit e52f75d

Please sign in to comment.