Skip to content

Commit 46a15c5

Browse files
authored
New Components - tuya (#16474)
* new components * pnpm-lock.yaml * update * updates
1 parent 88b7698 commit 46a15c5

File tree

8 files changed

+446
-8
lines changed

8 files changed

+446
-8
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import tuya from "../../tuya.app.mjs";
2+
3+
export default {
4+
key: "tuya-list-devices",
5+
name: "List Devices",
6+
description: "Get a list of devices associated with a home. [See the documentation](https://developer.tuya.com/en/docs/cloud/d7ee73aadb?id=Kawfjer0wkt2a)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
tuya,
11+
userId: {
12+
propDefinition: [
13+
tuya,
14+
"userId",
15+
],
16+
},
17+
homeId: {
18+
propDefinition: [
19+
tuya,
20+
"homeId",
21+
(c) => ({
22+
userId: c.userId,
23+
}),
24+
],
25+
optional: true,
26+
},
27+
},
28+
async run({ $ }) {
29+
const response = this.homeId
30+
? await this.tuya.listHomeDevices({
31+
homeId: this.homeId,
32+
})
33+
: await this.tuya.listUserDevices({
34+
userId: this.userId,
35+
});
36+
if (response?.result?.length) {
37+
$.export("$summary", `Found ${response.result.length} device${response.result.length === 1
38+
? ""
39+
: "s"}`);
40+
}
41+
return response;
42+
},
43+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import tuya from "../../tuya.app.mjs";
2+
3+
export default {
4+
key: "tuya-list-homes",
5+
name: "List Homes",
6+
description: "Based on the user ID, query the list of homes where the specified user belongs. [See the documentation](https://developer.tuya.com/en/docs/cloud/f5dd40ed14?id=Kawfjh9hpov1n)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
tuya,
11+
userId: {
12+
propDefinition: [
13+
tuya,
14+
"userId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const response = await this.tuya.listHomes({
20+
userId: this.userId,
21+
});
22+
if (response?.result?.length) {
23+
$.export("$summary", `Found ${response.result.length} home${response.result.length === 1
24+
? ""
25+
: "s"}`);
26+
}
27+
return response;
28+
},
29+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import tuya from "../../tuya.app.mjs";
2+
3+
export default {
4+
key: "tuya-send-instructions-to-device",
5+
name: "Send Instructions to Device",
6+
description: "Send an instruction to the specified device. [See the documentation](https://developer.tuya.com/en/docs/cloud/device-control?id=K95zu01ksols7#title-35-Send%20instructions%20to%20the%20device)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
tuya,
11+
userId: {
12+
propDefinition: [
13+
tuya,
14+
"userId",
15+
],
16+
},
17+
homeId: {
18+
propDefinition: [
19+
tuya,
20+
"homeId",
21+
(c) => ({
22+
userId: c.userId,
23+
}),
24+
],
25+
optional: true,
26+
},
27+
deviceId: {
28+
propDefinition: [
29+
tuya,
30+
"deviceId",
31+
(c) => ({
32+
userId: c.userId,
33+
homeId: c.homeId,
34+
}),
35+
],
36+
},
37+
instructionCode: {
38+
propDefinition: [
39+
tuya,
40+
"instructionCode",
41+
(c) => ({
42+
deviceId: c.deviceId,
43+
}),
44+
],
45+
},
46+
value: {
47+
type: "string",
48+
label: "Value",
49+
description: "The value to set",
50+
},
51+
},
52+
async run({ $ }) {
53+
const response = await this.tuya.sendInstructionsToDevice({
54+
deviceId: this.deviceId,
55+
data: {
56+
commands: [
57+
{
58+
code: this.instructionCode,
59+
value: this.value,
60+
},
61+
],
62+
},
63+
});
64+
if (response.success) {
65+
$.export("$summary", "Successfully sent instructions to device");
66+
}
67+
return response;
68+
},
69+
};

components/tuya/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/tuya",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Tuya Components",
55
"main": "tuya.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"@tuya/tuya-connector-nodejs": "^2.1.2"
1418
}
15-
}
19+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import tuya from "../../tuya.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
import sampleEmit from "./test-event.mjs";
4+
5+
export default {
6+
key: "tuya-new-device-parameter-updated",
7+
name: "New Device Parameter Updated",
8+
description: "Emit new event when the specified device parameter is updated. [See the documentation](https://developer.tuya.com/en/docs/cloud/device-management?id=K9g6rfntdz78a#title-10-Get%20a%20list%20of%20devices%20under%20a%20specified%20user)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
tuya,
14+
db: "$.service.db",
15+
timer: {
16+
type: "$.interface.timer",
17+
default: {
18+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
19+
},
20+
},
21+
userId: {
22+
propDefinition: [
23+
tuya,
24+
"userId",
25+
],
26+
},
27+
deviceParameter: {
28+
type: "string",
29+
label: "Device Parameter",
30+
description: "The device parameter to watch for updates. E.g. `switch_1`",
31+
},
32+
homeId: {
33+
propDefinition: [
34+
tuya,
35+
"homeId",
36+
(c) => ({
37+
userId: c.userId,
38+
}),
39+
],
40+
optional: true,
41+
},
42+
},
43+
methods: {
44+
_getPreviousValues() {
45+
return this.db.get("previousValues") || {};
46+
},
47+
_setPreviousValues(previousValues) {
48+
this.db.set("previousValues", previousValues);
49+
},
50+
getCurrentValue(device) {
51+
const { status } = device;
52+
const relevantStatus = status.find(({ code }) => code === this.deviceParameter);
53+
return relevantStatus?.value;
54+
},
55+
generateMeta(item) {
56+
const ts = Date.now();
57+
return {
58+
id: `${item.id}${ts}`,
59+
summary: `Device Updated with ID: ${item.id}`,
60+
ts,
61+
};
62+
},
63+
},
64+
async run() {
65+
const previousValues = this._getPreviousValues();
66+
const newValues = {};
67+
68+
const { result: devices } = this.homeId
69+
? await this.tuya.listHomeDevices({
70+
homeId: this.homeId,
71+
})
72+
: await this.tuya.listUserDevices({
73+
userId: this.userId,
74+
});
75+
76+
for (const device of devices) {
77+
const currentValue = this.getCurrentValue(device);
78+
if (previousValues[device.id] !== currentValue) {
79+
const meta = this.generateMeta(device);
80+
this.$emit(device, meta);
81+
}
82+
newValues[device.id] = currentValue;
83+
}
84+
85+
this._setPreviousValues(newValues);
86+
},
87+
sampleEmit,
88+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export default {
2+
"active_time": 1748632139,
3+
"biz_type": 18,
4+
"category": "cz",
5+
"create_time": 1748632139,
6+
"icon": "smart/icon/bay1599677305883cNM4/03aaf2fd77d712895eb7b8408a374ebe.png",
7+
"id": "vdevo174863213925233",
8+
"ip": "47.225.143.157",
9+
"lat": "",
10+
"local_key": ".'0k9<h@|2j7!OK9",
11+
"lon": "",
12+
"model": "TS-441",
13+
"name": "Tramontina Plug-vdevo",
14+
"online": true,
15+
"owner_id": "250587566",
16+
"product_id": "mjzxchzvqmxps3lf",
17+
"product_name": "Plugue Tramontina",
18+
"status": [
19+
{
20+
"code": "switch_1",
21+
"value": false
22+
},
23+
{
24+
"code": "countdown_1",
25+
"value": 0
26+
},
27+
{
28+
"code": "add_ele",
29+
"value": 0
30+
},
31+
{
32+
"code": "cur_current",
33+
"value": 0
34+
},
35+
{
36+
"code": "cur_power",
37+
"value": 0
38+
},
39+
{
40+
"code": "cur_voltage",
41+
"value": 0
42+
},
43+
{
44+
"code": "relay_status",
45+
"value": "power_off"
46+
},
47+
{
48+
"code": "overcharge_switch",
49+
"value": false
50+
},
51+
{
52+
"code": "light_mode",
53+
"value": "relay"
54+
},
55+
{
56+
"code": "child_lock",
57+
"value": false
58+
},
59+
{
60+
"code": "cycle_time",
61+
"value": ""
62+
},
63+
{
64+
"code": "random_time",
65+
"value": ""
66+
},
67+
{
68+
"code": "switch_inching",
69+
"value": ""
70+
}
71+
],
72+
"sub": false,
73+
"time_zone": "",
74+
"uid": "az1748632127862aWriL",
75+
"update_time": 1748632139,
76+
"uuid": "vdevo174863213925233"
77+
}

0 commit comments

Comments
 (0)