Skip to content

New Components - tuya #16474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions components/tuya/actions/list-devices/list-devices.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import tuya from "../../tuya.app.mjs";

export default {
key: "tuya-list-devices",
name: "List Devices",
description: "Get a list of devices associated with a home. [See the documentation](https://developer.tuya.com/en/docs/cloud/d7ee73aadb?id=Kawfjer0wkt2a)",
version: "0.0.1",
type: "action",
props: {
tuya,
userId: {
propDefinition: [
tuya,
"userId",
],
},
homeId: {
propDefinition: [
tuya,
"homeId",
(c) => ({
userId: c.userId,
}),
],
optional: true,
},
},
async run({ $ }) {
const response = this.homeId
? await this.tuya.listHomeDevices({
homeId: this.homeId,
})
: await this.tuya.listUserDevices({
userId: this.userId,
});
if (response?.result?.length) {
$.export("$summary", `Found ${response.result.length} device${response.result.length === 1
? ""
: "s"}`);
}
return response;
},
};
29 changes: 29 additions & 0 deletions components/tuya/actions/list-homes/list-homes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import tuya from "../../tuya.app.mjs";

export default {
key: "tuya-list-homes",
name: "List Homes",
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)",
version: "0.0.1",
type: "action",
props: {
tuya,
userId: {
propDefinition: [
tuya,
"userId",
],
},
},
async run({ $ }) {
const response = await this.tuya.listHomes({
userId: this.userId,
});
if (response?.result?.length) {
$.export("$summary", `Found ${response.result.length} home${response.result.length === 1
? ""
: "s"}`);
}
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import tuya from "../../tuya.app.mjs";

export default {
key: "tuya-send-instructions-to-device",
name: "Send Instructions to Device",
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)",
version: "0.0.1",
type: "action",
props: {
tuya,
userId: {
propDefinition: [
tuya,
"userId",
],
},
homeId: {
propDefinition: [
tuya,
"homeId",
(c) => ({
userId: c.userId,
}),
],
optional: true,
},
deviceId: {
propDefinition: [
tuya,
"deviceId",
(c) => ({
userId: c.userId,
homeId: c.homeId,
}),
],
},
instructionCode: {
propDefinition: [
tuya,
"instructionCode",
(c) => ({
deviceId: c.deviceId,
}),
],
},
value: {
type: "string",
label: "Value",
description: "The value to set",
},
},
async run({ $ }) {
const response = await this.tuya.sendInstructionsToDevice({
deviceId: this.deviceId,
data: {
commands: [
{
code: this.instructionCode,
value: this.value,
},
],
},
});
if (response.success) {
$.export("$summary", "Successfully sent instructions to device");
}
return response;
},
};
8 changes: 6 additions & 2 deletions components/tuya/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/tuya",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Tuya Components",
"main": "tuya.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3",
"@tuya/tuya-connector-nodejs": "^2.1.2"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import tuya from "../../tuya.app.mjs";
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";

export default {
key: "tuya-new-device-activated",
name: "New Device Activated",
description: "Emit new event when a device is activated. [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)",
version: "0.0.1",
type: "source",
dedupe: "unique",
props: {
tuya,
db: "$.service.db",
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
userId: {
propDefinition: [
tuya,
"userId",
],
},
homeId: {
propDefinition: [
tuya,
"homeId",
(c) => ({
userId: c.userId,
}),
],
optional: true,
},
},
methods: {
_getLastTs() {
return this.db.get("lastTs") || 0;
},
_setLastTs(lastTs) {
this.db.set("lastTs", lastTs);
},
generateMeta(item) {
return {
id: `${item.id}${item.active_time}`,
summary: `Device Activated with ID: ${item.id}`,
ts: item.active_time,
};
},
},
async run() {
const lastTs = this._getLastTs();
let maxTs = lastTs;

const { result: devices } = this.homeId
? await this.tuya.listHomeDevices({
homeId: this.homeId,
})
: await this.tuya.listUserDevices({
userId: this.userId,
});

for (const device of devices) {
if (device.active_time >= lastTs) {
const meta = this.generateMeta(device);
this.$emit(device, meta);
maxTs = Math.max(device.active_time, maxTs);
}
}

this._setLastTs(maxTs);
},
};
132 changes: 127 additions & 5 deletions components/tuya/tuya.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,133 @@
import { TuyaContext } from "@tuya/tuya-connector-nodejs";
import { ConfigurationError } from "@pipedream/platform";

export default {
type: "app",
app: "tuya",
propDefinitions: {},
propDefinitions: {
userId: {
type: "string",
label: "User ID",
description: "The identifier of a user",
async options() {
const { result: { list } } = await this.listUsers();
return list?.map(({
user_id: value, user_name: label,
}) => ({
label,
value,
})) || [];
},
},
homeId: {
type: "string",
label: "Home ID",
description: "The identifier of a home",
async options({ userId }) {
const { result } = await this.listHomes({
userId,
});
return result?.map(({
home_id: value, name: label,
}) => ({
label,
value,
})) || [];
},
},
deviceId: {
type: "string",
label: "Device ID",
description: "The identifier of a device",
async options({
userId, homeId,
}) {
const { result } = homeId
? await this.listHomeDevices({
homeId,
})
: await this.listUserDevices({
userId,
});
return result?.map(({
id: value, name: label,
}) => ({
label,
value,
})) || [];
},
},
instructionCode: {
type: "string",
label: "Instruction Code",
description: "The code of the command to use",
async options({ deviceId }) {
const { result } = await this.listDeviceFunctions({
deviceId,
});
return result?.functions?.map(({
code: value, name: label,
}) => ({
label,
value,
})) || [];
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_getClient() {
return new TuyaContext({
baseUrl: this.$auth.base_url,
accessKey: this.$auth.client_id,
secretKey: this.$auth.client_secret,
});
},
async _makeRequest({
method = "GET",
path,
data = {},
}) {
const response = await this._getClient().request({
method,
path,
body: data,
});
if (!response.success) {
console.log(response);
throw new ConfigurationError(`${response.msg}`);
}
},
listUsers() {
return this._getClient().user.users({});
},
listHomes({ userId }) {
return this._makeRequest({
path: `/v1.0/users/${userId}/homes`,
});
},
listUserDevices({ userId }) {
return this._makeRequest({
path: `/v1.0/users/${userId}/devices`,
});
},
listHomeDevices({ homeId }) {
return this._makeRequest({
path: `/v1.0/homes/${homeId}/devices`,
});
},
listDeviceFunctions({ deviceId }) {
return this._makeRequest({
path: `/v1.0/devices/${deviceId}/functions`,
});
},
sendInstructionsToDevice({
deviceId, data,
}) {
return this._makeRequest({
method: "POST",
path: `/v1.0/devices/${deviceId}/commands`,
data,
});
},
},
};
};
Loading
Loading