forked from JohanBendz/com.tuya.zigbee
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapi.js
More file actions
149 lines (134 loc) · 4.54 KB
/
Copy pathapi.js
File metadata and controls
149 lines (134 loc) · 4.54 KB
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
module.exports = {
/**
* Fetch all devices configured in this Homey Pro.
* Returns a lightweight array for use in settings dropdown menus.
* Uses native Homey SDK3 APIs — no dependency on homey-api.
*/
async getDevices({ homey }) {
try {
const driverList = homey.drivers.getDrivers();
const allDevices = [];
for (const driverId of Object.keys(driverList)) {
const driver = driverList[driverId];
const devices = driver.getDevices();
for (const device of Object.values(devices)) {
allDevices.push({
id: device.getId(),
name: device.getName(),
zoneName: device.getZone()?.getName() || '',
driverId: device.getDriver().getId() || '',
driverUri: device.getDriver().getUri() || ''
});
}
}
return allDevices.sort((a, b) => a.name.localeCompare(b.name));
} catch (err) {
homey.error('[FlowRepair API] Failed to fetch devices:', err);
throw new Error(`Failed to retrieve devices: ${err.message}`);
}
},
/**
* Search and replace old device references with new ones
* inside triggers, conditions, and actions of all Flows and Advanced Flows.
* Uses native Homey SDK3 ManagerFlow APIs.
*/
async replaceDevice({ homey, body }) {
const { oldId, newId } = body;
if (!oldId || !newId) {
throw new Error('Both oldId and newId are required parameters.');
}
try {
const flowManager = homey.flow;
let flowsUpdated = 0;
let advancedFlowsUpdated = 0;
// 1. Process Standard Flows
const flows = await flowManager.getFlows();
for (const flow of Object.values(flows)) {
let updated = false;
// Triggers
if (flow.trigger && flow.trigger.uri) {
const replaceTrigger = flow.trigger.uri.replace('homey:device:', '');
if (replaceTrigger === oldId) {
flow.trigger.uri = `homey:device:${newId}`;
updated = true;
}
}
// Actions
if (Array.isArray(flow.actions)) {
for (let i = 0; i < flow.actions.length; i++) {
const action = flow.actions[i];
if (action.uri) {
const replaceAction = action.uri.replace('homey:device:', '');
if (replaceAction === oldId) {
flow.actions[i].uri = `homey:device:${newId}`;
updated = true;
}
}
}
}
// Conditions
if (Array.isArray(flow.conditions)) {
for (let i = 0; i < flow.conditions.length; i++) {
const condition = flow.conditions[i];
if (condition.uri) {
const replaceCondition = condition.uri.replace('homey:device:', '');
if (replaceCondition === oldId) {
flow.conditions[i].uri = `homey:device:${newId}`;
updated = true;
}
}
}
}
if (updated) {
await flowManager.updateFlow({
id: flow.id,
flow: {
trigger: flow.trigger,
actions: flow.actions,
conditions: flow.conditions
}
});
flowsUpdated++;
}
}
// 2. Process Advanced Flows
let advancedFlows;
try {
advancedFlows = await flowManager.getAdvancedFlows();
} catch (e) {
// getAdvancedFlows might not be available on older SDK versions
advancedFlows = {};
}
for (const af of Object.values(advancedFlows)) {
let updated = false;
const cards = af.cards;
for (const cardId in cards) {
const card = cards[cardId];
if (card.ownerUri) {
const replaceId = card.ownerUri.replace('homey:device:', '');
if (replaceId === oldId) {
card.ownerUri = `homey:device:${newId}`;
updated = true;
}
}
}
if (updated) {
await flowManager.updateAdvancedFlow({
id: af.id,
advancedflow: { cards }
});
advancedFlowsUpdated++;
}
}
homey.log(`[FlowRepair API] Successfully migrated ${flowsUpdated} standard flows and ${advancedFlowsUpdated} advanced flows from ${oldId} to ${newId}.`);
return {
success: true,
flowsUpdated,
advancedFlowsUpdated
};
} catch (err) {
homey.error('[FlowRepair API] Replacement failed:', err);
throw new Error(`Device replacement failed: ${err.message}`);
}
}
};