-
Notifications
You must be signed in to change notification settings - Fork 2
/
SwitchBot.ts
196 lines (158 loc) · 5.46 KB
/
SwitchBot.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { Buffer } from "node:buffer";
import { createHmac, randomUUID } from "node:crypto";
import axios, { AxiosInstance } from "axios";
import SwitchBotBlindTilt from "./devices/SwitchBotBlindTilt";
import SwitchBotBot from "./devices/SwitchBotBot";
import SwitchBotCamera from "./devices/SwitchBotCamera";
import SwitchBotCeilingLight from "./devices/SwitchBotCeilingLight";
import SwitchBotColorBulb from "./devices/SwitchBotColorBulb";
import SwitchBotContactSensor from "./devices/SwitchBotContactSensor";
import SwitchBotCurtain from "./devices/SwitchBotCurtain";
import SwitchBotCurtain3 from "./devices/SwitchBotCurtain3";
import SwitchBotHub from "./devices/SwitchBotHub";
import SwitchBotHub2 from "./devices/SwitchBotHub2";
import SwitchBotHumidifier from "./devices/SwitchBotHumidifier";
import SwitchBotKeypad from "./devices/SwitchBotKeypad";
import SwitchBotLock from "./devices/SwitchBotLock";
import SwitchBotMeter from "./devices/SwitchBotMeter";
import SwitchBotMotionSensor from "./devices/SwitchBotMotionSensor";
import SwitchBotPlug from "./devices/SwitchBotPlug";
import SwitchBotPlugMini from "./devices/SwitchBotPlugMini";
import SwitchBotRemote from "./devices/SwitchBotRemote";
import SwitchBotRobotVacuumCleaner from "./devices/SwitchBotRobotVacuumCleaner";
import SwitchBotStripLight from "./devices/SwitchBotStripLight";
import Scene from "./scenes/Scene";
import {
DeviceId,
GetAllDevicesResponse,
GetAllScenesResponse,
SceneId,
} from "./types";
export type SwitchBotOptions = {
openToken: string;
secretKey: string;
};
export default class SwitchBot {
private readonly _openToken: string;
private readonly _secretKey: string;
private readonly _axios: AxiosInstance;
constructor(options: SwitchBotOptions) {
this._openToken = options.openToken;
this._secretKey = options.secretKey;
this._axios = axios.create({
baseURL: "https://api.switch-bot.com",
headers: {
Authorization: this._openToken,
},
});
}
// Devices
public devices = async () => {
const response =
await this.getRequest<GetAllDevicesResponse>("/v1.1/devices");
return response.body;
};
public blindTilt = (deviceId: DeviceId) => {
return new SwitchBotBlindTilt(deviceId, this.getDeps());
};
public bot = (deviceId: DeviceId) => {
return new SwitchBotBot(deviceId, this.getDeps());
};
public camera = (deviceId: DeviceId) => {
return new SwitchBotCamera(deviceId, this.getDeps());
};
public ceilingLight = (deviceId: DeviceId) => {
return new SwitchBotCeilingLight(deviceId, this.getDeps());
};
public colorBulb = (deviceId: DeviceId) => {
return new SwitchBotColorBulb(deviceId, this.getDeps());
};
public contactSensor = (deviceId: DeviceId) => {
return new SwitchBotContactSensor(deviceId, this.getDeps());
};
public curtain = (deviceId: DeviceId) => {
return new SwitchBotCurtain(deviceId, this.getDeps());
};
public curtain3 = (deviceId: DeviceId) => {
return new SwitchBotCurtain3(deviceId, this.getDeps());
};
public hub = (deviceId: DeviceId) => {
return new SwitchBotHub(deviceId, this.getDeps());
};
public hub2 = (deviceId: DeviceId) => {
return new SwitchBotHub2(deviceId, this.getDeps());
};
public humidifier = (deviceId: DeviceId) => {
return new SwitchBotHumidifier(deviceId, this.getDeps());
};
public keypad = (deviceId: DeviceId) => {
return new SwitchBotKeypad(deviceId, this.getDeps());
};
public lock = (deviceId: DeviceId) => {
return new SwitchBotLock(deviceId, this.getDeps());
};
public meter = (deviceId: DeviceId) => {
return new SwitchBotMeter(deviceId, this.getDeps());
};
public motionSensor = (deviceId: DeviceId) => {
return new SwitchBotMotionSensor(deviceId, this.getDeps());
};
public plug = (deviceId: DeviceId) => {
return new SwitchBotPlug(deviceId, this.getDeps());
};
public plugMini = (deviceId: DeviceId) => {
return new SwitchBotPlugMini(deviceId, this.getDeps());
};
public remote = (deviceId: DeviceId) => {
return new SwitchBotRemote(deviceId, this.getDeps());
};
public robotVacuumCleaner = (deviceId: DeviceId) => {
return new SwitchBotRobotVacuumCleaner(deviceId, this.getDeps());
};
public stripLight = (deviceId: DeviceId) => {
return new SwitchBotStripLight(deviceId, this.getDeps());
};
// Scenes
public scenes = async () => {
const response =
await this.getRequest<GetAllScenesResponse>("/v1.1/scenes");
return response.body;
};
public scene = (sceneId: SceneId) => {
return new Scene(sceneId, this.getDeps());
};
// Internal
private getDeps = () => ({
getRequest: this.getRequest,
postRequest: this.postRequest,
});
private getRequestHeaders = () => {
const t = Date.now();
const nonce = randomUUID();
const data = this._openToken + t + nonce;
const sign = createHmac("sha256", this._secretKey)
.update(Buffer.from(data, "utf-8"))
.digest()
.toString("base64");
return {
sign,
nonce,
t,
};
};
private getRequest = async <T>(path: string) => {
const response = await this._axios.get<T>(path, {
headers: this.getRequestHeaders(),
});
return response.data;
};
private postRequest = async <T>(path: string, body: any) => {
const response = await this._axios.post<T>(path, body, {
headers: {
...this.getRequestHeaders(),
"Content-Type": "application/json",
},
});
return response.data;
};
}