Skip to content

Commit ef8c964

Browse files
committed
updates
1 parent 3759c3b commit ef8c964

File tree

4 files changed

+168
-88
lines changed

4 files changed

+168
-88
lines changed

components/tuya/sources/new-device-activated/new-device-activated.mjs

Lines changed: 0 additions & 74 deletions
This file was deleted.
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+
}

components/tuya/tuya.app.mjs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ export default {
88
userId: {
99
type: "string",
1010
label: "User ID",
11-
description: "The identifier of a user",
12-
async options() {
13-
const { result: { list } } = await this.listUsers();
14-
return list?.map(({
15-
user_id: value, user_name: label,
16-
}) => ({
17-
label,
18-
value,
19-
})) || [];
20-
},
11+
description: "The unique identifier of a user. E.g. `az1748632127862aWriL`",
2112
},
2213
homeId: {
2314
type: "string",
@@ -60,7 +51,7 @@ export default {
6051
instructionCode: {
6152
type: "string",
6253
label: "Instruction Code",
63-
description: "The code of the command to use",
54+
description: "The code of the command to use. E.g. `switch_1`",
6455
async options({ deviceId }) {
6556
const { result } = await this.listDeviceFunctions({
6657
deviceId,
@@ -96,9 +87,7 @@ export default {
9687
console.log(response);
9788
throw new ConfigurationError(`${response.msg}`);
9889
}
99-
},
100-
listUsers() {
101-
return this._getClient().user.users({});
90+
return response;
10291
},
10392
listHomes({ userId }) {
10493
return this._makeRequest({

0 commit comments

Comments
 (0)