@switchbot/homebridge-switchbot 5.0.0-beta.99 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.changeset/config.json +14 -0
- package/.github/copilot-instructions.md +39 -0
- package/.github/workflows/ci.yml +4 -1
- package/.github/workflows/manual-e2e.yml +6 -3
- package/.github/workflows/release.yml +64 -15
- package/.github/workflows/stale.yml +2 -4
- package/.husky/pre-push +15 -0
- package/CHANGELOG.md +126 -134
- package/MIGRATION.md +16 -6
- package/README.md +84 -3
- package/TODO.md +263 -0
- package/config.schema.json +229 -36
- package/dist/SwitchBotHAPPlatform.d.ts +133 -0
- package/dist/SwitchBotHAPPlatform.d.ts.map +1 -0
- package/dist/SwitchBotHAPPlatform.js +555 -0
- package/dist/SwitchBotHAPPlatform.js.map +1 -0
- package/dist/SwitchBotMatterPlatform.d.ts +141 -0
- package/dist/SwitchBotMatterPlatform.d.ts.map +1 -0
- package/dist/SwitchBotMatterPlatform.js +536 -0
- package/dist/SwitchBotMatterPlatform.js.map +1 -0
- package/dist/device-types.d.ts +31 -0
- package/dist/device-types.d.ts.map +1 -0
- package/dist/device-types.js +246 -0
- package/dist/device-types.js.map +1 -0
- package/dist/deviceCommandMapper.d.ts +10 -0
- package/dist/deviceCommandMapper.d.ts.map +1 -0
- package/dist/deviceCommandMapper.js +319 -0
- package/dist/deviceCommandMapper.js.map +1 -0
- package/dist/deviceFactory.d.ts +3 -2
- package/dist/deviceFactory.d.ts.map +1 -1
- package/dist/deviceFactory.js +107 -29
- package/dist/deviceFactory.js.map +1 -1
- package/dist/devices/genericDevice.d.ts +59 -37
- package/dist/devices/genericDevice.d.ts.map +1 -1
- package/dist/devices/genericDevice.js +376 -78
- package/dist/devices/genericDevice.js.map +1 -1
- package/dist/errors.d.ts +38 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +32 -0
- package/dist/errors.js.map +1 -0
- package/dist/homebridge-ui/device-types.js +246 -0
- package/dist/homebridge-ui/device-types.js.map +1 -0
- package/dist/homebridge-ui/deviceCommandMapper.js +319 -0
- package/dist/homebridge-ui/deviceCommandMapper.js.map +1 -0
- package/dist/homebridge-ui/endpoints/config.d.ts +3 -0
- package/dist/homebridge-ui/endpoints/config.d.ts.map +1 -0
- package/dist/homebridge-ui/endpoints/config.js +90 -0
- package/dist/homebridge-ui/endpoints/config.js.map +1 -0
- package/dist/homebridge-ui/endpoints/devices.d.ts +6 -0
- package/dist/homebridge-ui/endpoints/devices.d.ts.map +1 -0
- package/dist/homebridge-ui/endpoints/devices.js +144 -0
- package/dist/homebridge-ui/endpoints/devices.js.map +1 -0
- package/dist/homebridge-ui/endpoints/discovery.d.ts +7 -0
- package/dist/homebridge-ui/endpoints/discovery.d.ts.map +1 -0
- package/dist/homebridge-ui/endpoints/discovery.js +219 -0
- package/dist/homebridge-ui/endpoints/discovery.js.map +1 -0
- package/dist/homebridge-ui/errors.js +32 -0
- package/dist/homebridge-ui/errors.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/config.js +90 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/config.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/devices.js +144 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/devices.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js +219 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/server.js +11 -0
- package/dist/homebridge-ui/homebridge-ui/server.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/utils/config-parser.js +108 -0
- package/dist/homebridge-ui/homebridge-ui/utils/config-parser.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/utils/device-migration.js +111 -0
- package/dist/homebridge-ui/homebridge-ui/utils/device-migration.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/utils/logger.js +17 -0
- package/dist/homebridge-ui/homebridge-ui/utils/logger.js.map +1 -0
- package/dist/homebridge-ui/public/css/styles.css +483 -0
- package/dist/homebridge-ui/public/index.html +197 -621
- package/dist/homebridge-ui/public/js/advanced-settings.d.ts +3 -0
- package/dist/homebridge-ui/public/js/advanced-settings.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/advanced-settings.js +95 -0
- package/dist/homebridge-ui/public/js/advanced-settings.js.map +1 -0
- package/dist/homebridge-ui/public/js/advanced-settings.ts +94 -0
- package/dist/homebridge-ui/public/js/api.d.ts +66 -0
- package/dist/homebridge-ui/public/js/api.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/api.js +295 -0
- package/dist/homebridge-ui/public/js/api.js.map +1 -0
- package/dist/homebridge-ui/public/js/api.ts +355 -0
- package/dist/homebridge-ui/public/js/app.d.ts +2 -0
- package/dist/homebridge-ui/public/js/app.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/app.js +3722 -0
- package/dist/homebridge-ui/public/js/app.js.map +7 -0
- package/dist/homebridge-ui/public/js/app.ts +22 -0
- package/dist/homebridge-ui/public/js/constants.d.ts +2 -0
- package/dist/homebridge-ui/public/js/constants.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/constants.js +2 -0
- package/dist/homebridge-ui/public/js/constants.js.map +1 -0
- package/dist/homebridge-ui/public/js/constants.ts +1 -0
- package/dist/homebridge-ui/public/js/credentials.d.ts +3 -0
- package/dist/homebridge-ui/public/js/credentials.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/credentials.js +99 -0
- package/dist/homebridge-ui/public/js/credentials.js.map +1 -0
- package/dist/homebridge-ui/public/js/credentials.ts +105 -0
- package/dist/homebridge-ui/public/js/devices-delete.d.ts +3 -0
- package/dist/homebridge-ui/public/js/devices-delete.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/devices-delete.js +199 -0
- package/dist/homebridge-ui/public/js/devices-delete.js.map +1 -0
- package/dist/homebridge-ui/public/js/devices-delete.ts +227 -0
- package/dist/homebridge-ui/public/js/devices.d.ts +9 -0
- package/dist/homebridge-ui/public/js/devices.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/devices.js +98 -0
- package/dist/homebridge-ui/public/js/devices.js.map +1 -0
- package/dist/homebridge-ui/public/js/devices.ts +106 -0
- package/dist/homebridge-ui/public/js/discovery.d.ts +9 -0
- package/dist/homebridge-ui/public/js/discovery.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/discovery.js +1201 -0
- package/dist/homebridge-ui/public/js/discovery.js.map +1 -0
- package/dist/homebridge-ui/public/js/discovery.ts +1335 -0
- package/dist/homebridge-ui/public/js/logger.d.ts +7 -0
- package/dist/homebridge-ui/public/js/logger.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/logger.js +17 -0
- package/dist/homebridge-ui/public/js/logger.js.map +1 -0
- package/dist/homebridge-ui/public/js/logger.ts +17 -0
- package/dist/homebridge-ui/public/js/modal.d.ts +5 -0
- package/dist/homebridge-ui/public/js/modal.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/modal.js +35 -0
- package/dist/homebridge-ui/public/js/modal.js.map +1 -0
- package/dist/homebridge-ui/public/js/modal.ts +35 -0
- package/dist/homebridge-ui/public/js/modals.d.ts +15 -0
- package/dist/homebridge-ui/public/js/modals.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/modals.js +675 -0
- package/dist/homebridge-ui/public/js/modals.js.map +1 -0
- package/dist/homebridge-ui/public/js/modals.ts +765 -0
- package/dist/homebridge-ui/public/js/render.d.ts +71 -0
- package/dist/homebridge-ui/public/js/render.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/render.js +960 -0
- package/dist/homebridge-ui/public/js/render.js.map +1 -0
- package/dist/homebridge-ui/public/js/render.ts +1084 -0
- package/dist/homebridge-ui/public/js/toast.d.ts +6 -0
- package/dist/homebridge-ui/public/js/toast.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/toast.js +38 -0
- package/dist/homebridge-ui/public/js/toast.js.map +1 -0
- package/dist/homebridge-ui/public/js/toast.ts +44 -0
- package/dist/homebridge-ui/public/js/types.d.ts +23 -0
- package/dist/homebridge-ui/public/js/types.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/types.js +2 -0
- package/dist/homebridge-ui/public/js/types.js.map +1 -0
- package/dist/homebridge-ui/public/js/types.ts +26 -0
- package/dist/homebridge-ui/server.d.ts +1 -3
- package/dist/homebridge-ui/server.d.ts.map +1 -1
- package/dist/homebridge-ui/server.js +8 -471
- package/dist/homebridge-ui/server.js.map +1 -1
- package/dist/homebridge-ui/settings.js +8 -0
- package/dist/homebridge-ui/settings.js.map +1 -0
- package/dist/homebridge-ui/switchbotClient.js +247 -0
- package/dist/homebridge-ui/switchbotClient.js.map +1 -0
- package/dist/homebridge-ui/utils/config-parser.d.ts +39 -0
- package/dist/homebridge-ui/utils/config-parser.d.ts.map +1 -0
- package/dist/homebridge-ui/utils/config-parser.js +108 -0
- package/dist/homebridge-ui/utils/config-parser.js.map +1 -0
- package/dist/homebridge-ui/utils/device-migration.d.ts +35 -0
- package/dist/homebridge-ui/utils/device-migration.d.ts.map +1 -0
- package/dist/homebridge-ui/utils/device-migration.js +111 -0
- package/dist/homebridge-ui/utils/device-migration.js.map +1 -0
- package/dist/homebridge-ui/utils/logger.d.ts +7 -0
- package/dist/homebridge-ui/utils/logger.d.ts.map +1 -0
- package/dist/homebridge-ui/utils/logger.js +17 -0
- package/dist/homebridge-ui/utils/logger.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/settings.d.ts +1 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +1 -0
- package/dist/settings.js.map +1 -1
- package/dist/switchbotClient.d.ts +12 -10
- package/dist/switchbotClient.d.ts.map +1 -1
- package/dist/switchbotClient.js +156 -103
- package/dist/switchbotClient.js.map +1 -1
- package/dist/utils.d.ts +76 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +1121 -4
- package/dist/utils.js.map +1 -1
- package/docs/assets/highlight.css +16 -2
- package/docs/assets/main.js +1 -1
- package/docs/index.html +82 -5
- package/docs/variables/default.html +3 -1
- package/eslint.config.js +9 -5
- package/nodemon.json +2 -2
- package/package.json +34 -21
- package/scripts/build-ui.js +37 -0
- package/scripts/free-dev-ports.mjs +105 -0
- package/scripts/generate-matter-maps.js +34 -17
- package/scripts/sync-device-types.mjs +31 -0
- package/src/SwitchBotHAPPlatform.ts +558 -0
- package/src/SwitchBotMatterPlatform.ts +538 -0
- package/src/device-types.js +246 -0
- package/src/device-types.js.map +1 -0
- package/src/device-types.ts +261 -0
- package/src/deviceCommandMapper.js +319 -0
- package/src/deviceCommandMapper.js.map +1 -0
- package/src/deviceCommandMapper.ts +333 -0
- package/src/deviceFactory.ts +125 -45
- package/src/devices/genericDevice.ts +411 -69
- package/src/errors.js +32 -0
- package/src/errors.js.map +1 -0
- package/src/errors.ts +35 -0
- package/src/homebridge-ui/endpoints/config.ts +110 -0
- package/src/homebridge-ui/endpoints/devices.ts +153 -0
- package/src/homebridge-ui/endpoints/discovery.ts +240 -0
- package/src/homebridge-ui/public/css/styles.css +483 -0
- package/src/homebridge-ui/public/index.html +197 -621
- package/src/homebridge-ui/public/js/advanced-settings.ts +94 -0
- package/src/homebridge-ui/public/js/api.ts +355 -0
- package/src/homebridge-ui/public/js/app.ts +22 -0
- package/src/homebridge-ui/public/js/constants.ts +1 -0
- package/src/homebridge-ui/public/js/credentials.ts +105 -0
- package/src/homebridge-ui/public/js/devices-delete.ts +227 -0
- package/src/homebridge-ui/public/js/devices.ts +106 -0
- package/src/homebridge-ui/public/js/discovery.ts +1335 -0
- package/src/homebridge-ui/public/js/logger.ts +17 -0
- package/src/homebridge-ui/public/js/modal.ts +35 -0
- package/src/homebridge-ui/public/js/modals.ts +765 -0
- package/src/homebridge-ui/public/js/render.ts +1084 -0
- package/src/homebridge-ui/public/js/toast.ts +44 -0
- package/src/homebridge-ui/public/js/types.ts +26 -0
- package/src/homebridge-ui/server.ts +9 -554
- package/src/homebridge-ui/utils/config-parser.ts +125 -0
- package/src/homebridge-ui/utils/device-migration.ts +144 -0
- package/src/homebridge-ui/utils/logger.ts +17 -0
- package/src/index.ts +12 -2
- package/src/settings.js +8 -0
- package/src/settings.js.map +1 -0
- package/src/settings.ts +2 -0
- package/src/switchbotClient.js +247 -0
- package/src/switchbotClient.js.map +1 -0
- package/src/switchbotClient.ts +177 -114
- package/src/utils.ts +1133 -5
- package/test/client/switchbot-client-debounce.spec.ts +35 -0
- package/test/client/switchbot-client-openapi.spec.ts +19 -0
- package/test/client/switchbotClient.spec.ts +64 -0
- package/test/device/device-mapping.spec.ts +23 -0
- package/test/device/deviceBase.spec.ts +26 -0
- package/test/device/deviceFactory-edge.spec.ts +15 -0
- package/test/device/deviceFactory.spec.ts +33 -0
- package/test/device/fan-swing.spec.ts +34 -0
- package/test/device/genericDevice-blepoll.spec.ts +47 -0
- package/test/device/irdevice.spec.ts +9 -0
- package/test/device/lock-users.spec.ts +35 -0
- package/test/device/matter-descriptors.spec.ts +22 -0
- package/test/device/matter-device-state.spec.ts +37 -0
- package/test/e2e/run-e2e.spec.ts +18 -19
- package/test/errors/errors.spec.ts +10 -0
- package/test/helpers/matter-harness.ts +20 -9
- package/test/homebridge-ui/server.spec.ts +9 -0
- package/test/platform/accessory-restore.spec.ts +37 -0
- package/test/platform/matter-childbridge.spec.ts +34 -0
- package/test/platform/matter-integration.spec.ts +33 -0
- package/test/platform/platform-edge.spec.ts +73 -0
- package/test/platform/platform.integration.spec.ts +34 -0
- package/test/utils/utils-extra.spec.ts +10 -0
- package/test/utils/utils.spec.ts +53 -0
- package/todo/TODO.md +80 -0
- package/tsconfig.ui.json +11 -0
- package/.github/npm-version-script-esm.js +0 -97
- package/.github/workflows/beta-release.yml +0 -52
- package/dist/platform.d.ts +0 -35
- package/dist/platform.d.ts.map +0 -1
- package/dist/platform.js +0 -945
- package/dist/platform.js.map +0 -1
- package/src/platform.ts +0 -963
- package/test/accessory-restore.spec.ts +0 -73
- package/test/device-mapping.spec.ts +0 -37
- package/test/deviceFactory.spec.ts +0 -18
- package/test/fan-swing.spec.ts +0 -29
- package/test/lock-users.spec.ts +0 -44
- package/test/matter-childbridge.spec.ts +0 -55
- package/test/matter-descriptors.spec.ts +0 -97
- package/test/matter-device-state.spec.ts +0 -101
- package/test/matter-integration.spec.ts +0 -70
- package/test/platform.integration.spec.ts +0 -55
- package/test/switchbot-client-debounce.spec.ts +0 -131
- package/test/switchbot-client-openapi.spec.ts +0 -56
- package/test/switchbotClient.spec.ts +0 -10
- package/test/utils.spec.ts +0 -20
|
@@ -0,0 +1,3722 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __esm = (fn, res) => function __init() {
|
|
4
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
5
|
+
};
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/homebridge-ui/public/js/logger.ts
|
|
12
|
+
var PREFIX, uiLog;
|
|
13
|
+
var init_logger = __esm({
|
|
14
|
+
"src/homebridge-ui/public/js/logger.ts"() {
|
|
15
|
+
"use strict";
|
|
16
|
+
PREFIX = "[SwitchBot UI/html]";
|
|
17
|
+
uiLog = {
|
|
18
|
+
info: (message, ...parameters) => {
|
|
19
|
+
console.log(PREFIX, message, ...parameters);
|
|
20
|
+
},
|
|
21
|
+
warn: (message, ...parameters) => {
|
|
22
|
+
console.warn(PREFIX, message, ...parameters);
|
|
23
|
+
},
|
|
24
|
+
error: (message, ...parameters) => {
|
|
25
|
+
console.error(PREFIX, message, ...parameters);
|
|
26
|
+
},
|
|
27
|
+
debug: (message, ...parameters) => {
|
|
28
|
+
console.debug(PREFIX, message, ...parameters);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// src/homebridge-ui/public/js/types.ts
|
|
35
|
+
var init_types = __esm({
|
|
36
|
+
"src/homebridge-ui/public/js/types.ts"() {
|
|
37
|
+
"use strict";
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// src/homebridge-ui/public/js/modal.ts
|
|
42
|
+
function callUiMethod(name, ...args) {
|
|
43
|
+
try {
|
|
44
|
+
if (typeof homebridge?.[name] === "function") {
|
|
45
|
+
uiLog.info(`[callUiMethod] Invoking homebridge.${String(name)}()`);
|
|
46
|
+
const fn = homebridge?.[name];
|
|
47
|
+
if (typeof fn === "function") {
|
|
48
|
+
uiLog.info(`[callUiMethod] Invoking homebridge.${String(name)}()`);
|
|
49
|
+
fn.apply(homebridge, args);
|
|
50
|
+
} else {
|
|
51
|
+
uiLog.warn(`[callUiMethod] homebridge[${String(name)}] is not a function.`);
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
uiLog.warn(`[callUiMethod] homebridge[${String(name)}] is not a function.`);
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {
|
|
57
|
+
uiLog.warn(`Homebridge UI method ${String(name)} failed:`, e);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function showBusyUi() {
|
|
61
|
+
callUiMethod("disableSaveButton");
|
|
62
|
+
callUiMethod("showSpinner");
|
|
63
|
+
}
|
|
64
|
+
function hideBusyUi() {
|
|
65
|
+
callUiMethod("hideSpinner");
|
|
66
|
+
callUiMethod("enableSaveButton");
|
|
67
|
+
}
|
|
68
|
+
var init_modal = __esm({
|
|
69
|
+
"src/homebridge-ui/public/js/modal.ts"() {
|
|
70
|
+
"use strict";
|
|
71
|
+
init_types();
|
|
72
|
+
init_logger();
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// src/homebridge-ui/public/js/toast.ts
|
|
77
|
+
function showToast(method, message, title = "SwitchBot") {
|
|
78
|
+
try {
|
|
79
|
+
const hb = typeof window !== "undefined" ? window.homebridge : void 0;
|
|
80
|
+
const toast = hb && typeof hb.toast === "object" ? hb.toast : void 0;
|
|
81
|
+
const fn = toast && typeof toast[method] === "function" ? toast[method] : void 0;
|
|
82
|
+
if (fn) {
|
|
83
|
+
try {
|
|
84
|
+
fn(message, title);
|
|
85
|
+
return;
|
|
86
|
+
} catch (err) {
|
|
87
|
+
uiLog.warn(`Toast ${method} threw:`, err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
uiLog.info(`[Toast:${method}] ${title} - ${message}`);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
uiLog.warn(`Toast ${method} outer error:`, e);
|
|
93
|
+
uiLog.info(`[Toast:${method}] ${title} - ${message}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function toastSuccess(message, title) {
|
|
97
|
+
showToast("success", message, title);
|
|
98
|
+
}
|
|
99
|
+
function toastError(message, title) {
|
|
100
|
+
showToast("error", message, title);
|
|
101
|
+
}
|
|
102
|
+
function toastWarning(message, title) {
|
|
103
|
+
showToast("warning", message, title);
|
|
104
|
+
}
|
|
105
|
+
function toastInfo(message, title) {
|
|
106
|
+
showToast("info", message, title);
|
|
107
|
+
}
|
|
108
|
+
var init_toast = __esm({
|
|
109
|
+
"src/homebridge-ui/public/js/toast.ts"() {
|
|
110
|
+
"use strict";
|
|
111
|
+
init_types();
|
|
112
|
+
init_logger();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// src/device-types.js
|
|
117
|
+
function getValidDeviceTypes() {
|
|
118
|
+
const validTypes = /* @__PURE__ */ new Set();
|
|
119
|
+
for (const category of Object.values(DEVICE_TYPES)) {
|
|
120
|
+
for (const type of category) {
|
|
121
|
+
validTypes.add(type);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return validTypes;
|
|
125
|
+
}
|
|
126
|
+
function normalizeDeviceType(deviceType) {
|
|
127
|
+
if (!deviceType || typeof deviceType !== "string") {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
const trimmed = deviceType.trim();
|
|
131
|
+
const lowercase = trimmed.toLowerCase();
|
|
132
|
+
const validTypes = getValidDeviceTypes();
|
|
133
|
+
if (validTypes.has(trimmed)) {
|
|
134
|
+
return trimmed;
|
|
135
|
+
}
|
|
136
|
+
const normalized = DEVICE_TYPE_NORMALIZATION_MAP[lowercase];
|
|
137
|
+
if (normalized && validTypes.has(normalized)) {
|
|
138
|
+
return normalized;
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
function isValidDeviceType(deviceType) {
|
|
143
|
+
if (!deviceType || typeof deviceType !== "string") {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
const validTypes = getValidDeviceTypes();
|
|
147
|
+
return validTypes.has(deviceType.trim());
|
|
148
|
+
}
|
|
149
|
+
var DEVICE_TYPES, DEVICE_TYPE_NORMALIZATION_MAP;
|
|
150
|
+
var init_device_types = __esm({
|
|
151
|
+
"src/device-types.js"() {
|
|
152
|
+
"use strict";
|
|
153
|
+
DEVICE_TYPES = {
|
|
154
|
+
"Window Coverings": ["Blind Tilt", "Curtain", "Curtain3", "Roller Shade"],
|
|
155
|
+
"Locks & Access": [
|
|
156
|
+
"Keypad",
|
|
157
|
+
"Keypad Touch",
|
|
158
|
+
"Keypad Vision",
|
|
159
|
+
"Keypad Vision Pro",
|
|
160
|
+
"Lock Vision Pro",
|
|
161
|
+
"Lock Lite",
|
|
162
|
+
"Smart Lock",
|
|
163
|
+
"Smart Lock Pro",
|
|
164
|
+
"Smart Lock Ultra",
|
|
165
|
+
"Video Doorbell"
|
|
166
|
+
],
|
|
167
|
+
"Sensors": ["Contact Sensor", "Motion Sensor", "Presence Sensor", "Water Detector"],
|
|
168
|
+
"Lighting": [
|
|
169
|
+
"Candle Warmer Lamp",
|
|
170
|
+
"Ceiling Light",
|
|
171
|
+
"Ceiling Light Pro",
|
|
172
|
+
"Color Bulb",
|
|
173
|
+
"Floor Lamp",
|
|
174
|
+
"RGBIC Neon Rope Light",
|
|
175
|
+
"RGBIC Neon Wire Rope Light",
|
|
176
|
+
"RGBICWW Floor Lamp",
|
|
177
|
+
"RGBICWW Strip Light",
|
|
178
|
+
"Strip Light",
|
|
179
|
+
"Strip Light 3"
|
|
180
|
+
],
|
|
181
|
+
"Climate Control": [
|
|
182
|
+
"Air Purifier PM2.5",
|
|
183
|
+
"Air Purifier Table PM2.5",
|
|
184
|
+
"Air Purifier VOC",
|
|
185
|
+
"Air Purifier Table VOC",
|
|
186
|
+
"Battery Circulator Fan",
|
|
187
|
+
"Circulator Fan",
|
|
188
|
+
"Humidifier",
|
|
189
|
+
"Humidifier2",
|
|
190
|
+
"Meter",
|
|
191
|
+
"MeterPlus",
|
|
192
|
+
"Meter Plus",
|
|
193
|
+
"MeterPro",
|
|
194
|
+
"Meter Pro",
|
|
195
|
+
"MeterPro(CO2)",
|
|
196
|
+
"Meter Pro (CO2)",
|
|
197
|
+
"Smart Radiator Thermostat",
|
|
198
|
+
"Standing Circulator Fan",
|
|
199
|
+
"WoIOSensor"
|
|
200
|
+
],
|
|
201
|
+
"Plugs & Switches": [
|
|
202
|
+
"Garage Door Opener",
|
|
203
|
+
"Plug",
|
|
204
|
+
"Plug Mini (EU)",
|
|
205
|
+
"Plug Mini (JP)",
|
|
206
|
+
"Plug Mini (US)",
|
|
207
|
+
"Relay Switch 1",
|
|
208
|
+
"Relay Switch 1PM",
|
|
209
|
+
"Relay Switch 2PM"
|
|
210
|
+
],
|
|
211
|
+
"Robot Vacuums": [
|
|
212
|
+
"K10+",
|
|
213
|
+
"K10+ Pro",
|
|
214
|
+
"Robot Vacuum Cleaner K10+ Pro Combo",
|
|
215
|
+
"Robot Vacuum Cleaner K11+",
|
|
216
|
+
"Robot Vacuum Cleaner K20 Plus Pro",
|
|
217
|
+
"Robot Vacuum Cleaner S1",
|
|
218
|
+
"Robot Vacuum Cleaner S1 Plus",
|
|
219
|
+
"Robot Vacuum Cleaner S10",
|
|
220
|
+
"Robot Vacuum Cleaner S20"
|
|
221
|
+
],
|
|
222
|
+
"Hubs": ["AI Hub", "Hub", "Hub 2", "Hub 3", "Hub Mini", "Hub Plus"],
|
|
223
|
+
"Cameras": [
|
|
224
|
+
"Indoor Cam",
|
|
225
|
+
"Pan/Tilt Cam",
|
|
226
|
+
"Pan/Tilt Cam 2K",
|
|
227
|
+
"Pan/Tilt Cam Plus 2K",
|
|
228
|
+
"Pan/Tilt Cam Plus 3K"
|
|
229
|
+
],
|
|
230
|
+
"IR Devices": [
|
|
231
|
+
"Air Conditioner",
|
|
232
|
+
"Air Purifier",
|
|
233
|
+
"Camera",
|
|
234
|
+
"DVD",
|
|
235
|
+
"Fan",
|
|
236
|
+
"Light",
|
|
237
|
+
"Others",
|
|
238
|
+
"Projector",
|
|
239
|
+
"Set Top Box",
|
|
240
|
+
"Speaker",
|
|
241
|
+
"Streamer",
|
|
242
|
+
"TV",
|
|
243
|
+
"Vacuum Cleaner",
|
|
244
|
+
"Water Heater"
|
|
245
|
+
],
|
|
246
|
+
"Other Devices": ["AI Art Frame", "Bot", "Home Climate Panel", "Remote", "remote with screen"]
|
|
247
|
+
};
|
|
248
|
+
DEVICE_TYPE_NORMALIZATION_MAP = {
|
|
249
|
+
// --- node-switchbot v4 normalization additions ---
|
|
250
|
+
"hub mini": "Hub Mini",
|
|
251
|
+
"hub 3": "Hub 3",
|
|
252
|
+
"keypad": "Keypad",
|
|
253
|
+
"plug mini": "Plug Mini (US)",
|
|
254
|
+
// fallback to US if region not specified
|
|
255
|
+
"art frame": "AI Art Frame",
|
|
256
|
+
"rgbicww": "RGBICWW Strip Light",
|
|
257
|
+
"lock vision": "Lock Vision Pro",
|
|
258
|
+
// alias for new lock vision
|
|
259
|
+
"lock pro": "Smart Lock Pro",
|
|
260
|
+
"lock lite": "Lock Lite",
|
|
261
|
+
"circulator fan": "Circulator Fan",
|
|
262
|
+
"smart thermostat radiator": "Smart Radiator Thermostat",
|
|
263
|
+
"climate panel": "Home Climate Panel",
|
|
264
|
+
"evaporative humidifier": "Humidifier",
|
|
265
|
+
// --- end node-switchbot v4 additions ---
|
|
266
|
+
// Only keep the last occurrence for each key, all values canonical
|
|
267
|
+
"air purifier pm2.5": "Air Purifier PM2.5",
|
|
268
|
+
"pan/tilt cam plus 3k": "Pan/Tilt Cam Plus 3K",
|
|
269
|
+
"remote with screen": "Remote with Screen",
|
|
270
|
+
"ai hub": "AI Hub",
|
|
271
|
+
"water detector": "Water Detector",
|
|
272
|
+
"video doorbell": "Video Doorbell",
|
|
273
|
+
"smart radiator thermostat": "Smart Radiator Thermostat",
|
|
274
|
+
"woiosensor": "WoIOSensor",
|
|
275
|
+
"garage door opener": "Garage Door Opener",
|
|
276
|
+
"air purifier table pm2.5": "Air Purifier Table PM2.5",
|
|
277
|
+
"air purifier voc": "Air Purifier VOC",
|
|
278
|
+
"air purifier table voc": "Air Purifier Table VOC",
|
|
279
|
+
"plug mini (eu)": "Plug Mini (EU)",
|
|
280
|
+
// Only last occurrence for each key is kept above. Removed duplicates here.
|
|
281
|
+
// Climate control conversions
|
|
282
|
+
"humidifier2": "Humidifier2",
|
|
283
|
+
"battery circulator fan": "Battery Circulator Fan",
|
|
284
|
+
"standing circulator fan": "Standing Circulator Fan",
|
|
285
|
+
// Lock/keypad conversions
|
|
286
|
+
"smart lock": "Smart Lock",
|
|
287
|
+
"smart lock pro": "Smart Lock Pro",
|
|
288
|
+
"smart lock ultra": "Smart Lock Ultra",
|
|
289
|
+
"keypad touch": "Keypad Touch",
|
|
290
|
+
"keypad vision": "Keypad Vision",
|
|
291
|
+
"keypad vision pro": "Keypad Vision Pro",
|
|
292
|
+
// Light conversions
|
|
293
|
+
"color bulb": "Color Bulb",
|
|
294
|
+
"ceiling light": "Ceiling Light",
|
|
295
|
+
"ceiling light pro": "Ceiling Light Pro",
|
|
296
|
+
"candle warmer lamp": "Candle Warmer Lamp",
|
|
297
|
+
"floor lamp": "Floor Lamp",
|
|
298
|
+
"rgbic neon rope light": "RGBIC Neon Rope Light",
|
|
299
|
+
"rgbic neon wire rope light": "RGBIC Neon Wire Rope Light",
|
|
300
|
+
"rgbicww floor lamp": "RGBICWW Floor Lamp",
|
|
301
|
+
"rgbicww strip light": "RGBICWW Strip Light",
|
|
302
|
+
"strip light": "Strip Light",
|
|
303
|
+
"strip light 3": "Strip Light 3",
|
|
304
|
+
// Vacuum conversions
|
|
305
|
+
"robot vacuum cleaner s1": "Robot Vacuum Cleaner S1",
|
|
306
|
+
"robot vacuum cleaner s1 plus": "Robot Vacuum Cleaner S1 Plus",
|
|
307
|
+
"robot vacuum cleaner s10": "Robot Vacuum Cleaner S10",
|
|
308
|
+
"robot vacuum cleaner s20": "Robot Vacuum Cleaner S20",
|
|
309
|
+
"robot vacuum cleaner k10+ pro combo": "Robot Vacuum Cleaner K10+ Pro Combo",
|
|
310
|
+
"robot vacuum cleaner k11+": "Robot Vacuum Cleaner K11+",
|
|
311
|
+
"robot vacuum cleaner k20 plus pro": "Robot Vacuum Cleaner K20 Plus Pro",
|
|
312
|
+
// Exact device type mappings (API format → canonical format)
|
|
313
|
+
"relay switch 1": "Relay Switch 1",
|
|
314
|
+
"blind tilt": "Blind Tilt",
|
|
315
|
+
"roller shade": "Roller Shade",
|
|
316
|
+
"curtain3": "Curtain3",
|
|
317
|
+
"hub 2": "Hub 2",
|
|
318
|
+
"meterplus": "MeterPlus",
|
|
319
|
+
"meterpro": "MeterPro",
|
|
320
|
+
"meterpro(co2)": "MeterPro(CO2)",
|
|
321
|
+
"walletfinder": "WalletFinder",
|
|
322
|
+
"k10+": "K10+",
|
|
323
|
+
"k10+ pro (wosweeperminipro)": "K10+ Pro (wosweeperminipro)",
|
|
324
|
+
// Handle spaced variants from config files (normalize back to canonical type)
|
|
325
|
+
"meter pro": "Meter Pro",
|
|
326
|
+
"meter pro (co2)": "Meter Pro (CO2)",
|
|
327
|
+
"meter plus": "Meter Plus",
|
|
328
|
+
"relay switch 1 pm": "Relay Switch 1PM",
|
|
329
|
+
"relay switch 2 pm": "Relay Switch 2PM",
|
|
330
|
+
"plug mini eu": "Plug Mini (EU)",
|
|
331
|
+
"plug mini jp": "Plug Mini (JP)",
|
|
332
|
+
"plug mini us": "Plug Mini (US)",
|
|
333
|
+
// Migration mappings for invalid/legacy device types
|
|
334
|
+
"lock vision pro": "Lock Vision Pro",
|
|
335
|
+
// Valid alias; map to canonical
|
|
336
|
+
// 'lock vision': 'Keypad Vision', // Invalid type (removed, now alias above)
|
|
337
|
+
"lock touch": "Keypad Touch",
|
|
338
|
+
// Invalid type
|
|
339
|
+
// Additional normalization for new/unknown types from logs
|
|
340
|
+
"woplugus": "Plug Mini (US)"
|
|
341
|
+
// Removed duplicate keys below, only last occurrence kept
|
|
342
|
+
// 'plug mini us': 'plug mini (us)', // duplicate, removed
|
|
343
|
+
// 'plug us': 'plug mini (us)', // duplicate, removed
|
|
344
|
+
// 'plug': 'plug', // duplicate, removed
|
|
345
|
+
// 'air purifier pm2.5': 'air purifier pm2.5', // duplicate, removed
|
|
346
|
+
// 'rgbic neon wire rope light': 'rgbic neon wire rope light', // duplicate, removed
|
|
347
|
+
// 'candle warmer lamp': 'candle warmer lamp', // duplicate, removed
|
|
348
|
+
// 'pan/tilt cam plus 3k': 'pan/tilt cam plus 3k', // duplicate, removed
|
|
349
|
+
// 'remote with screen': 'remote with screen', // duplicate, removed
|
|
350
|
+
// 'ai hub': 'ai hub', // duplicate, removed
|
|
351
|
+
// 'lock vision pro': 'lock vision pro', // duplicate, remove this line
|
|
352
|
+
// Add any other device types from logs as needed
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
// src/homebridge-ui/public/js/api.ts
|
|
358
|
+
var api_exports = {};
|
|
359
|
+
__export(api_exports, {
|
|
360
|
+
addDevice: () => addDevice,
|
|
361
|
+
addDevicesInBulk: () => addDevicesInBulk,
|
|
362
|
+
deleteAllDevices: () => deleteAllDevices,
|
|
363
|
+
deleteDevice: () => deleteDevice,
|
|
364
|
+
discoverDevices: () => discoverDevices,
|
|
365
|
+
fetchBluetoothStatus: () => fetchBluetoothStatus,
|
|
366
|
+
fetchCredentialStatus: () => fetchCredentialStatus,
|
|
367
|
+
fetchDevices: () => fetchDevices,
|
|
368
|
+
saveCredentials: () => saveCredentials2,
|
|
369
|
+
syncParentPluginConfigFromDisk: () => syncParentPluginConfigFromDisk,
|
|
370
|
+
testDeviceConnection: () => testDeviceConnection,
|
|
371
|
+
updateDevice: () => updateDevice,
|
|
372
|
+
validateAndFixDeviceTypes: () => validateAndFixDeviceTypes
|
|
373
|
+
});
|
|
374
|
+
async function fetchDevices() {
|
|
375
|
+
try {
|
|
376
|
+
if (typeof homebridge.getPluginConfig !== "function") {
|
|
377
|
+
throw new TypeError("Homebridge UI API not available");
|
|
378
|
+
}
|
|
379
|
+
const configArr = await homebridge.getPluginConfig();
|
|
380
|
+
const config = Array.isArray(configArr) && configArr.length > 0 ? configArr.find(isSwitchBotPlatformConfig) : null;
|
|
381
|
+
if (!config || !Array.isArray(config.devices)) {
|
|
382
|
+
return [];
|
|
383
|
+
}
|
|
384
|
+
return config.devices;
|
|
385
|
+
} catch (e) {
|
|
386
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
387
|
+
uiLog.error("Error fetching devices:", msg);
|
|
388
|
+
return [];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function validateAndFixDeviceTypes(devices) {
|
|
392
|
+
const errors = [];
|
|
393
|
+
for (const d of devices) {
|
|
394
|
+
if (!isValidDeviceType(d.configDeviceType)) {
|
|
395
|
+
const fixed = normalizeDeviceType(d.configDeviceType);
|
|
396
|
+
if (fixed) {
|
|
397
|
+
d.configDeviceType = fixed;
|
|
398
|
+
} else {
|
|
399
|
+
errors.push({
|
|
400
|
+
deviceId: d.deviceId,
|
|
401
|
+
name: d.configDeviceName,
|
|
402
|
+
type: d.configDeviceType
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return errors;
|
|
408
|
+
}
|
|
409
|
+
function isSwitchBotPlatformConfig(block) {
|
|
410
|
+
const platformName = String(block?.platform || block?.name || "").toLowerCase();
|
|
411
|
+
return platformName === "switchbot" || platformName === "@switchbot/homebridge-switchbot" || platformName.includes("switchbot");
|
|
412
|
+
}
|
|
413
|
+
async function syncParentPluginConfigFromDisk(autoSave = false) {
|
|
414
|
+
try {
|
|
415
|
+
if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") {
|
|
416
|
+
uiLog.warn("Parent config sync API not available");
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
const pluginConfigBlocks = await homebridge.getPluginConfig();
|
|
420
|
+
if (!Array.isArray(pluginConfigBlocks) || !pluginConfigBlocks.length) {
|
|
421
|
+
uiLog.warn("No plugin config blocks returned from Homebridge");
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
const index = pluginConfigBlocks.findIndex((block) => isSwitchBotPlatformConfig(block));
|
|
425
|
+
if (index < 0) {
|
|
426
|
+
uiLog.warn("SwitchBot platform block not found in Homebridge plugin config");
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
const errors = validateAndFixDeviceTypes(pluginConfigBlocks[index].devices || []);
|
|
430
|
+
if (errors.length > 0) {
|
|
431
|
+
toastError(`Invalid device types found: ${errors.map((e) => `${e.name} (${e.type})`).join(", ")}`);
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
await homebridge.updatePluginConfig(pluginConfigBlocks);
|
|
435
|
+
if (autoSave && typeof homebridge.savePluginConfig === "function") {
|
|
436
|
+
uiLog.info("Auto-saving config to disk...");
|
|
437
|
+
await homebridge.savePluginConfig();
|
|
438
|
+
uiLog.info("Config saved successfully");
|
|
439
|
+
}
|
|
440
|
+
return true;
|
|
441
|
+
} catch (e) {
|
|
442
|
+
uiLog.warn("Failed to sync parent plugin config cache:", e);
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
async function fetchCredentialStatus() {
|
|
447
|
+
try {
|
|
448
|
+
const resp = await homebridge.request("/credentials", {});
|
|
449
|
+
uiLog.info("Load credentials response:", resp);
|
|
450
|
+
if (!resp || resp.success === false) {
|
|
451
|
+
uiLog.error("Failed to load credentials:", resp);
|
|
452
|
+
return null;
|
|
453
|
+
}
|
|
454
|
+
return resp.data || {};
|
|
455
|
+
} catch (e) {
|
|
456
|
+
uiLog.error("Error loading credentials:", e);
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
async function saveCredentials2(token, secret) {
|
|
461
|
+
uiLog.info("Saving credentials...");
|
|
462
|
+
const resp = await homebridge.request("/credentials", { token, secret });
|
|
463
|
+
uiLog.info("Save response:", resp);
|
|
464
|
+
if (!resp || resp.success === false) {
|
|
465
|
+
throw new Error(resp?.message || "Save failed");
|
|
466
|
+
}
|
|
467
|
+
return resp.data || resp;
|
|
468
|
+
}
|
|
469
|
+
async function discoverDevices(mode = "all", options) {
|
|
470
|
+
const resp = await homebridge.request("/discover", { mode, ...options });
|
|
471
|
+
uiLog.info("Discover response:", resp);
|
|
472
|
+
if (!resp || resp.success === false) {
|
|
473
|
+
throw new Error(resp?.data?.message || "Discovery failed");
|
|
474
|
+
}
|
|
475
|
+
return resp.data || [];
|
|
476
|
+
}
|
|
477
|
+
async function fetchBluetoothStatus() {
|
|
478
|
+
try {
|
|
479
|
+
const resp = await homebridge.request("/ble-status", {});
|
|
480
|
+
if (!resp || resp.success === false) {
|
|
481
|
+
return { available: false, message: "Bluetooth status unavailable" };
|
|
482
|
+
}
|
|
483
|
+
return resp.data || { available: false, message: "Bluetooth status unavailable" };
|
|
484
|
+
} catch (_e) {
|
|
485
|
+
return { available: false, message: "Bluetooth status unavailable" };
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
async function testDeviceConnection(payload) {
|
|
489
|
+
const resp = await homebridge.request("/test-connection", payload);
|
|
490
|
+
if (!resp || resp.success === false) {
|
|
491
|
+
throw new Error(resp?.data?.message || "Connection test failed");
|
|
492
|
+
}
|
|
493
|
+
return resp.data || {
|
|
494
|
+
success: false,
|
|
495
|
+
deviceId: payload.deviceId,
|
|
496
|
+
method: "Auto",
|
|
497
|
+
latencyMs: 0,
|
|
498
|
+
message: "Connection test failed"
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
async function addDevice(deviceId, name, type, options) {
|
|
502
|
+
if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") {
|
|
503
|
+
throw new TypeError("Homebridge UI API not available");
|
|
504
|
+
}
|
|
505
|
+
const configArr = await homebridge.getPluginConfig();
|
|
506
|
+
const idx = Array.isArray(configArr) ? configArr.findIndex(isSwitchBotPlatformConfig) : -1;
|
|
507
|
+
if (idx === -1) {
|
|
508
|
+
throw new Error("SwitchBot config not found");
|
|
509
|
+
}
|
|
510
|
+
const config = configArr[idx];
|
|
511
|
+
if (!Array.isArray(config.devices)) {
|
|
512
|
+
config.devices = [];
|
|
513
|
+
}
|
|
514
|
+
const normalizedDeviceId = String(deviceId).trim().toLowerCase();
|
|
515
|
+
const exists = config.devices.some((d) => String(d.deviceId ?? d.id ?? "").trim().toLowerCase() === normalizedDeviceId);
|
|
516
|
+
if (exists) {
|
|
517
|
+
return { alreadyExists: true, message: "Device already in config" };
|
|
518
|
+
}
|
|
519
|
+
const newDevice = { deviceId, configDeviceName: name, configDeviceType: type };
|
|
520
|
+
if (options?.address) {
|
|
521
|
+
newDevice.address = options.address;
|
|
522
|
+
}
|
|
523
|
+
if (options?.model) {
|
|
524
|
+
newDevice.model = options.model;
|
|
525
|
+
}
|
|
526
|
+
if (options?.rssi !== void 0 && options?.rssi !== null && options?.rssi !== 0) {
|
|
527
|
+
newDevice.rssi = options.rssi;
|
|
528
|
+
}
|
|
529
|
+
if (options?.encryptionKey) {
|
|
530
|
+
newDevice.encryptionKey = options.encryptionKey;
|
|
531
|
+
}
|
|
532
|
+
if (options?.keyId) {
|
|
533
|
+
newDevice.keyId = options.keyId;
|
|
534
|
+
}
|
|
535
|
+
config.devices.push(newDevice);
|
|
536
|
+
await homebridge.updatePluginConfig(configArr);
|
|
537
|
+
if (typeof homebridge.savePluginConfig === "function") {
|
|
538
|
+
await homebridge.savePluginConfig();
|
|
539
|
+
}
|
|
540
|
+
return { added: true, message: `Device "${name}" added successfully` };
|
|
541
|
+
}
|
|
542
|
+
async function addDevicesInBulk(devices) {
|
|
543
|
+
const resp = await homebridge.request("/add-devices", { devices });
|
|
544
|
+
uiLog.info("Bulk add response:", resp);
|
|
545
|
+
if (!resp || resp.success === false) {
|
|
546
|
+
throw new Error(resp?.data?.message || "Bulk add failed");
|
|
547
|
+
}
|
|
548
|
+
return resp.data || resp;
|
|
549
|
+
}
|
|
550
|
+
async function updateDevice(deviceId, configDeviceName, configDeviceType, options) {
|
|
551
|
+
if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") {
|
|
552
|
+
throw new TypeError("Homebridge UI API not available");
|
|
553
|
+
}
|
|
554
|
+
const configArr = await homebridge.getPluginConfig();
|
|
555
|
+
const idx = Array.isArray(configArr) ? configArr.findIndex(isSwitchBotPlatformConfig) : -1;
|
|
556
|
+
if (idx === -1) {
|
|
557
|
+
throw new Error("SwitchBot config not found");
|
|
558
|
+
}
|
|
559
|
+
const config = configArr[idx];
|
|
560
|
+
if (!Array.isArray(config.devices)) {
|
|
561
|
+
throw new TypeError("No devices array in config");
|
|
562
|
+
}
|
|
563
|
+
const normalizedDeviceId = String(deviceId).trim().toLowerCase();
|
|
564
|
+
const device = config.devices.find((d) => String(d.deviceId ?? d.id ?? "").trim().toLowerCase() === normalizedDeviceId);
|
|
565
|
+
if (!device) {
|
|
566
|
+
throw new Error("Device not found in config");
|
|
567
|
+
}
|
|
568
|
+
if (configDeviceName) {
|
|
569
|
+
device.configDeviceName = configDeviceName;
|
|
570
|
+
}
|
|
571
|
+
if (configDeviceType) {
|
|
572
|
+
device.configDeviceType = configDeviceType;
|
|
573
|
+
}
|
|
574
|
+
if (options) {
|
|
575
|
+
Object.assign(device, options);
|
|
576
|
+
}
|
|
577
|
+
await homebridge.updatePluginConfig(configArr);
|
|
578
|
+
if (typeof homebridge.savePluginConfig === "function") {
|
|
579
|
+
await homebridge.savePluginConfig();
|
|
580
|
+
}
|
|
581
|
+
return { updated: true, message: `Device updated successfully` };
|
|
582
|
+
}
|
|
583
|
+
async function deleteDevice(deviceId) {
|
|
584
|
+
if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") {
|
|
585
|
+
throw new TypeError("Homebridge UI API not available");
|
|
586
|
+
}
|
|
587
|
+
const configArr = await homebridge.getPluginConfig();
|
|
588
|
+
const idx = Array.isArray(configArr) ? configArr.findIndex(isSwitchBotPlatformConfig) : -1;
|
|
589
|
+
if (idx === -1) {
|
|
590
|
+
throw new Error("SwitchBot config not found");
|
|
591
|
+
}
|
|
592
|
+
const config = configArr[idx];
|
|
593
|
+
if (!Array.isArray(config.devices)) {
|
|
594
|
+
throw new TypeError("No devices array in config");
|
|
595
|
+
}
|
|
596
|
+
const normalizedDeviceId = String(deviceId).trim().toLowerCase();
|
|
597
|
+
const before = config.devices.length;
|
|
598
|
+
config.devices = config.devices.filter((d) => String(d.deviceId ?? d.id ?? "").trim().toLowerCase() !== normalizedDeviceId);
|
|
599
|
+
config.devices = config.devices.filter((d) => d && typeof d === "object" && d.deviceId && d.configDeviceType);
|
|
600
|
+
if (config.devices.length === before) {
|
|
601
|
+
throw new Error("Device not found in config");
|
|
602
|
+
}
|
|
603
|
+
await homebridge.updatePluginConfig(configArr);
|
|
604
|
+
if (typeof homebridge.savePluginConfig === "function") {
|
|
605
|
+
await homebridge.savePluginConfig();
|
|
606
|
+
}
|
|
607
|
+
return { deleted: true, message: `Device removed from config` };
|
|
608
|
+
}
|
|
609
|
+
async function deleteAllDevices() {
|
|
610
|
+
if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") {
|
|
611
|
+
throw new TypeError("Homebridge UI API not available");
|
|
612
|
+
}
|
|
613
|
+
const configArr = await homebridge.getPluginConfig();
|
|
614
|
+
let idx = Array.isArray(configArr) ? configArr.findIndex(isSwitchBotPlatformConfig) : -1;
|
|
615
|
+
if (idx === -1) {
|
|
616
|
+
const newBlock = { platform: "SwitchBot", devices: [] };
|
|
617
|
+
configArr.push(newBlock);
|
|
618
|
+
idx = configArr.length - 1;
|
|
619
|
+
}
|
|
620
|
+
const config = configArr[idx];
|
|
621
|
+
if (!Array.isArray(config.devices)) {
|
|
622
|
+
config.devices = [];
|
|
623
|
+
}
|
|
624
|
+
const deletedCount = config.devices.length;
|
|
625
|
+
config.devices = [];
|
|
626
|
+
if (!config.platform) {
|
|
627
|
+
config.platform = "SwitchBot";
|
|
628
|
+
}
|
|
629
|
+
if (!config.name) {
|
|
630
|
+
config.name = "SwitchBot";
|
|
631
|
+
}
|
|
632
|
+
await homebridge.updatePluginConfig(configArr);
|
|
633
|
+
if (typeof homebridge.savePluginConfig === "function") {
|
|
634
|
+
await homebridge.savePluginConfig();
|
|
635
|
+
}
|
|
636
|
+
return { deleted: true, deletedCount, message: `Removed ${deletedCount} device(s) from config` };
|
|
637
|
+
}
|
|
638
|
+
var init_api = __esm({
|
|
639
|
+
"src/homebridge-ui/public/js/api.ts"() {
|
|
640
|
+
"use strict";
|
|
641
|
+
init_device_types();
|
|
642
|
+
init_types();
|
|
643
|
+
init_logger();
|
|
644
|
+
init_toast();
|
|
645
|
+
}
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
// src/homebridge-ui/public/js/discovery.ts
|
|
649
|
+
var discovery_exports = {};
|
|
650
|
+
__export(discovery_exports, {
|
|
651
|
+
addDeviceToConfig: () => addDeviceToConfig,
|
|
652
|
+
discoverDevices: () => discoverDevices2,
|
|
653
|
+
initializeDiscoverySettings: () => initializeDiscoverySettings
|
|
654
|
+
});
|
|
655
|
+
function normalizeId(value) {
|
|
656
|
+
return String(value ?? "").trim().toLowerCase();
|
|
657
|
+
}
|
|
658
|
+
function dedupeById(devices) {
|
|
659
|
+
return devices.filter((d, index, arr) => !!d?.id && arr.findIndex((x) => x?.id === d.id) === index);
|
|
660
|
+
}
|
|
661
|
+
function mergeDiscoveredDevices(existingDevices, incomingDevices) {
|
|
662
|
+
const deviceMap = /* @__PURE__ */ new Map();
|
|
663
|
+
for (const d of dedupeById(existingDevices)) {
|
|
664
|
+
deviceMap.set(d.id, { ...d });
|
|
665
|
+
}
|
|
666
|
+
for (const d of dedupeById(incomingDevices)) {
|
|
667
|
+
const current = deviceMap.get(d.id);
|
|
668
|
+
if (current) {
|
|
669
|
+
let nextConnectionType = current.connectionType;
|
|
670
|
+
if (current.connectionType && d.connectionType && current.connectionType !== d.connectionType) {
|
|
671
|
+
const types = [current.connectionType, d.connectionType].sort().join(",");
|
|
672
|
+
if (types === "BLE,OpenAPI" || types === "OpenAPI,BLE") {
|
|
673
|
+
nextConnectionType = "Both";
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
deviceMap.set(d.id, {
|
|
677
|
+
...current,
|
|
678
|
+
...d,
|
|
679
|
+
connectionType: nextConnectionType
|
|
680
|
+
});
|
|
681
|
+
} else {
|
|
682
|
+
deviceMap.set(d.id, { ...d });
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
const merged = [...deviceMap.values()];
|
|
686
|
+
if (merged.length > 0) {
|
|
687
|
+
console.warn("[SwitchBot][Discovery][mergeDiscoveredDevices] Merged device sample:", merged[0]);
|
|
688
|
+
console.warn("[SwitchBot][Discovery][mergeDiscoveredDevices] Total merged devices:", merged.length);
|
|
689
|
+
}
|
|
690
|
+
return merged;
|
|
691
|
+
}
|
|
692
|
+
function setDiscoveryCache(devices) {
|
|
693
|
+
try {
|
|
694
|
+
const payload = { timestamp: Date.now(), devices };
|
|
695
|
+
localStorage.setItem(DISCOVERY_CACHE_KEY, JSON.stringify(payload));
|
|
696
|
+
} catch (_e) {
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
function clearDiscoveryCache() {
|
|
700
|
+
try {
|
|
701
|
+
localStorage.removeItem(DISCOVERY_CACHE_KEY);
|
|
702
|
+
} catch (_e) {
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
function getDiscoveryCache(validOnly = true) {
|
|
706
|
+
try {
|
|
707
|
+
const stored = localStorage.getItem(DISCOVERY_CACHE_KEY);
|
|
708
|
+
if (!stored) {
|
|
709
|
+
return null;
|
|
710
|
+
}
|
|
711
|
+
const payload = JSON.parse(stored);
|
|
712
|
+
if (!payload || !Array.isArray(payload.devices) || typeof payload.timestamp !== "number") {
|
|
713
|
+
return null;
|
|
714
|
+
}
|
|
715
|
+
const age = Date.now() - payload.timestamp;
|
|
716
|
+
if (validOnly && age > DISCOVERY_CACHE_TTL_MS) {
|
|
717
|
+
return null;
|
|
718
|
+
}
|
|
719
|
+
return payload;
|
|
720
|
+
} catch (_e) {
|
|
721
|
+
return null;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
function getDiscoveryAutoRefreshSeconds() {
|
|
725
|
+
try {
|
|
726
|
+
const stored = localStorage.getItem(DISCOVERY_AUTO_REFRESH_KEY);
|
|
727
|
+
const value = Number(stored || 0);
|
|
728
|
+
return Number.isFinite(value) && value >= 0 ? value : 0;
|
|
729
|
+
} catch (_e) {
|
|
730
|
+
return 0;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
function setDiscoveryAutoRefreshSeconds(value) {
|
|
734
|
+
try {
|
|
735
|
+
localStorage.setItem(DISCOVERY_AUTO_REFRESH_KEY, String(Math.max(0, value)));
|
|
736
|
+
} catch (_e) {
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
function getDiscoveryHideAddedPreference() {
|
|
740
|
+
try {
|
|
741
|
+
return localStorage.getItem(DISCOVERY_HIDE_ADDED_KEY) === "true";
|
|
742
|
+
} catch (_e) {
|
|
743
|
+
return false;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
function setDiscoveryHideAddedPreference(value) {
|
|
747
|
+
try {
|
|
748
|
+
localStorage.setItem(DISCOVERY_HIDE_ADDED_KEY, String(value));
|
|
749
|
+
} catch (_e) {
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
function formatElapsedShort(ms) {
|
|
753
|
+
const totalSeconds = Math.max(0, Math.floor(ms / 1e3));
|
|
754
|
+
if (totalSeconds < 60) {
|
|
755
|
+
return `${totalSeconds}s ago`;
|
|
756
|
+
}
|
|
757
|
+
const minutes = Math.floor(totalSeconds / 60);
|
|
758
|
+
if (minutes < 60) {
|
|
759
|
+
return `${minutes}m ago`;
|
|
760
|
+
}
|
|
761
|
+
const hours = Math.floor(minutes / 60);
|
|
762
|
+
if (hours < 24) {
|
|
763
|
+
return `${hours}h ago`;
|
|
764
|
+
}
|
|
765
|
+
const days = Math.floor(hours / 24);
|
|
766
|
+
return `${days}d ago`;
|
|
767
|
+
}
|
|
768
|
+
function updateLastScannedStatus() {
|
|
769
|
+
const lastScannedStatus = document.getElementById("lastScannedStatus");
|
|
770
|
+
if (!lastScannedStatus) {
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
const cache = getDiscoveryCache(false);
|
|
774
|
+
if (!cache) {
|
|
775
|
+
lastScannedStatus.textContent = "Last scanned: never";
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
const ageMs = Date.now() - cache.timestamp;
|
|
779
|
+
const timestampText = new Date(cache.timestamp).toLocaleString();
|
|
780
|
+
const stale = ageMs > DISCOVERY_CACHE_TTL_MS;
|
|
781
|
+
lastScannedStatus.textContent = stale ? `Last scanned: ${formatElapsedShort(ageMs)} (${timestampText}, cache expired)` : `Last scanned: ${formatElapsedShort(ageMs)} (${timestampText})`;
|
|
782
|
+
}
|
|
783
|
+
async function renderCachedDiscoveryResults() {
|
|
784
|
+
const cache = getDiscoveryCache(true);
|
|
785
|
+
const list = document.getElementById("discoveredList");
|
|
786
|
+
if (!cache || !list || !cache.devices.length) {
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
list.style.display = "block";
|
|
790
|
+
if (!window._discoverySelectedIds) {
|
|
791
|
+
window._discoverySelectedIds = /* @__PURE__ */ new Set();
|
|
792
|
+
}
|
|
793
|
+
await updateDiscoveryView(
|
|
794
|
+
cache.devices,
|
|
795
|
+
getDiscoveryPreferences(),
|
|
796
|
+
getDiscoveryGroupByPreference(),
|
|
797
|
+
getDiscoveryHideAddedPreference(),
|
|
798
|
+
window._discoverySelectedIds
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
function getDiscoveryBleSettings() {
|
|
802
|
+
try {
|
|
803
|
+
const stored = localStorage.getItem(DISCOVERY_BLE_SETTINGS_KEY);
|
|
804
|
+
if (!stored) {
|
|
805
|
+
return { bleEnabled: true, bleScanDurationSeconds: 5, bleTimeoutSeconds: 8 };
|
|
806
|
+
}
|
|
807
|
+
const parsed = JSON.parse(stored);
|
|
808
|
+
return {
|
|
809
|
+
bleEnabled: parsed?.bleEnabled !== false,
|
|
810
|
+
bleScanDurationSeconds: Math.max(3, Math.min(15, Number(parsed?.bleScanDurationSeconds || 5))),
|
|
811
|
+
bleTimeoutSeconds: Math.max(3, Math.min(30, Number(parsed?.bleTimeoutSeconds || 8)))
|
|
812
|
+
};
|
|
813
|
+
} catch (_e) {
|
|
814
|
+
return { bleEnabled: true, bleScanDurationSeconds: 5, bleTimeoutSeconds: 8 };
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function setDiscoveryBleSettings(settings) {
|
|
818
|
+
try {
|
|
819
|
+
localStorage.setItem(DISCOVERY_BLE_SETTINGS_KEY, JSON.stringify(settings));
|
|
820
|
+
} catch (_e) {
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
async function initializeDiscoverySettings() {
|
|
824
|
+
const scanSelect = document.getElementById("bleScanDurationSelect");
|
|
825
|
+
const timeoutInput = document.getElementById("bleTimeoutInput");
|
|
826
|
+
const disableBleCheckbox = document.getElementById("disableBleScanCheckbox");
|
|
827
|
+
const scanSetting = document.getElementById("bleScanSetting");
|
|
828
|
+
const timeoutSetting = document.getElementById("bleTimeoutSetting");
|
|
829
|
+
const bluetoothStatus = document.getElementById("bluetoothStatus");
|
|
830
|
+
const autoRefreshSelect = document.getElementById("autoRefreshIntervalSelect");
|
|
831
|
+
const refreshBtn = document.getElementById("refreshDiscoverBtn");
|
|
832
|
+
const current = getDiscoveryBleSettings();
|
|
833
|
+
if (scanSelect) {
|
|
834
|
+
scanSelect.value = String(current.bleScanDurationSeconds);
|
|
835
|
+
}
|
|
836
|
+
if (timeoutInput) {
|
|
837
|
+
timeoutInput.value = String(current.bleTimeoutSeconds);
|
|
838
|
+
}
|
|
839
|
+
if (disableBleCheckbox) {
|
|
840
|
+
disableBleCheckbox.checked = !current.bleEnabled;
|
|
841
|
+
}
|
|
842
|
+
if (autoRefreshSelect) {
|
|
843
|
+
autoRefreshSelect.value = String(getDiscoveryAutoRefreshSeconds());
|
|
844
|
+
}
|
|
845
|
+
const updateBleSettingVisibility = () => {
|
|
846
|
+
const disabled = !!disableBleCheckbox?.checked;
|
|
847
|
+
if (scanSetting) {
|
|
848
|
+
scanSetting.style.display = disabled ? "none" : "inline-flex";
|
|
849
|
+
}
|
|
850
|
+
if (timeoutSetting) {
|
|
851
|
+
timeoutSetting.style.display = disabled ? "none" : "inline-flex";
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
const persistFromControls = () => {
|
|
855
|
+
const next = {
|
|
856
|
+
bleEnabled: !(disableBleCheckbox?.checked ?? false),
|
|
857
|
+
bleScanDurationSeconds: Math.max(3, Math.min(15, Number(scanSelect?.value || 5))),
|
|
858
|
+
bleTimeoutSeconds: Math.max(3, Math.min(30, Number(timeoutInput?.value || 8)))
|
|
859
|
+
};
|
|
860
|
+
setDiscoveryBleSettings(next);
|
|
861
|
+
};
|
|
862
|
+
scanSelect?.addEventListener("change", persistFromControls);
|
|
863
|
+
timeoutInput?.addEventListener("change", persistFromControls);
|
|
864
|
+
disableBleCheckbox?.addEventListener("change", () => {
|
|
865
|
+
persistFromControls();
|
|
866
|
+
updateBleSettingVisibility();
|
|
867
|
+
});
|
|
868
|
+
updateBleSettingVisibility();
|
|
869
|
+
if (bluetoothStatus) {
|
|
870
|
+
const status = await fetchBluetoothStatus();
|
|
871
|
+
bluetoothStatus.textContent = status.available ? `Bluetooth: available (${status.message})` : `Bluetooth: unavailable (${status.message})`;
|
|
872
|
+
}
|
|
873
|
+
updateLastScannedStatus();
|
|
874
|
+
if (discoveryLastScannedTimer) {
|
|
875
|
+
clearInterval(discoveryLastScannedTimer);
|
|
876
|
+
}
|
|
877
|
+
discoveryLastScannedTimer = setInterval(updateLastScannedStatus, 15e3);
|
|
878
|
+
refreshBtn?.addEventListener("click", () => {
|
|
879
|
+
void discoverDevices2();
|
|
880
|
+
});
|
|
881
|
+
const applyAutoRefresh = () => {
|
|
882
|
+
const seconds = Math.max(0, Number(autoRefreshSelect?.value || 0));
|
|
883
|
+
setDiscoveryAutoRefreshSeconds(seconds);
|
|
884
|
+
if (discoveryAutoRefreshTimer) {
|
|
885
|
+
clearInterval(discoveryAutoRefreshTimer);
|
|
886
|
+
discoveryAutoRefreshTimer = null;
|
|
887
|
+
}
|
|
888
|
+
if (seconds > 0) {
|
|
889
|
+
discoveryAutoRefreshTimer = setInterval(() => {
|
|
890
|
+
const discoverBtn = document.getElementById("discoverBtn");
|
|
891
|
+
if (!discoverBtn || discoverBtn.disabled || document.hidden) {
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
void discoverDevices2();
|
|
895
|
+
}, seconds * 1e3);
|
|
896
|
+
}
|
|
897
|
+
};
|
|
898
|
+
autoRefreshSelect?.addEventListener("change", applyAutoRefresh);
|
|
899
|
+
applyAutoRefresh();
|
|
900
|
+
await renderCachedDiscoveryResults();
|
|
901
|
+
}
|
|
902
|
+
function getDiscoveryGroupByPreference() {
|
|
903
|
+
try {
|
|
904
|
+
const stored = localStorage.getItem(DISCOVERY_GROUP_BY_KEY);
|
|
905
|
+
if (stored === "hub" || stored === "type") {
|
|
906
|
+
return stored;
|
|
907
|
+
}
|
|
908
|
+
return "type";
|
|
909
|
+
} catch (_e) {
|
|
910
|
+
return "type";
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
function setDiscoveryGroupByPreference(groupBy) {
|
|
914
|
+
try {
|
|
915
|
+
localStorage.setItem(DISCOVERY_GROUP_BY_KEY, groupBy);
|
|
916
|
+
} catch (_e) {
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
function getDiscoveryGroupExpandedState() {
|
|
920
|
+
try {
|
|
921
|
+
const stored = localStorage.getItem(DISCOVERY_GROUP_EXPANDED_KEY);
|
|
922
|
+
if (!stored) {
|
|
923
|
+
return {};
|
|
924
|
+
}
|
|
925
|
+
const parsed = JSON.parse(stored);
|
|
926
|
+
return typeof parsed === "object" && parsed ? parsed : {};
|
|
927
|
+
} catch (_e) {
|
|
928
|
+
return {};
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
function setDiscoveryGroupExpandedState(state) {
|
|
932
|
+
try {
|
|
933
|
+
localStorage.setItem(DISCOVERY_GROUP_EXPANDED_KEY, JSON.stringify(state));
|
|
934
|
+
} catch (_e) {
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
function isDiscoveryGroupExpanded(groupKey) {
|
|
938
|
+
const state = getDiscoveryGroupExpandedState();
|
|
939
|
+
return state[groupKey] !== false;
|
|
940
|
+
}
|
|
941
|
+
function setDiscoveryGroupExpanded(groupKey, expanded) {
|
|
942
|
+
const state = getDiscoveryGroupExpandedState();
|
|
943
|
+
state[groupKey] = expanded;
|
|
944
|
+
setDiscoveryGroupExpandedState(state);
|
|
945
|
+
}
|
|
946
|
+
async function discoverDevices2() {
|
|
947
|
+
const btn = document.getElementById("discoverBtn");
|
|
948
|
+
const cancelBtn = document.getElementById("cancelDiscoverBtn");
|
|
949
|
+
const status = document.getElementById("discoverStatus");
|
|
950
|
+
const phaseProgress = document.getElementById("discoverPhaseProgress");
|
|
951
|
+
const phaseFill = document.getElementById("discoverPhaseFill");
|
|
952
|
+
const phaseLabel = document.getElementById("discoverPhaseLabel");
|
|
953
|
+
const list = document.getElementById("discoveredList");
|
|
954
|
+
const autoAddAll = document.getElementById("autoAddAllCheckbox")?.checked;
|
|
955
|
+
const scanSelect = document.getElementById("bleScanDurationSelect");
|
|
956
|
+
const timeoutInput = document.getElementById("bleTimeoutInput");
|
|
957
|
+
const disableBleCheckbox = document.getElementById("disableBleScanCheckbox");
|
|
958
|
+
if (!btn) {
|
|
959
|
+
console.error("[SwitchBot][Discovery] discoverDevices: discoverBtn not found in DOM");
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
if (!status) {
|
|
963
|
+
console.error("[SwitchBot][Discovery] discoverDevices: discoverStatus not found in DOM");
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
if (!list) {
|
|
967
|
+
console.error("[SwitchBot][Discovery] discoverDevices: discoveredList container not found in DOM");
|
|
968
|
+
toastError("Discovery UI error: device list container missing. Please reload the page.");
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
const spinnerFrames = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
|
|
972
|
+
let spinnerIndex = 0;
|
|
973
|
+
const startedAt = Date.now();
|
|
974
|
+
let phaseStartedAt = startedAt;
|
|
975
|
+
let phase = "Preparing discovery...";
|
|
976
|
+
let cancelled = false;
|
|
977
|
+
const setPhase = (nextPhase) => {
|
|
978
|
+
phase = nextPhase;
|
|
979
|
+
phaseStartedAt = Date.now();
|
|
980
|
+
};
|
|
981
|
+
const getPhasePercent = (phaseName) => {
|
|
982
|
+
if (phaseName.includes("Scanning BLE")) {
|
|
983
|
+
return 35;
|
|
984
|
+
}
|
|
985
|
+
if (phaseName.includes("Fetching OpenAPI")) {
|
|
986
|
+
return 75;
|
|
987
|
+
}
|
|
988
|
+
if (phaseName.includes("Complete")) {
|
|
989
|
+
return 100;
|
|
990
|
+
}
|
|
991
|
+
return 10;
|
|
992
|
+
};
|
|
993
|
+
const renderProgress = () => {
|
|
994
|
+
const totalSeconds = Math.max(0, Math.floor((Date.now() - startedAt) / 1e3));
|
|
995
|
+
const phaseSeconds = Math.max(0, Math.floor((Date.now() - phaseStartedAt) / 1e3));
|
|
996
|
+
const frame = spinnerFrames[spinnerIndex % spinnerFrames.length];
|
|
997
|
+
spinnerIndex += 1;
|
|
998
|
+
status.textContent = `${frame} ${phase} (${phaseSeconds}s, ${totalSeconds}s total)`;
|
|
999
|
+
if (phaseProgress) {
|
|
1000
|
+
phaseProgress.style.display = "block";
|
|
1001
|
+
}
|
|
1002
|
+
if (phaseFill) {
|
|
1003
|
+
phaseFill.style.width = `${getPhasePercent(phase)}%`;
|
|
1004
|
+
}
|
|
1005
|
+
if (phaseLabel) {
|
|
1006
|
+
phaseLabel.textContent = phase;
|
|
1007
|
+
}
|
|
1008
|
+
};
|
|
1009
|
+
const progressTimer = setInterval(renderProgress, 250);
|
|
1010
|
+
let discoveredDevices = [];
|
|
1011
|
+
const preferences = getDiscoveryPreferences();
|
|
1012
|
+
let groupBy = getDiscoveryGroupByPreference();
|
|
1013
|
+
let hideAdded = getDiscoveryHideAddedPreference();
|
|
1014
|
+
if (!window._discoverySelectedIds) {
|
|
1015
|
+
window._discoverySelectedIds = /* @__PURE__ */ new Set();
|
|
1016
|
+
}
|
|
1017
|
+
const selectedIds = window._discoverySelectedIds;
|
|
1018
|
+
let controlsInitialized = false;
|
|
1019
|
+
async function batchSetDeviceEnabled(selectedIds2, enabled) {
|
|
1020
|
+
if (typeof homebridge.getPluginConfig !== "function") {
|
|
1021
|
+
throw new TypeError("homebridge.getPluginConfig is not available");
|
|
1022
|
+
}
|
|
1023
|
+
const configArr = await homebridge.getPluginConfig();
|
|
1024
|
+
const platformIdx = Array.isArray(configArr) ? configArr.findIndex((c) => (c.platform || c.name || "").toLowerCase().includes("switchbot")) : -1;
|
|
1025
|
+
if (platformIdx === -1) {
|
|
1026
|
+
throw new Error("SwitchBot platform config not found");
|
|
1027
|
+
}
|
|
1028
|
+
const platformConfig = configArr[platformIdx];
|
|
1029
|
+
if (!Array.isArray(platformConfig.devices)) {
|
|
1030
|
+
throw new TypeError("No devices array in config");
|
|
1031
|
+
}
|
|
1032
|
+
let changed = false;
|
|
1033
|
+
for (const dev of platformConfig.devices) {
|
|
1034
|
+
const id = String(dev.deviceId || dev.id || "").trim().toLowerCase();
|
|
1035
|
+
if (selectedIds2.has(id)) {
|
|
1036
|
+
if (dev.enabled !== enabled) {
|
|
1037
|
+
dev.enabled = enabled;
|
|
1038
|
+
changed = true;
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
if (changed) {
|
|
1043
|
+
if (typeof homebridge.updatePluginConfig === "function") {
|
|
1044
|
+
await homebridge.updatePluginConfig(configArr);
|
|
1045
|
+
} else {
|
|
1046
|
+
throw new TypeError("homebridge.updatePluginConfig is not available");
|
|
1047
|
+
}
|
|
1048
|
+
if (typeof homebridge.savePluginConfig === "function") {
|
|
1049
|
+
await homebridge.savePluginConfig();
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
const ensureDiscoveryControls = async () => {
|
|
1054
|
+
const selectAllBtn = document.createElement("button");
|
|
1055
|
+
selectAllBtn.textContent = "Select All";
|
|
1056
|
+
selectAllBtn.style.fontSize = "13px";
|
|
1057
|
+
selectAllBtn.style.padding = "6px 18px";
|
|
1058
|
+
selectAllBtn.style.borderRadius = "6px";
|
|
1059
|
+
selectAllBtn.style.background = "#f3f4f6";
|
|
1060
|
+
selectAllBtn.style.color = "#1d4ed8";
|
|
1061
|
+
selectAllBtn.style.border = "1px solid #d1d5db";
|
|
1062
|
+
selectAllBtn.style.cursor = "pointer";
|
|
1063
|
+
selectAllBtn.style.marginRight = "8px";
|
|
1064
|
+
selectAllBtn.onclick = () => {
|
|
1065
|
+
for (const d of discoveredDevices) {
|
|
1066
|
+
selectedIds.add(normalizeId(d.id));
|
|
1067
|
+
}
|
|
1068
|
+
window.dispatchEvent(new Event("discovery-selection-changed"));
|
|
1069
|
+
void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1070
|
+
};
|
|
1071
|
+
const deselectAllBtn = document.createElement("button");
|
|
1072
|
+
deselectAllBtn.textContent = "Deselect All";
|
|
1073
|
+
deselectAllBtn.style.fontSize = "13px";
|
|
1074
|
+
deselectAllBtn.style.padding = "6px 18px";
|
|
1075
|
+
deselectAllBtn.style.borderRadius = "6px";
|
|
1076
|
+
deselectAllBtn.style.background = "#f3f4f6";
|
|
1077
|
+
deselectAllBtn.style.color = "#ef4444";
|
|
1078
|
+
deselectAllBtn.style.border = "1px solid #d1d5db";
|
|
1079
|
+
deselectAllBtn.style.cursor = "pointer";
|
|
1080
|
+
deselectAllBtn.onclick = () => {
|
|
1081
|
+
for (const d of discoveredDevices) {
|
|
1082
|
+
selectedIds.delete(normalizeId(d.id));
|
|
1083
|
+
}
|
|
1084
|
+
window.dispatchEvent(new Event("discovery-selection-changed"));
|
|
1085
|
+
void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1086
|
+
};
|
|
1087
|
+
const selectControlsRow = document.createElement("div");
|
|
1088
|
+
selectControlsRow.style.display = "flex";
|
|
1089
|
+
selectControlsRow.style.gap = "10px";
|
|
1090
|
+
selectControlsRow.style.margin = "0 0 10px 0";
|
|
1091
|
+
selectControlsRow.appendChild(selectAllBtn);
|
|
1092
|
+
selectControlsRow.appendChild(deselectAllBtn);
|
|
1093
|
+
if (controlsInitialized) {
|
|
1094
|
+
return;
|
|
1095
|
+
}
|
|
1096
|
+
const controlsDiv = document.createElement("div");
|
|
1097
|
+
controlsDiv.style.cssText = "margin-bottom: 12px; display: flex; gap: 12px; flex-wrap: wrap; align-items: center;";
|
|
1098
|
+
const filterLabel = document.createElement("label");
|
|
1099
|
+
filterLabel.style.fontSize = "12px";
|
|
1100
|
+
filterLabel.style.fontWeight = "500";
|
|
1101
|
+
filterLabel.textContent = "Filter:";
|
|
1102
|
+
const filterGroup = document.createElement("div");
|
|
1103
|
+
filterGroup.style.display = "flex";
|
|
1104
|
+
filterGroup.style.gap = "4px";
|
|
1105
|
+
const filterOptions = [
|
|
1106
|
+
{ label: "All", value: "all" },
|
|
1107
|
+
{ label: "BLE", value: "ble" },
|
|
1108
|
+
{ label: "API", value: "api" },
|
|
1109
|
+
{ label: "Both", value: "both" },
|
|
1110
|
+
{ label: "IR", value: "ir" }
|
|
1111
|
+
];
|
|
1112
|
+
for (const option of filterOptions) {
|
|
1113
|
+
const filterBtn = document.createElement("button");
|
|
1114
|
+
filterBtn.textContent = option.label;
|
|
1115
|
+
filterBtn.style.padding = "4px 8px";
|
|
1116
|
+
filterBtn.style.fontSize = "11px";
|
|
1117
|
+
filterBtn.style.borderRadius = "3px";
|
|
1118
|
+
filterBtn.style.cursor = "pointer";
|
|
1119
|
+
filterBtn.style.border = preferences.connectionType === option.value ? "2px solid #007AFF" : "1px solid #ccc";
|
|
1120
|
+
filterBtn.style.backgroundColor = preferences.connectionType === option.value ? "#f0f7ff" : "#fff";
|
|
1121
|
+
filterBtn.style.color = preferences.connectionType === option.value ? "#1d4ed8" : "#374151";
|
|
1122
|
+
filterBtn.onclick = () => {
|
|
1123
|
+
preferences.connectionType = option.value;
|
|
1124
|
+
setDiscoveryPreferences(preferences);
|
|
1125
|
+
void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1126
|
+
Array.prototype.forEach.call(filterGroup.querySelectorAll("button"), (b) => {
|
|
1127
|
+
b.style.border = "1px solid #ccc";
|
|
1128
|
+
b.style.backgroundColor = "#fff";
|
|
1129
|
+
b.style.color = "#374151";
|
|
1130
|
+
});
|
|
1131
|
+
filterBtn.style.border = "2px solid #007AFF";
|
|
1132
|
+
filterBtn.style.backgroundColor = "#f0f7ff";
|
|
1133
|
+
filterBtn.style.color = "#1d4ed8";
|
|
1134
|
+
};
|
|
1135
|
+
filterGroup.appendChild(filterBtn);
|
|
1136
|
+
}
|
|
1137
|
+
const sortLabel = document.createElement("label");
|
|
1138
|
+
sortLabel.style.fontSize = "12px";
|
|
1139
|
+
sortLabel.style.fontWeight = "500";
|
|
1140
|
+
sortLabel.style.marginLeft = "8px";
|
|
1141
|
+
sortLabel.textContent = "Sort:";
|
|
1142
|
+
const sortSelect = document.createElement("select");
|
|
1143
|
+
sortSelect.style.fontSize = "11px";
|
|
1144
|
+
sortSelect.style.padding = "4px 8px";
|
|
1145
|
+
sortSelect.style.borderRadius = "3px";
|
|
1146
|
+
sortSelect.value = preferences.sortBy;
|
|
1147
|
+
const sortOptions = [
|
|
1148
|
+
{ label: "Name", value: "name" },
|
|
1149
|
+
{ label: "Signal Strength", value: "signal" },
|
|
1150
|
+
{ label: "Type", value: "type" },
|
|
1151
|
+
{ label: "Connection", value: "connection" }
|
|
1152
|
+
];
|
|
1153
|
+
for (const opt of sortOptions) {
|
|
1154
|
+
const sortOption = document.createElement("option");
|
|
1155
|
+
sortOption.value = opt.value;
|
|
1156
|
+
sortOption.textContent = opt.label;
|
|
1157
|
+
sortSelect.appendChild(sortOption);
|
|
1158
|
+
}
|
|
1159
|
+
sortSelect.onchange = () => {
|
|
1160
|
+
preferences.sortBy = sortSelect.value;
|
|
1161
|
+
setDiscoveryPreferences(preferences);
|
|
1162
|
+
void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1163
|
+
};
|
|
1164
|
+
const groupSelect = document.createElement("select");
|
|
1165
|
+
groupSelect.style.fontSize = "11px";
|
|
1166
|
+
groupSelect.style.padding = "4px 8px";
|
|
1167
|
+
groupSelect.style.borderRadius = "3px";
|
|
1168
|
+
if (!localStorage.getItem(DISCOVERY_GROUP_BY_KEY)) {
|
|
1169
|
+
groupSelect.value = "type";
|
|
1170
|
+
} else {
|
|
1171
|
+
groupSelect.value = groupBy;
|
|
1172
|
+
}
|
|
1173
|
+
const groupLabel = document.createElement("label");
|
|
1174
|
+
groupLabel.style.fontSize = "12px";
|
|
1175
|
+
groupLabel.style.fontWeight = "500";
|
|
1176
|
+
groupLabel.style.marginLeft = "8px";
|
|
1177
|
+
const groupLabelTextMap = {
|
|
1178
|
+
connection: "Connection",
|
|
1179
|
+
hub: "Hub",
|
|
1180
|
+
type: "Device Type"
|
|
1181
|
+
};
|
|
1182
|
+
groupLabel.textContent = `Group: ${groupLabelTextMap[groupSelect.value] || "Connection"}`;
|
|
1183
|
+
const groupOptions = [
|
|
1184
|
+
{ label: "Connection", value: "connection" },
|
|
1185
|
+
{ label: "Hub", value: "hub" },
|
|
1186
|
+
{ label: "Device Type", value: "type" }
|
|
1187
|
+
];
|
|
1188
|
+
for (const opt of groupOptions) {
|
|
1189
|
+
const groupOption = document.createElement("option");
|
|
1190
|
+
groupOption.value = opt.value;
|
|
1191
|
+
groupOption.textContent = opt.label;
|
|
1192
|
+
groupSelect.appendChild(groupOption);
|
|
1193
|
+
}
|
|
1194
|
+
groupSelect.onchange = () => {
|
|
1195
|
+
groupBy = groupSelect.value;
|
|
1196
|
+
setDiscoveryGroupByPreference(groupBy);
|
|
1197
|
+
groupLabel.textContent = `Group: ${groupLabelTextMap[groupSelect.value] || "Connection"}`;
|
|
1198
|
+
void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1199
|
+
};
|
|
1200
|
+
const hideAddedLabel = document.createElement("label");
|
|
1201
|
+
hideAddedLabel.style.display = "inline-flex";
|
|
1202
|
+
hideAddedLabel.style.alignItems = "center";
|
|
1203
|
+
hideAddedLabel.style.gap = "4px";
|
|
1204
|
+
hideAddedLabel.style.fontSize = "11px";
|
|
1205
|
+
hideAddedLabel.style.marginLeft = "8px";
|
|
1206
|
+
const hideAddedCheckbox = document.createElement("input");
|
|
1207
|
+
hideAddedCheckbox.type = "checkbox";
|
|
1208
|
+
hideAddedCheckbox.checked = hideAdded;
|
|
1209
|
+
hideAddedCheckbox.style.margin = "0";
|
|
1210
|
+
hideAddedCheckbox.style.width = "auto";
|
|
1211
|
+
const hideAddedText = document.createElement("span");
|
|
1212
|
+
hideAddedText.textContent = "Hide Added";
|
|
1213
|
+
hideAddedLabel.appendChild(hideAddedCheckbox);
|
|
1214
|
+
hideAddedLabel.appendChild(hideAddedText);
|
|
1215
|
+
hideAddedCheckbox.onchange = () => {
|
|
1216
|
+
hideAdded = hideAddedCheckbox.checked;
|
|
1217
|
+
setDiscoveryHideAddedPreference(hideAdded);
|
|
1218
|
+
void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1219
|
+
};
|
|
1220
|
+
const searchInput = document.createElement("input");
|
|
1221
|
+
searchInput.type = "text";
|
|
1222
|
+
searchInput.placeholder = "Search by name, ID, or type...";
|
|
1223
|
+
searchInput.style.fontSize = "13px";
|
|
1224
|
+
searchInput.style.padding = "8px 16px";
|
|
1225
|
+
searchInput.style.borderRadius = "6px";
|
|
1226
|
+
searchInput.style.border = "1px solid #ccc";
|
|
1227
|
+
searchInput.style.flex = "1 1 0%";
|
|
1228
|
+
searchInput.style.minWidth = "120px";
|
|
1229
|
+
searchInput.style.maxWidth = "100%";
|
|
1230
|
+
searchInput.style.width = "100%";
|
|
1231
|
+
searchInput.value = preferences.searchQuery;
|
|
1232
|
+
let searchTimeout;
|
|
1233
|
+
searchInput.oninput = () => {
|
|
1234
|
+
clearTimeout(searchTimeout);
|
|
1235
|
+
searchTimeout = setTimeout(() => {
|
|
1236
|
+
preferences.searchQuery = searchInput.value;
|
|
1237
|
+
setDiscoveryPreferences(preferences);
|
|
1238
|
+
void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1239
|
+
}, 300);
|
|
1240
|
+
};
|
|
1241
|
+
const actionBtnStyle = {
|
|
1242
|
+
fontSize: "16px",
|
|
1243
|
+
padding: "10px 0",
|
|
1244
|
+
borderRadius: "10px",
|
|
1245
|
+
margin: "0 12px 0 0",
|
|
1246
|
+
width: "100%",
|
|
1247
|
+
maxWidth: "220px",
|
|
1248
|
+
fontWeight: "bold",
|
|
1249
|
+
background: "#ef4444",
|
|
1250
|
+
color: "#fff",
|
|
1251
|
+
border: "none",
|
|
1252
|
+
cursor: "pointer",
|
|
1253
|
+
boxShadow: "0 2px 8px #0001",
|
|
1254
|
+
transition: "background 0.2s",
|
|
1255
|
+
outline: "none",
|
|
1256
|
+
display: "block"
|
|
1257
|
+
};
|
|
1258
|
+
const addSelectedBtn = document.createElement("button");
|
|
1259
|
+
addSelectedBtn.textContent = "Add Selected to Config";
|
|
1260
|
+
Object.assign(addSelectedBtn.style, actionBtnStyle);
|
|
1261
|
+
addSelectedBtn.disabled = true;
|
|
1262
|
+
addSelectedBtn.onclick = async () => {
|
|
1263
|
+
if (!selectedIds.size) {
|
|
1264
|
+
return;
|
|
1265
|
+
}
|
|
1266
|
+
addSelectedBtn.disabled = true;
|
|
1267
|
+
addSelectedBtn.textContent = "Adding...";
|
|
1268
|
+
try {
|
|
1269
|
+
showBusyUi();
|
|
1270
|
+
const selectedDevices = discoveredDevices.filter((d) => selectedIds.has(normalizeId(d.id)));
|
|
1271
|
+
const bulkResult = await addDevicesInBulk(selectedDevices.map((d) => ({
|
|
1272
|
+
deviceId: d.id,
|
|
1273
|
+
name: d.name,
|
|
1274
|
+
type: d.type,
|
|
1275
|
+
rssi: d.rssi,
|
|
1276
|
+
address: d.address,
|
|
1277
|
+
model: d.model
|
|
1278
|
+
})));
|
|
1279
|
+
uiLog.info("Batch add response:", bulkResult);
|
|
1280
|
+
if (!bulkResult || bulkResult.success === false) {
|
|
1281
|
+
throw new Error(bulkResult?.data?.message || "Batch add failed");
|
|
1282
|
+
}
|
|
1283
|
+
const addedCount = bulkResult?.addedCount ?? bulkResult?.data?.addedCount ?? 0;
|
|
1284
|
+
const skippedCount = bulkResult?.skippedCount ?? bulkResult?.data?.skippedCount ?? 0;
|
|
1285
|
+
toastSuccess(`Added ${addedCount} device(s)${skippedCount > 0 ? ` (${skippedCount} skipped)` : ""}`);
|
|
1286
|
+
await loadConfiguredDevices();
|
|
1287
|
+
selectedIds.clear();
|
|
1288
|
+
addSelectedBtn.disabled = true;
|
|
1289
|
+
addSelectedBtn.textContent = "Add Selected";
|
|
1290
|
+
await updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1291
|
+
} catch (e) {
|
|
1292
|
+
uiLog.error("Batch add error:", e);
|
|
1293
|
+
toastError(e instanceof Error ? e.message : "Failed to add devices");
|
|
1294
|
+
addSelectedBtn.disabled = false;
|
|
1295
|
+
addSelectedBtn.textContent = "Add Selected";
|
|
1296
|
+
} finally {
|
|
1297
|
+
hideBusyUi();
|
|
1298
|
+
}
|
|
1299
|
+
};
|
|
1300
|
+
const enableSelectedBtn = document.createElement("button");
|
|
1301
|
+
enableSelectedBtn.textContent = "Enable Selected";
|
|
1302
|
+
Object.assign(enableSelectedBtn.style, actionBtnStyle);
|
|
1303
|
+
enableSelectedBtn.disabled = true;
|
|
1304
|
+
enableSelectedBtn.onclick = async () => {
|
|
1305
|
+
if (!selectedIds.size) {
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
enableSelectedBtn.disabled = true;
|
|
1309
|
+
enableSelectedBtn.textContent = "Enabling...";
|
|
1310
|
+
try {
|
|
1311
|
+
showBusyUi();
|
|
1312
|
+
await batchSetDeviceEnabled(selectedIds, true);
|
|
1313
|
+
toastSuccess("Selected devices enabled");
|
|
1314
|
+
await loadConfiguredDevices();
|
|
1315
|
+
enableSelectedBtn.disabled = true;
|
|
1316
|
+
enableSelectedBtn.textContent = "Enable Selected";
|
|
1317
|
+
await updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1318
|
+
} catch (e) {
|
|
1319
|
+
uiLog.error("Batch enable error:", e);
|
|
1320
|
+
toastError(e instanceof Error ? e.message : "Failed to enable devices");
|
|
1321
|
+
enableSelectedBtn.disabled = false;
|
|
1322
|
+
enableSelectedBtn.textContent = "Enable Selected";
|
|
1323
|
+
} finally {
|
|
1324
|
+
hideBusyUi();
|
|
1325
|
+
}
|
|
1326
|
+
};
|
|
1327
|
+
const disableSelectedBtn = document.createElement("button");
|
|
1328
|
+
disableSelectedBtn.textContent = "Disable Selected";
|
|
1329
|
+
Object.assign(disableSelectedBtn.style, actionBtnStyle);
|
|
1330
|
+
disableSelectedBtn.disabled = true;
|
|
1331
|
+
disableSelectedBtn.onclick = async () => {
|
|
1332
|
+
if (!selectedIds.size) {
|
|
1333
|
+
return;
|
|
1334
|
+
}
|
|
1335
|
+
disableSelectedBtn.disabled = true;
|
|
1336
|
+
disableSelectedBtn.textContent = "Disabling...";
|
|
1337
|
+
try {
|
|
1338
|
+
showBusyUi();
|
|
1339
|
+
await batchSetDeviceEnabled(selectedIds, false);
|
|
1340
|
+
toastSuccess("Selected devices disabled");
|
|
1341
|
+
await loadConfiguredDevices();
|
|
1342
|
+
disableSelectedBtn.disabled = true;
|
|
1343
|
+
disableSelectedBtn.textContent = "Disable Selected";
|
|
1344
|
+
await updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1345
|
+
} catch (e) {
|
|
1346
|
+
uiLog.error("Batch disable error:", e);
|
|
1347
|
+
toastError(e instanceof Error ? e.message : "Failed to disable devices");
|
|
1348
|
+
disableSelectedBtn.disabled = false;
|
|
1349
|
+
disableSelectedBtn.textContent = "Disable Selected";
|
|
1350
|
+
} finally {
|
|
1351
|
+
hideBusyUi();
|
|
1352
|
+
}
|
|
1353
|
+
};
|
|
1354
|
+
controlsDiv.appendChild(filterLabel);
|
|
1355
|
+
controlsDiv.appendChild(filterGroup);
|
|
1356
|
+
controlsDiv.appendChild(sortLabel);
|
|
1357
|
+
controlsDiv.appendChild(sortSelect);
|
|
1358
|
+
controlsDiv.appendChild(groupLabel);
|
|
1359
|
+
controlsDiv.appendChild(groupSelect);
|
|
1360
|
+
controlsDiv.appendChild(hideAddedLabel);
|
|
1361
|
+
controlsDiv.appendChild(searchInput);
|
|
1362
|
+
const topActionRow = document.createElement("div");
|
|
1363
|
+
topActionRow.style.display = "flex";
|
|
1364
|
+
topActionRow.style.gap = "20px";
|
|
1365
|
+
topActionRow.style.margin = "18px 0 10px 0";
|
|
1366
|
+
topActionRow.style.justifyContent = "flex-start";
|
|
1367
|
+
topActionRow.appendChild(addSelectedBtn);
|
|
1368
|
+
topActionRow.appendChild(enableSelectedBtn);
|
|
1369
|
+
topActionRow.appendChild(disableSelectedBtn);
|
|
1370
|
+
list.innerHTML = "";
|
|
1371
|
+
list.appendChild(selectControlsRow);
|
|
1372
|
+
list.appendChild(topActionRow);
|
|
1373
|
+
list.appendChild(controlsDiv);
|
|
1374
|
+
let deviceListContainer = document.getElementById("discoveredDevices");
|
|
1375
|
+
if (!deviceListContainer) {
|
|
1376
|
+
deviceListContainer = document.createElement("ul");
|
|
1377
|
+
deviceListContainer.id = "discoveredDevices";
|
|
1378
|
+
deviceListContainer.style.maxHeight = "400px";
|
|
1379
|
+
deviceListContainer.style.overflowY = "auto";
|
|
1380
|
+
deviceListContainer.style.marginTop = "12px";
|
|
1381
|
+
deviceListContainer.style.padding = "0";
|
|
1382
|
+
deviceListContainer.style.listStyle = "none";
|
|
1383
|
+
list.appendChild(deviceListContainer);
|
|
1384
|
+
}
|
|
1385
|
+
list.style.display = "block";
|
|
1386
|
+
controlsInitialized = true;
|
|
1387
|
+
const updateActionButtons = () => {
|
|
1388
|
+
const hasSelection = selectedIds.size > 0;
|
|
1389
|
+
addSelectedBtn.disabled = !hasSelection;
|
|
1390
|
+
enableSelectedBtn.disabled = !hasSelection;
|
|
1391
|
+
disableSelectedBtn.disabled = !hasSelection;
|
|
1392
|
+
};
|
|
1393
|
+
setInterval(updateActionButtons, 300);
|
|
1394
|
+
};
|
|
1395
|
+
try {
|
|
1396
|
+
const bleSettings = {
|
|
1397
|
+
bleEnabled: !(disableBleCheckbox?.checked ?? false),
|
|
1398
|
+
bleScanDurationSeconds: Math.max(3, Math.min(15, Number(scanSelect?.value || 5))),
|
|
1399
|
+
bleTimeoutSeconds: Math.max(3, Math.min(30, Number(timeoutInput?.value || 8)))
|
|
1400
|
+
};
|
|
1401
|
+
setDiscoveryBleSettings(bleSettings);
|
|
1402
|
+
showBusyUi();
|
|
1403
|
+
btn.disabled = true;
|
|
1404
|
+
btn.textContent = "\u{1F50D} Discovering...";
|
|
1405
|
+
setPhase(bleSettings.bleEnabled ? "Scanning BLE..." : "Skipping BLE scan...");
|
|
1406
|
+
renderProgress();
|
|
1407
|
+
status.classList.remove("error");
|
|
1408
|
+
const devicesFoundDisplay = document.getElementById("discoverDevicesFound");
|
|
1409
|
+
if (devicesFoundDisplay) {
|
|
1410
|
+
devicesFoundDisplay.style.display = "none";
|
|
1411
|
+
devicesFoundDisplay.classList.remove("discovery-scanning-pulse");
|
|
1412
|
+
}
|
|
1413
|
+
if (cancelBtn) {
|
|
1414
|
+
cancelBtn.style.display = "inline-block";
|
|
1415
|
+
cancelBtn.disabled = false;
|
|
1416
|
+
cancelBtn.onclick = () => {
|
|
1417
|
+
cancelled = true;
|
|
1418
|
+
const totalSeconds = Math.max(0, Math.floor((Date.now() - startedAt) / 1e3));
|
|
1419
|
+
status.textContent = `Discovery cancelled (${totalSeconds}s total)`;
|
|
1420
|
+
if (phaseProgress) {
|
|
1421
|
+
phaseProgress.style.display = "none";
|
|
1422
|
+
}
|
|
1423
|
+
if (devicesFoundDisplay) {
|
|
1424
|
+
devicesFoundDisplay.style.display = "none";
|
|
1425
|
+
devicesFoundDisplay.classList.remove("discovery-scanning-pulse");
|
|
1426
|
+
}
|
|
1427
|
+
cancelBtn.style.display = "none";
|
|
1428
|
+
btn.disabled = false;
|
|
1429
|
+
btn.textContent = "\u{1F50D} Discover Devices";
|
|
1430
|
+
hideBusyUi();
|
|
1431
|
+
};
|
|
1432
|
+
}
|
|
1433
|
+
if (bleSettings.bleEnabled) {
|
|
1434
|
+
const bleDevicesRaw = await discoverDevices("ble", bleSettings);
|
|
1435
|
+
if (cancelled) {
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
discoveredDevices = dedupeById(bleDevicesRaw);
|
|
1439
|
+
uiLog.info("BLE discover response:", bleDevicesRaw);
|
|
1440
|
+
if (devicesFoundDisplay && bleDevicesRaw.length > 0) {
|
|
1441
|
+
devicesFoundDisplay.style.display = "inline";
|
|
1442
|
+
devicesFoundDisplay.classList.add("discovery-scanning-pulse");
|
|
1443
|
+
devicesFoundDisplay.textContent = `\u{1F4CA} ${bleDevicesRaw.length} device(s) found (scanning...)`;
|
|
1444
|
+
}
|
|
1445
|
+
} else {
|
|
1446
|
+
discoveredDevices = [];
|
|
1447
|
+
uiLog.info("BLE discovery skipped by user setting");
|
|
1448
|
+
}
|
|
1449
|
+
if (!autoAddAll && discoveredDevices.length > 0) {
|
|
1450
|
+
await ensureDiscoveryControls();
|
|
1451
|
+
await updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1452
|
+
status.textContent = `Showing ${discoveredDevices.length} device(s) from BLE, fetching OpenAPI...`;
|
|
1453
|
+
}
|
|
1454
|
+
setPhase("Fetching OpenAPI...");
|
|
1455
|
+
renderProgress();
|
|
1456
|
+
try {
|
|
1457
|
+
const openApiDevicesRaw = await discoverDevices("openapi");
|
|
1458
|
+
if (cancelled) {
|
|
1459
|
+
return;
|
|
1460
|
+
}
|
|
1461
|
+
uiLog.info("OpenAPI discover response:", openApiDevicesRaw);
|
|
1462
|
+
discoveredDevices = mergeDiscoveredDevices(discoveredDevices, openApiDevicesRaw);
|
|
1463
|
+
if (devicesFoundDisplay && discoveredDevices.length > 0) {
|
|
1464
|
+
devicesFoundDisplay.textContent = `\u{1F4CA} ${discoveredDevices.length} device(s) found (complete)`;
|
|
1465
|
+
}
|
|
1466
|
+
} catch (openApiError) {
|
|
1467
|
+
uiLog.warn("OpenAPI phase failed during discovery:", openApiError);
|
|
1468
|
+
if (!discoveredDevices.length) {
|
|
1469
|
+
throw openApiError;
|
|
1470
|
+
}
|
|
1471
|
+
if (devicesFoundDisplay) {
|
|
1472
|
+
devicesFoundDisplay.classList.remove("discovery-scanning-pulse");
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
setPhase("Complete");
|
|
1476
|
+
renderProgress();
|
|
1477
|
+
uiLog.info("Final merged discover response:", discoveredDevices);
|
|
1478
|
+
if (!discoveredDevices.length) {
|
|
1479
|
+
status.textContent = "No devices found in your SwitchBot account";
|
|
1480
|
+
toastInfo("No devices found in your SwitchBot account");
|
|
1481
|
+
list.style.display = "none";
|
|
1482
|
+
if (devicesFoundDisplay) {
|
|
1483
|
+
devicesFoundDisplay.style.display = "none";
|
|
1484
|
+
devicesFoundDisplay.classList.remove("discovery-scanning-pulse");
|
|
1485
|
+
}
|
|
1486
|
+
clearDiscoveryCache();
|
|
1487
|
+
updateLastScannedStatus();
|
|
1488
|
+
return;
|
|
1489
|
+
}
|
|
1490
|
+
if (autoAddAll) {
|
|
1491
|
+
status.textContent = `Auto-adding ${discoveredDevices.length} device(s)...`;
|
|
1492
|
+
try {
|
|
1493
|
+
const bulkResult = await addDevicesInBulk(
|
|
1494
|
+
discoveredDevices.map((d) => ({
|
|
1495
|
+
deviceId: d.id,
|
|
1496
|
+
name: d.name,
|
|
1497
|
+
type: d.type,
|
|
1498
|
+
rssi: d.rssi,
|
|
1499
|
+
address: d.address,
|
|
1500
|
+
model: d.model
|
|
1501
|
+
}))
|
|
1502
|
+
);
|
|
1503
|
+
uiLog.info("Bulk add response:", bulkResult);
|
|
1504
|
+
if (!bulkResult || bulkResult.success === false) {
|
|
1505
|
+
throw new Error(bulkResult?.data?.message || "Bulk add failed");
|
|
1506
|
+
}
|
|
1507
|
+
const addedCount = bulkResult?.addedCount ?? bulkResult?.data?.addedCount ?? 0;
|
|
1508
|
+
const skippedCount = bulkResult?.skippedCount ?? bulkResult?.data?.skippedCount ?? 0;
|
|
1509
|
+
status.textContent = `\u2713 Added ${addedCount} device(s)${skippedCount > 0 ? ` (${skippedCount} skipped)` : ""}`;
|
|
1510
|
+
if (addedCount > 0) {
|
|
1511
|
+
toastSuccess(`Added ${addedCount} device(s)${skippedCount > 0 ? ` (${skippedCount} skipped)` : ""}`);
|
|
1512
|
+
} else if (skippedCount > 0) {
|
|
1513
|
+
toastWarning(`No new devices were added (${skippedCount} skipped)`);
|
|
1514
|
+
}
|
|
1515
|
+
status.classList.remove("error");
|
|
1516
|
+
list.style.display = "none";
|
|
1517
|
+
if (discoveredDevices.length > 0) {
|
|
1518
|
+
const synced = await syncParentPluginConfigFromDisk(true);
|
|
1519
|
+
status.textContent += synced ? " - Config saved automatically." : " - Warning: config may not persist until you close/reopen settings.";
|
|
1520
|
+
if (synced) {
|
|
1521
|
+
toastSuccess("Configuration synced and saved automatically");
|
|
1522
|
+
} else {
|
|
1523
|
+
toastWarning("Configuration sync failed; close and reopen settings before Save");
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
} catch (e) {
|
|
1527
|
+
uiLog.error("Bulk add error:", e);
|
|
1528
|
+
status.textContent = `\u2717 Error: ${e instanceof Error ? e.message : "Failed to add devices"}`;
|
|
1529
|
+
status.classList.add("error");
|
|
1530
|
+
toastError(e instanceof Error ? e.message : "Failed to add devices");
|
|
1531
|
+
}
|
|
1532
|
+
await loadConfiguredDevices();
|
|
1533
|
+
return;
|
|
1534
|
+
}
|
|
1535
|
+
await ensureDiscoveryControls();
|
|
1536
|
+
await updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds);
|
|
1537
|
+
setDiscoveryCache(discoveredDevices);
|
|
1538
|
+
updateLastScannedStatus();
|
|
1539
|
+
} catch (e) {
|
|
1540
|
+
if (cancelled) {
|
|
1541
|
+
return;
|
|
1542
|
+
}
|
|
1543
|
+
uiLog.error("Discovery error:", e);
|
|
1544
|
+
status.textContent = `Error: ${e instanceof Error ? e.message : "Discovery failed"}`;
|
|
1545
|
+
status.classList.add("error");
|
|
1546
|
+
toastError(e instanceof Error ? e.message : "Discovery failed");
|
|
1547
|
+
list.style.display = "none";
|
|
1548
|
+
} finally {
|
|
1549
|
+
clearInterval(progressTimer);
|
|
1550
|
+
hideBusyUi();
|
|
1551
|
+
if (phaseProgress) {
|
|
1552
|
+
phaseProgress.style.display = "none";
|
|
1553
|
+
}
|
|
1554
|
+
if (phaseFill) {
|
|
1555
|
+
phaseFill.style.width = "0%";
|
|
1556
|
+
}
|
|
1557
|
+
if (phaseLabel) {
|
|
1558
|
+
phaseLabel.textContent = "";
|
|
1559
|
+
}
|
|
1560
|
+
const devicesFoundDisplay = document.getElementById("discoverDevicesFound");
|
|
1561
|
+
if (devicesFoundDisplay) {
|
|
1562
|
+
devicesFoundDisplay.style.display = "none";
|
|
1563
|
+
devicesFoundDisplay.classList.remove("discovery-scanning-pulse");
|
|
1564
|
+
}
|
|
1565
|
+
btn.disabled = false;
|
|
1566
|
+
btn.textContent = "\u{1F50D} Discover Devices";
|
|
1567
|
+
if (cancelBtn) {
|
|
1568
|
+
cancelBtn.disabled = false;
|
|
1569
|
+
cancelBtn.style.display = "none";
|
|
1570
|
+
cancelBtn.onclick = null;
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
async function updateDiscoveryView(allDevices, preferences, groupBy, hideAdded, selectedIds) {
|
|
1575
|
+
console.warn("[SwitchBot][Discovery] updateDiscoveryView: allDevices", allDevices);
|
|
1576
|
+
const visibleDevices = allDevices.filter((d) => {
|
|
1577
|
+
if (hideAdded && d.added) {
|
|
1578
|
+
return false;
|
|
1579
|
+
}
|
|
1580
|
+
return true;
|
|
1581
|
+
});
|
|
1582
|
+
console.warn("[SwitchBot][Discovery] visibleDevices after filter:", visibleDevices);
|
|
1583
|
+
const configuredIds = new Set(
|
|
1584
|
+
allDevices.filter((d) => d.added).map((d) => normalizeId(d.id))
|
|
1585
|
+
);
|
|
1586
|
+
const getConnectionGroup = (device) => {
|
|
1587
|
+
if (device?.isIR) {
|
|
1588
|
+
return "IR";
|
|
1589
|
+
}
|
|
1590
|
+
const connectionType = String(device?.connectionType || "").toLowerCase();
|
|
1591
|
+
if (connectionType.includes("both")) {
|
|
1592
|
+
return "Both";
|
|
1593
|
+
}
|
|
1594
|
+
if (connectionType.includes("ble")) {
|
|
1595
|
+
return "BLE";
|
|
1596
|
+
}
|
|
1597
|
+
if (connectionType.includes("api")) {
|
|
1598
|
+
return "OpenAPI";
|
|
1599
|
+
}
|
|
1600
|
+
return "Unknown";
|
|
1601
|
+
};
|
|
1602
|
+
const getHubGroup = (device) => {
|
|
1603
|
+
const hub = String(device?.hubDeviceId || "").trim();
|
|
1604
|
+
return hub ? `Hub ${hub}` : "No Hub";
|
|
1605
|
+
};
|
|
1606
|
+
const getTypeGroup = (device) => {
|
|
1607
|
+
const type = String(device?.type || "").trim();
|
|
1608
|
+
return type || "Unknown Type";
|
|
1609
|
+
};
|
|
1610
|
+
const groupedDevices = /* @__PURE__ */ new Map();
|
|
1611
|
+
for (const d of visibleDevices) {
|
|
1612
|
+
let group = getConnectionGroup(d);
|
|
1613
|
+
if (groupBy === "hub") {
|
|
1614
|
+
group = getHubGroup(d);
|
|
1615
|
+
} else if (groupBy === "type") {
|
|
1616
|
+
group = getTypeGroup(d);
|
|
1617
|
+
}
|
|
1618
|
+
const groupDevices = groupedDevices.get(group) || [];
|
|
1619
|
+
groupDevices.push(d);
|
|
1620
|
+
groupedDevices.set(group, groupDevices);
|
|
1621
|
+
}
|
|
1622
|
+
console.warn("[SwitchBot][Discovery] groupedDevices:", groupedDevices);
|
|
1623
|
+
let orderedGroups = [];
|
|
1624
|
+
if (groupBy === "hub") {
|
|
1625
|
+
const hubGroups = [...groupedDevices.keys()].filter((group) => group !== "No Hub").sort((a, b) => a.localeCompare(b));
|
|
1626
|
+
orderedGroups = groupedDevices.has("No Hub") ? [...hubGroups, "No Hub"] : hubGroups;
|
|
1627
|
+
} else if (groupBy === "type") {
|
|
1628
|
+
const typeGroups = [...groupedDevices.keys()].filter((group) => group !== "Unknown Type").sort((a, b) => a.localeCompare(b));
|
|
1629
|
+
orderedGroups = groupedDevices.has("Unknown Type") ? [...typeGroups, "Unknown Type"] : typeGroups;
|
|
1630
|
+
} else {
|
|
1631
|
+
const groupOrder = ["Both", "BLE", "OpenAPI", "IR", "Unknown"];
|
|
1632
|
+
orderedGroups = groupOrder.filter((group) => groupedDevices.has(group));
|
|
1633
|
+
}
|
|
1634
|
+
const container = document.createElement("div");
|
|
1635
|
+
container.id = "discoveredDevices";
|
|
1636
|
+
container.className = "discovery-groups";
|
|
1637
|
+
console.warn("[SwitchBot][Discovery] Rendering device groups:", orderedGroups);
|
|
1638
|
+
if (!visibleDevices.length) {
|
|
1639
|
+
const empty = document.createElement("div");
|
|
1640
|
+
empty.className = "discovery-group-empty";
|
|
1641
|
+
empty.textContent = hideAdded ? "No devices match current filters (or all are already added)." : "No devices match current filters.";
|
|
1642
|
+
container.appendChild(empty);
|
|
1643
|
+
console.warn("[SwitchBot][Discovery] No visible devices after filtering.");
|
|
1644
|
+
} else {
|
|
1645
|
+
for (const groupName of orderedGroups) {
|
|
1646
|
+
const groupItems = groupedDevices.get(groupName);
|
|
1647
|
+
if (!groupItems?.length) {
|
|
1648
|
+
continue;
|
|
1649
|
+
}
|
|
1650
|
+
console.warn(`[SwitchBot][Discovery] Rendering group: ${groupName}`, groupItems);
|
|
1651
|
+
const groupSection = document.createElement("section");
|
|
1652
|
+
groupSection.className = "discovery-group";
|
|
1653
|
+
const groupStorageKey = `${groupBy}:${groupName}`;
|
|
1654
|
+
let expanded = isDiscoveryGroupExpanded(groupStorageKey);
|
|
1655
|
+
const groupHeader = document.createElement("button");
|
|
1656
|
+
groupHeader.className = "discovery-group-header-btn";
|
|
1657
|
+
groupHeader.type = "button";
|
|
1658
|
+
const setGroupHeaderText = () => {
|
|
1659
|
+
const marker = expanded ? "\u25BE" : "\u25B8";
|
|
1660
|
+
groupHeader.textContent = `${marker} ${groupName} (${groupItems.length})`;
|
|
1661
|
+
};
|
|
1662
|
+
setGroupHeaderText();
|
|
1663
|
+
groupSection.appendChild(groupHeader);
|
|
1664
|
+
const groupList = await renderDiscoveredDevices(groupItems, {
|
|
1665
|
+
configuredIds,
|
|
1666
|
+
selectedIds,
|
|
1667
|
+
onToggleSelect: (device, selected) => {
|
|
1668
|
+
const id = normalizeId(device.id);
|
|
1669
|
+
if (selected) {
|
|
1670
|
+
selectedIds.add(id);
|
|
1671
|
+
} else {
|
|
1672
|
+
selectedIds.delete(id);
|
|
1673
|
+
}
|
|
1674
|
+
const btn = document.querySelector("button")?.parentElement?.querySelector("button");
|
|
1675
|
+
if (btn && btn.textContent?.includes("Add Selected")) {
|
|
1676
|
+
btn.disabled = selectedIds.size === 0;
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
});
|
|
1680
|
+
if (!expanded) {
|
|
1681
|
+
groupList.style.display = "none";
|
|
1682
|
+
}
|
|
1683
|
+
groupHeader.onclick = () => {
|
|
1684
|
+
expanded = !expanded;
|
|
1685
|
+
setDiscoveryGroupExpanded(groupStorageKey, expanded);
|
|
1686
|
+
setGroupHeaderText();
|
|
1687
|
+
groupList.style.display = expanded ? "grid" : "none";
|
|
1688
|
+
};
|
|
1689
|
+
groupSection.appendChild(groupList);
|
|
1690
|
+
container.appendChild(groupSection);
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
const existingList = document.getElementById("discoveredDevices");
|
|
1694
|
+
container.id = "discoveredDevices";
|
|
1695
|
+
if (existingList && existingList.parentNode) {
|
|
1696
|
+
existingList.replaceWith(container);
|
|
1697
|
+
} else {
|
|
1698
|
+
const listContainer = document.getElementById("discoveredList");
|
|
1699
|
+
if (listContainer) {
|
|
1700
|
+
listContainer.appendChild(container);
|
|
1701
|
+
} else {
|
|
1702
|
+
console.error("[SwitchBot][Discovery] render: discoveredList container not found in DOM (fallback)");
|
|
1703
|
+
toastError("Discovery UI error: device list container missing. Please reload the page.");
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
function updateBatchButtonStates() {
|
|
1707
|
+
const addSelectedBtn = document.getElementById("addSelectedBtn");
|
|
1708
|
+
const enableSelectedBtn = document.getElementById("enableSelectedBtn");
|
|
1709
|
+
const disableSelectedBtn = document.getElementById("disableSelectedBtn");
|
|
1710
|
+
const hasSelection = selectedIds.size > 0;
|
|
1711
|
+
if (addSelectedBtn) {
|
|
1712
|
+
addSelectedBtn.disabled = !hasSelection;
|
|
1713
|
+
}
|
|
1714
|
+
if (enableSelectedBtn) {
|
|
1715
|
+
enableSelectedBtn.disabled = !hasSelection;
|
|
1716
|
+
}
|
|
1717
|
+
if (disableSelectedBtn) {
|
|
1718
|
+
disableSelectedBtn.disabled = !hasSelection;
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
window.removeEventListener("discovery-selection-changed", updateBatchButtonStates);
|
|
1722
|
+
window.addEventListener("discovery-selection-changed", updateBatchButtonStates);
|
|
1723
|
+
updateBatchButtonStates();
|
|
1724
|
+
const status = document.getElementById("discoverStatus");
|
|
1725
|
+
if (status) {
|
|
1726
|
+
const totalCount = allDevices.length;
|
|
1727
|
+
const filteredCount = visibleDevices.length;
|
|
1728
|
+
status.textContent = filteredCount === totalCount ? `Found ${totalCount} device(s)` : `Showing ${filteredCount} of ${totalCount} device(s)`;
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
async function addDeviceToConfig(device) {
|
|
1732
|
+
const { addDeviceToConfig: addDevice2 } = await Promise.resolve().then(() => (init_devices(), devices_exports));
|
|
1733
|
+
await addDevice2(device);
|
|
1734
|
+
}
|
|
1735
|
+
var DISCOVERY_GROUP_BY_KEY, DISCOVERY_GROUP_EXPANDED_KEY, DISCOVERY_BLE_SETTINGS_KEY, DISCOVERY_HIDE_ADDED_KEY, DISCOVERY_CACHE_KEY, DISCOVERY_AUTO_REFRESH_KEY, DISCOVERY_CACHE_TTL_MS, discoveryAutoRefreshTimer, discoveryLastScannedTimer;
|
|
1736
|
+
var init_discovery = __esm({
|
|
1737
|
+
"src/homebridge-ui/public/js/discovery.ts"() {
|
|
1738
|
+
"use strict";
|
|
1739
|
+
init_api();
|
|
1740
|
+
init_devices();
|
|
1741
|
+
init_logger();
|
|
1742
|
+
init_modal();
|
|
1743
|
+
init_render();
|
|
1744
|
+
init_toast();
|
|
1745
|
+
DISCOVERY_GROUP_BY_KEY = "discoveryGroupBy";
|
|
1746
|
+
DISCOVERY_GROUP_EXPANDED_KEY = "discoveryGroupExpanded";
|
|
1747
|
+
DISCOVERY_BLE_SETTINGS_KEY = "discoveryBleSettings";
|
|
1748
|
+
DISCOVERY_HIDE_ADDED_KEY = "discoveryHideAdded";
|
|
1749
|
+
DISCOVERY_CACHE_KEY = "discoveryCache";
|
|
1750
|
+
DISCOVERY_AUTO_REFRESH_KEY = "discoveryAutoRefreshSeconds";
|
|
1751
|
+
DISCOVERY_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
1752
|
+
discoveryAutoRefreshTimer = null;
|
|
1753
|
+
discoveryLastScannedTimer = null;
|
|
1754
|
+
}
|
|
1755
|
+
});
|
|
1756
|
+
|
|
1757
|
+
// src/homebridge-ui/public/js/constants.ts
|
|
1758
|
+
var init_constants = __esm({
|
|
1759
|
+
"src/homebridge-ui/public/js/constants.ts"() {
|
|
1760
|
+
"use strict";
|
|
1761
|
+
init_device_types();
|
|
1762
|
+
}
|
|
1763
|
+
});
|
|
1764
|
+
|
|
1765
|
+
// src/homebridge-ui/public/js/modals.ts
|
|
1766
|
+
var modals_exports = {};
|
|
1767
|
+
__export(modals_exports, {
|
|
1768
|
+
editDevice: () => editDevice,
|
|
1769
|
+
importDiscoveredDevice: () => importDiscoveredDevice
|
|
1770
|
+
});
|
|
1771
|
+
async function importDiscoveredDevice(device) {
|
|
1772
|
+
const openApiRefreshLabel = document.createElement("label");
|
|
1773
|
+
openApiRefreshLabel.textContent = "OpenAPI Polling Interval (seconds)";
|
|
1774
|
+
openApiRefreshLabel.style.display = "block";
|
|
1775
|
+
openApiRefreshLabel.style.marginBottom = "6px";
|
|
1776
|
+
openApiRefreshLabel.style.fontWeight = "500";
|
|
1777
|
+
openApiRefreshLabel.style.fontSize = "12px";
|
|
1778
|
+
openApiRefreshLabel.style.color = "#6b7280";
|
|
1779
|
+
openApiRefreshLabel.title = "How often to poll this device via OpenAPI for status (in seconds). Overrides platform value if set. Default: 300 (5 minutes). Minimum: 30.";
|
|
1780
|
+
const openApiRefreshInput = document.createElement("input");
|
|
1781
|
+
openApiRefreshInput.type = "number";
|
|
1782
|
+
openApiRefreshInput.value = device.refreshRate || 300;
|
|
1783
|
+
openApiRefreshInput.min = "30";
|
|
1784
|
+
openApiRefreshInput.step = "1";
|
|
1785
|
+
openApiRefreshInput.style.width = "100%";
|
|
1786
|
+
openApiRefreshInput.style.marginBottom = "12px";
|
|
1787
|
+
openApiRefreshInput.style.padding = "8px 10px";
|
|
1788
|
+
openApiRefreshInput.style.borderRadius = "6px";
|
|
1789
|
+
openApiRefreshInput.style.fontSize = "14px";
|
|
1790
|
+
openApiRefreshInput.style.boxSizing = "border-box";
|
|
1791
|
+
return new Promise((resolve) => {
|
|
1792
|
+
const div = document.createElement("div");
|
|
1793
|
+
div.style.position = "fixed";
|
|
1794
|
+
div.style.top = "0";
|
|
1795
|
+
div.style.left = "0";
|
|
1796
|
+
div.style.width = "100%";
|
|
1797
|
+
div.style.height = "100%";
|
|
1798
|
+
div.style.background = "rgba(0,0,0,0.7)";
|
|
1799
|
+
div.style.display = "flex";
|
|
1800
|
+
div.style.alignItems = "center";
|
|
1801
|
+
div.style.justifyContent = "center";
|
|
1802
|
+
div.style.zIndex = "9999";
|
|
1803
|
+
const modal = document.createElement("div");
|
|
1804
|
+
modal.style.background = getComputedStyle(document.body).backgroundColor;
|
|
1805
|
+
modal.style.color = getComputedStyle(document.body).color;
|
|
1806
|
+
modal.style.padding = "0";
|
|
1807
|
+
modal.style.borderRadius = "10px";
|
|
1808
|
+
modal.style.minWidth = "440px";
|
|
1809
|
+
modal.style.maxWidth = "90vw";
|
|
1810
|
+
modal.style.boxShadow = "0 8px 32px rgba(0,0,0,0.35)";
|
|
1811
|
+
modal.style.overflow = "hidden";
|
|
1812
|
+
modal.style.borderTop = "3px solid var(--switchbot-red, #ef4444)";
|
|
1813
|
+
const title = document.createElement("h3");
|
|
1814
|
+
title.textContent = "Import Discovered Device";
|
|
1815
|
+
title.style.marginTop = "0";
|
|
1816
|
+
title.style.marginBottom = "16px";
|
|
1817
|
+
title.style.padding = "20px 20px 0";
|
|
1818
|
+
title.style.fontSize = "18px";
|
|
1819
|
+
title.style.fontWeight = "600";
|
|
1820
|
+
title.style.color = "var(--switchbot-red, #ef4444)";
|
|
1821
|
+
title.style.letterSpacing = "-0.02em";
|
|
1822
|
+
const contentDiv = document.createElement("div");
|
|
1823
|
+
contentDiv.style.padding = "0 20px 20px";
|
|
1824
|
+
const nameLabel = document.createElement("label");
|
|
1825
|
+
nameLabel.textContent = "Device Name";
|
|
1826
|
+
nameLabel.style.display = "block";
|
|
1827
|
+
nameLabel.style.marginBottom = "6px";
|
|
1828
|
+
nameLabel.style.fontWeight = "500";
|
|
1829
|
+
nameLabel.style.fontSize = "12px";
|
|
1830
|
+
nameLabel.style.color = "#6b7280";
|
|
1831
|
+
const nameInput = document.createElement("input");
|
|
1832
|
+
nameInput.type = "text";
|
|
1833
|
+
let safeName = device.name;
|
|
1834
|
+
if (!safeName || safeName === "undefined") {
|
|
1835
|
+
safeName = device.id || "";
|
|
1836
|
+
}
|
|
1837
|
+
nameInput.value = safeName;
|
|
1838
|
+
nameInput.style.width = "100%";
|
|
1839
|
+
nameInput.style.marginBottom = "12px";
|
|
1840
|
+
nameInput.style.padding = "8px 10px";
|
|
1841
|
+
nameInput.style.borderRadius = "6px";
|
|
1842
|
+
nameInput.style.fontSize = "14px";
|
|
1843
|
+
nameInput.style.boxSizing = "border-box";
|
|
1844
|
+
const typeLabel = document.createElement("label");
|
|
1845
|
+
typeLabel.textContent = "Config Device Type";
|
|
1846
|
+
typeLabel.style.display = "block";
|
|
1847
|
+
typeLabel.style.marginBottom = "6px";
|
|
1848
|
+
typeLabel.style.fontWeight = "500";
|
|
1849
|
+
typeLabel.style.fontSize = "12px";
|
|
1850
|
+
typeLabel.style.color = "#6b7280";
|
|
1851
|
+
const typeSelect = document.createElement("select");
|
|
1852
|
+
typeSelect.style.width = "100%";
|
|
1853
|
+
typeSelect.style.padding = "8px 10px";
|
|
1854
|
+
typeSelect.style.marginBottom = "12px";
|
|
1855
|
+
typeSelect.style.borderRadius = "6px";
|
|
1856
|
+
typeSelect.style.fontSize = "14px";
|
|
1857
|
+
typeSelect.style.background = getComputedStyle(nameInput).background;
|
|
1858
|
+
typeSelect.style.color = getComputedStyle(nameInput).color;
|
|
1859
|
+
typeSelect.style.border = getComputedStyle(nameInput).border;
|
|
1860
|
+
typeSelect.style.boxSizing = "border-box";
|
|
1861
|
+
Object.keys(DEVICE_TYPES).forEach((categoryName) => {
|
|
1862
|
+
const optgroup = document.createElement("optgroup");
|
|
1863
|
+
optgroup.label = categoryName;
|
|
1864
|
+
DEVICE_TYPES[categoryName].forEach((deviceType) => {
|
|
1865
|
+
const opt = document.createElement("option");
|
|
1866
|
+
opt.value = deviceType;
|
|
1867
|
+
opt.text = deviceType;
|
|
1868
|
+
const detectedType = (device.type || "").toLowerCase();
|
|
1869
|
+
opt.selected = deviceType.toLowerCase() === detectedType;
|
|
1870
|
+
optgroup.appendChild(opt);
|
|
1871
|
+
});
|
|
1872
|
+
typeSelect.appendChild(optgroup);
|
|
1873
|
+
});
|
|
1874
|
+
const connectionPrefLabel = document.createElement("label");
|
|
1875
|
+
connectionPrefLabel.textContent = "Connection Preference";
|
|
1876
|
+
connectionPrefLabel.style.display = "block";
|
|
1877
|
+
connectionPrefLabel.style.marginBottom = "6px";
|
|
1878
|
+
connectionPrefLabel.style.fontWeight = "500";
|
|
1879
|
+
connectionPrefLabel.style.fontSize = "12px";
|
|
1880
|
+
connectionPrefLabel.style.color = "#6b7280";
|
|
1881
|
+
const connectionPrefSelect = document.createElement("select");
|
|
1882
|
+
connectionPrefSelect.style.width = "100%";
|
|
1883
|
+
connectionPrefSelect.style.marginBottom = "12px";
|
|
1884
|
+
connectionPrefSelect.style.padding = "8px 10px";
|
|
1885
|
+
connectionPrefSelect.style.borderRadius = "6px";
|
|
1886
|
+
connectionPrefSelect.style.fontSize = "14px";
|
|
1887
|
+
connectionPrefSelect.style.boxSizing = "border-box";
|
|
1888
|
+
["auto", "ble", "openapi"].forEach((val) => {
|
|
1889
|
+
const opt = document.createElement("option");
|
|
1890
|
+
opt.value = val;
|
|
1891
|
+
opt.text = val.charAt(0).toUpperCase() + val.slice(1);
|
|
1892
|
+
opt.selected = (device.connectionPreference || "auto") === val;
|
|
1893
|
+
connectionPrefSelect.appendChild(opt);
|
|
1894
|
+
});
|
|
1895
|
+
const roomLabel = document.createElement("label");
|
|
1896
|
+
roomLabel.textContent = "Room";
|
|
1897
|
+
roomLabel.style.display = "block";
|
|
1898
|
+
roomLabel.style.marginBottom = "6px";
|
|
1899
|
+
roomLabel.style.fontWeight = "500";
|
|
1900
|
+
roomLabel.style.fontSize = "12px";
|
|
1901
|
+
roomLabel.style.color = "#6b7280";
|
|
1902
|
+
const roomInput = document.createElement("input");
|
|
1903
|
+
roomInput.type = "text";
|
|
1904
|
+
roomInput.value = device.room || "";
|
|
1905
|
+
roomInput.placeholder = "Optional room/location metadata";
|
|
1906
|
+
roomInput.style.width = "100%";
|
|
1907
|
+
roomInput.style.marginBottom = "12px";
|
|
1908
|
+
roomInput.style.padding = "8px 10px";
|
|
1909
|
+
roomInput.style.borderRadius = "6px";
|
|
1910
|
+
roomInput.style.fontSize = "14px";
|
|
1911
|
+
roomInput.style.boxSizing = "border-box";
|
|
1912
|
+
const macLabel = document.createElement("label");
|
|
1913
|
+
macLabel.textContent = "BLE MAC Address (optional)";
|
|
1914
|
+
macLabel.style.display = "block";
|
|
1915
|
+
macLabel.style.marginBottom = "6px";
|
|
1916
|
+
macLabel.style.fontWeight = "500";
|
|
1917
|
+
macLabel.style.fontSize = "12px";
|
|
1918
|
+
macLabel.style.color = "#6b7280";
|
|
1919
|
+
const macInput = document.createElement("input");
|
|
1920
|
+
macInput.type = "text";
|
|
1921
|
+
macInput.value = device.address || "";
|
|
1922
|
+
macInput.placeholder = "AA:BB:CC:DD:EE:FF";
|
|
1923
|
+
macInput.style.width = "100%";
|
|
1924
|
+
macInput.style.marginBottom = "12px";
|
|
1925
|
+
macInput.style.padding = "8px 10px";
|
|
1926
|
+
macInput.style.borderRadius = "6px";
|
|
1927
|
+
macInput.style.fontSize = "14px";
|
|
1928
|
+
macInput.style.boxSizing = "border-box";
|
|
1929
|
+
const encryptionKeyLabel = document.createElement("label");
|
|
1930
|
+
encryptionKeyLabel.textContent = "BLE Encryption Key (optional)";
|
|
1931
|
+
encryptionKeyLabel.style.display = "block";
|
|
1932
|
+
encryptionKeyLabel.style.marginBottom = "6px";
|
|
1933
|
+
encryptionKeyLabel.style.fontWeight = "500";
|
|
1934
|
+
encryptionKeyLabel.style.fontSize = "12px";
|
|
1935
|
+
encryptionKeyLabel.style.color = "#6b7280";
|
|
1936
|
+
const encryptionKeyInput = document.createElement("input");
|
|
1937
|
+
encryptionKeyInput.type = "password";
|
|
1938
|
+
encryptionKeyInput.value = device.encryptionKey || "";
|
|
1939
|
+
encryptionKeyInput.placeholder = "Paste device BLE encryption key";
|
|
1940
|
+
encryptionKeyInput.style.width = "100%";
|
|
1941
|
+
encryptionKeyInput.style.marginBottom = "12px";
|
|
1942
|
+
encryptionKeyInput.style.padding = "8px 10px";
|
|
1943
|
+
encryptionKeyInput.style.borderRadius = "6px";
|
|
1944
|
+
encryptionKeyInput.style.fontSize = "14px";
|
|
1945
|
+
encryptionKeyInput.style.boxSizing = "border-box";
|
|
1946
|
+
const keyIdLabel = document.createElement("label");
|
|
1947
|
+
keyIdLabel.textContent = "BLE Key ID (optional)";
|
|
1948
|
+
keyIdLabel.style.display = "block";
|
|
1949
|
+
keyIdLabel.style.marginBottom = "6px";
|
|
1950
|
+
keyIdLabel.style.fontWeight = "500";
|
|
1951
|
+
keyIdLabel.style.fontSize = "12px";
|
|
1952
|
+
keyIdLabel.style.color = "#6b7280";
|
|
1953
|
+
const keyIdInput = document.createElement("input");
|
|
1954
|
+
keyIdInput.type = "text";
|
|
1955
|
+
keyIdInput.value = device.keyId || "";
|
|
1956
|
+
keyIdInput.placeholder = "e.g. ff";
|
|
1957
|
+
keyIdInput.style.width = "100%";
|
|
1958
|
+
keyIdInput.style.marginBottom = "12px";
|
|
1959
|
+
keyIdInput.style.padding = "8px 10px";
|
|
1960
|
+
keyIdInput.style.borderRadius = "6px";
|
|
1961
|
+
keyIdInput.style.fontSize = "14px";
|
|
1962
|
+
keyIdInput.style.boxSizing = "border-box";
|
|
1963
|
+
const blePollingEnabledLabel = document.createElement("label");
|
|
1964
|
+
blePollingEnabledLabel.textContent = "Enable BLE Polling Fallback";
|
|
1965
|
+
blePollingEnabledLabel.style.display = "block";
|
|
1966
|
+
blePollingEnabledLabel.style.marginBottom = "6px";
|
|
1967
|
+
blePollingEnabledLabel.style.fontWeight = "500";
|
|
1968
|
+
blePollingEnabledLabel.style.fontSize = "12px";
|
|
1969
|
+
blePollingEnabledLabel.style.color = "#6b7280";
|
|
1970
|
+
const blePollingEnabledInput = document.createElement("input");
|
|
1971
|
+
blePollingEnabledInput.type = "checkbox";
|
|
1972
|
+
blePollingEnabledInput.checked = device.blePollingEnabled !== false;
|
|
1973
|
+
blePollingEnabledInput.style.marginRight = "8px";
|
|
1974
|
+
blePollingEnabledInput.style.marginBottom = "12px";
|
|
1975
|
+
const blePollIntervalLabel = document.createElement("label");
|
|
1976
|
+
blePollIntervalLabel.textContent = "BLE Polling Interval (ms)";
|
|
1977
|
+
blePollIntervalLabel.style.display = "block";
|
|
1978
|
+
blePollIntervalLabel.style.marginBottom = "6px";
|
|
1979
|
+
blePollIntervalLabel.style.fontWeight = "500";
|
|
1980
|
+
blePollIntervalLabel.style.fontSize = "12px";
|
|
1981
|
+
blePollIntervalLabel.style.color = "#6b7280";
|
|
1982
|
+
const blePollIntervalInput = document.createElement("input");
|
|
1983
|
+
blePollIntervalInput.type = "number";
|
|
1984
|
+
blePollIntervalInput.value = device.blePollIntervalMs || 6e5;
|
|
1985
|
+
blePollIntervalInput.min = "60000";
|
|
1986
|
+
blePollIntervalInput.step = "1000";
|
|
1987
|
+
blePollIntervalInput.style.width = "100%";
|
|
1988
|
+
blePollIntervalInput.style.marginBottom = "12px";
|
|
1989
|
+
blePollIntervalInput.style.padding = "8px 10px";
|
|
1990
|
+
blePollIntervalInput.style.borderRadius = "6px";
|
|
1991
|
+
blePollIntervalInput.style.fontSize = "14px";
|
|
1992
|
+
blePollIntervalInput.style.boxSizing = "border-box";
|
|
1993
|
+
const buttons = document.createElement("div");
|
|
1994
|
+
buttons.style.display = "flex";
|
|
1995
|
+
buttons.style.gap = "10px";
|
|
1996
|
+
buttons.style.justifyContent = "flex-end";
|
|
1997
|
+
buttons.style.marginTop = "18px";
|
|
1998
|
+
buttons.style.paddingTop = "18px";
|
|
1999
|
+
buttons.style.borderTop = "1px solid rgba(0, 0, 0, 0.08)";
|
|
2000
|
+
const cancelBtn = document.createElement("button");
|
|
2001
|
+
cancelBtn.textContent = "Cancel";
|
|
2002
|
+
cancelBtn.className = "secondary";
|
|
2003
|
+
cancelBtn.style.background = "#6b7280";
|
|
2004
|
+
cancelBtn.style.padding = "8px 16px";
|
|
2005
|
+
cancelBtn.style.fontSize = "13px";
|
|
2006
|
+
const importBtn = document.createElement("button");
|
|
2007
|
+
importBtn.textContent = "Add to Config";
|
|
2008
|
+
importBtn.style.background = "var(--switchbot-red, #ef4444)";
|
|
2009
|
+
importBtn.style.padding = "8px 20px";
|
|
2010
|
+
importBtn.style.fontSize = "13px";
|
|
2011
|
+
const cleanup = (result) => {
|
|
2012
|
+
div.remove();
|
|
2013
|
+
resolve(result);
|
|
2014
|
+
};
|
|
2015
|
+
cancelBtn.onclick = () => cleanup(null);
|
|
2016
|
+
importBtn.onclick = () => {
|
|
2017
|
+
let finalName = nameInput.value;
|
|
2018
|
+
if (!finalName || finalName === "undefined") {
|
|
2019
|
+
finalName = device.id || "";
|
|
2020
|
+
}
|
|
2021
|
+
cleanup({
|
|
2022
|
+
configDeviceName: finalName,
|
|
2023
|
+
configDeviceType: typeSelect.value || device.type,
|
|
2024
|
+
address: macInput.value || void 0,
|
|
2025
|
+
connectionPreference: connectionPrefSelect.value || void 0,
|
|
2026
|
+
room: roomInput.value || void 0,
|
|
2027
|
+
encryptionKey: encryptionKeyInput.value || void 0,
|
|
2028
|
+
keyId: keyIdInput.value || void 0,
|
|
2029
|
+
refreshRate: Number(openApiRefreshInput.value) || 300,
|
|
2030
|
+
blePollingEnabled: blePollingEnabledInput.checked,
|
|
2031
|
+
blePollIntervalMs: Number(blePollIntervalInput.value) || 6e5
|
|
2032
|
+
});
|
|
2033
|
+
};
|
|
2034
|
+
div.addEventListener("click", (event) => {
|
|
2035
|
+
if (event.target === div) {
|
|
2036
|
+
cleanup(null);
|
|
2037
|
+
}
|
|
2038
|
+
});
|
|
2039
|
+
buttons.appendChild(cancelBtn);
|
|
2040
|
+
buttons.appendChild(importBtn);
|
|
2041
|
+
contentDiv.appendChild(nameLabel);
|
|
2042
|
+
contentDiv.appendChild(nameInput);
|
|
2043
|
+
contentDiv.appendChild(typeLabel);
|
|
2044
|
+
contentDiv.appendChild(typeSelect);
|
|
2045
|
+
contentDiv.appendChild(connectionPrefLabel);
|
|
2046
|
+
contentDiv.appendChild(connectionPrefSelect);
|
|
2047
|
+
contentDiv.appendChild(roomLabel);
|
|
2048
|
+
contentDiv.appendChild(roomInput);
|
|
2049
|
+
contentDiv.appendChild(macLabel);
|
|
2050
|
+
contentDiv.appendChild(macInput);
|
|
2051
|
+
contentDiv.appendChild(encryptionKeyLabel);
|
|
2052
|
+
contentDiv.appendChild(encryptionKeyInput);
|
|
2053
|
+
contentDiv.appendChild(keyIdLabel);
|
|
2054
|
+
contentDiv.appendChild(keyIdInput);
|
|
2055
|
+
contentDiv.appendChild(openApiRefreshLabel);
|
|
2056
|
+
contentDiv.appendChild(openApiRefreshInput);
|
|
2057
|
+
contentDiv.appendChild(blePollingEnabledLabel);
|
|
2058
|
+
contentDiv.appendChild(blePollingEnabledInput);
|
|
2059
|
+
contentDiv.appendChild(blePollIntervalLabel);
|
|
2060
|
+
contentDiv.appendChild(blePollIntervalInput);
|
|
2061
|
+
contentDiv.appendChild(buttons);
|
|
2062
|
+
modal.appendChild(title);
|
|
2063
|
+
modal.appendChild(contentDiv);
|
|
2064
|
+
div.appendChild(modal);
|
|
2065
|
+
document.body.appendChild(div);
|
|
2066
|
+
nameInput.focus();
|
|
2067
|
+
});
|
|
2068
|
+
}
|
|
2069
|
+
async function editDevice(device) {
|
|
2070
|
+
const openApiRefreshLabel = document.createElement("label");
|
|
2071
|
+
openApiRefreshLabel.textContent = "OpenAPI Polling Interval (seconds)";
|
|
2072
|
+
openApiRefreshLabel.style.display = "block";
|
|
2073
|
+
openApiRefreshLabel.style.marginBottom = "6px";
|
|
2074
|
+
openApiRefreshLabel.style.fontWeight = "500";
|
|
2075
|
+
openApiRefreshLabel.style.fontSize = "12px";
|
|
2076
|
+
openApiRefreshLabel.style.color = "#6b7280";
|
|
2077
|
+
openApiRefreshLabel.title = "How often to poll this device via OpenAPI for status (in seconds). Overrides platform value if set. Default: 300 (5 minutes). Minimum: 30.";
|
|
2078
|
+
const openApiRefreshInput = document.createElement("input");
|
|
2079
|
+
openApiRefreshInput.type = "number";
|
|
2080
|
+
openApiRefreshInput.value = device.refreshRate || 300;
|
|
2081
|
+
openApiRefreshInput.min = "30";
|
|
2082
|
+
openApiRefreshInput.step = "1";
|
|
2083
|
+
openApiRefreshInput.style.width = "100%";
|
|
2084
|
+
openApiRefreshInput.style.marginBottom = "12px";
|
|
2085
|
+
openApiRefreshInput.style.padding = "8px 10px";
|
|
2086
|
+
openApiRefreshInput.style.borderRadius = "6px";
|
|
2087
|
+
openApiRefreshInput.style.fontSize = "14px";
|
|
2088
|
+
openApiRefreshInput.style.boxSizing = "border-box";
|
|
2089
|
+
const blePollingEnabledLabel = document.createElement("label");
|
|
2090
|
+
blePollingEnabledLabel.textContent = "Enable BLE Polling Fallback";
|
|
2091
|
+
blePollingEnabledLabel.style.display = "block";
|
|
2092
|
+
blePollingEnabledLabel.style.marginBottom = "6px";
|
|
2093
|
+
blePollingEnabledLabel.style.fontWeight = "500";
|
|
2094
|
+
blePollingEnabledLabel.style.fontSize = "12px";
|
|
2095
|
+
blePollingEnabledLabel.style.color = "#6b7280";
|
|
2096
|
+
const blePollingEnabledInput = document.createElement("input");
|
|
2097
|
+
blePollingEnabledInput.type = "checkbox";
|
|
2098
|
+
blePollingEnabledInput.checked = device.blePollingEnabled !== false;
|
|
2099
|
+
blePollingEnabledInput.style.marginRight = "8px";
|
|
2100
|
+
blePollingEnabledInput.style.marginBottom = "12px";
|
|
2101
|
+
const blePollIntervalLabel = document.createElement("label");
|
|
2102
|
+
blePollIntervalLabel.textContent = "BLE Polling Interval (ms)";
|
|
2103
|
+
blePollIntervalLabel.style.display = "block";
|
|
2104
|
+
blePollIntervalLabel.style.marginBottom = "6px";
|
|
2105
|
+
blePollIntervalLabel.style.fontWeight = "500";
|
|
2106
|
+
blePollIntervalLabel.style.fontSize = "12px";
|
|
2107
|
+
blePollIntervalLabel.style.color = "#6b7280";
|
|
2108
|
+
const blePollIntervalInput = document.createElement("input");
|
|
2109
|
+
blePollIntervalInput.type = "number";
|
|
2110
|
+
blePollIntervalInput.value = device.blePollIntervalMs || 6e5;
|
|
2111
|
+
blePollIntervalInput.min = "60000";
|
|
2112
|
+
blePollIntervalInput.step = "1000";
|
|
2113
|
+
blePollIntervalInput.style.width = "100%";
|
|
2114
|
+
blePollIntervalInput.style.marginBottom = "12px";
|
|
2115
|
+
blePollIntervalInput.style.padding = "8px 10px";
|
|
2116
|
+
blePollIntervalInput.style.borderRadius = "6px";
|
|
2117
|
+
blePollIntervalInput.style.fontSize = "14px";
|
|
2118
|
+
blePollIntervalInput.style.boxSizing = "border-box";
|
|
2119
|
+
const typeLabel = document.createElement("label");
|
|
2120
|
+
typeLabel.textContent = "Config Device Type";
|
|
2121
|
+
typeLabel.style.display = "block";
|
|
2122
|
+
typeLabel.style.marginBottom = "6px";
|
|
2123
|
+
typeLabel.style.fontWeight = "500";
|
|
2124
|
+
typeLabel.style.fontSize = "12px";
|
|
2125
|
+
typeLabel.style.color = "#6b7280";
|
|
2126
|
+
const typeSelect = document.createElement("select");
|
|
2127
|
+
typeSelect.style.width = "100%";
|
|
2128
|
+
typeSelect.style.padding = "8px 10px";
|
|
2129
|
+
typeSelect.style.marginBottom = "12px";
|
|
2130
|
+
typeSelect.style.borderRadius = "6px";
|
|
2131
|
+
typeSelect.style.fontSize = "14px";
|
|
2132
|
+
typeSelect.style.background = getComputedStyle(document.body).backgroundColor;
|
|
2133
|
+
typeSelect.style.color = getComputedStyle(document.body).color;
|
|
2134
|
+
typeSelect.style.border = "1px solid #ccc";
|
|
2135
|
+
typeSelect.style.boxSizing = "border-box";
|
|
2136
|
+
Object.keys(DEVICE_TYPES).forEach((categoryName) => {
|
|
2137
|
+
const optgroup = document.createElement("optgroup");
|
|
2138
|
+
optgroup.label = categoryName;
|
|
2139
|
+
DEVICE_TYPES[categoryName].forEach((deviceType) => {
|
|
2140
|
+
const opt = document.createElement("option");
|
|
2141
|
+
opt.value = deviceType;
|
|
2142
|
+
opt.text = deviceType;
|
|
2143
|
+
const currentType = device.configDeviceType || device.deviceType || device.type || "";
|
|
2144
|
+
opt.selected = currentType === deviceType;
|
|
2145
|
+
optgroup.appendChild(opt);
|
|
2146
|
+
});
|
|
2147
|
+
typeSelect.appendChild(optgroup);
|
|
2148
|
+
});
|
|
2149
|
+
const div = document.createElement("div");
|
|
2150
|
+
div.style.position = "fixed";
|
|
2151
|
+
div.style.top = "0";
|
|
2152
|
+
div.style.left = "0";
|
|
2153
|
+
div.style.width = "100%";
|
|
2154
|
+
div.style.height = "100%";
|
|
2155
|
+
div.style.background = "rgba(0,0,0,0.7)";
|
|
2156
|
+
div.style.display = "flex";
|
|
2157
|
+
div.style.alignItems = "center";
|
|
2158
|
+
div.style.justifyContent = "center";
|
|
2159
|
+
div.style.zIndex = "9999";
|
|
2160
|
+
const modal = document.createElement("div");
|
|
2161
|
+
modal.style.background = getComputedStyle(document.body).backgroundColor;
|
|
2162
|
+
modal.style.color = getComputedStyle(document.body).color;
|
|
2163
|
+
modal.style.padding = "0";
|
|
2164
|
+
modal.style.borderRadius = "10px";
|
|
2165
|
+
modal.style.minWidth = "440px";
|
|
2166
|
+
modal.style.maxWidth = "90vw";
|
|
2167
|
+
modal.style.boxShadow = "0 8px 32px rgba(0,0,0,0.35)";
|
|
2168
|
+
modal.style.overflow = "hidden";
|
|
2169
|
+
modal.style.borderTop = "3px solid var(--switchbot-red, #ef4444)";
|
|
2170
|
+
const title = document.createElement("h3");
|
|
2171
|
+
title.textContent = "Edit Device";
|
|
2172
|
+
title.style.marginTop = "0";
|
|
2173
|
+
title.style.marginBottom = "16px";
|
|
2174
|
+
title.style.padding = "20px 20px 0";
|
|
2175
|
+
title.style.fontSize = "18px";
|
|
2176
|
+
title.style.fontWeight = "600";
|
|
2177
|
+
title.style.color = "var(--switchbot-red, #ef4444)";
|
|
2178
|
+
title.style.letterSpacing = "-0.02em";
|
|
2179
|
+
const contentDiv = document.createElement("div");
|
|
2180
|
+
contentDiv.style.padding = "0 20px 20px";
|
|
2181
|
+
const nameLabel = document.createElement("label");
|
|
2182
|
+
nameLabel.textContent = "Device Name";
|
|
2183
|
+
nameLabel.style.display = "block";
|
|
2184
|
+
nameLabel.style.marginBottom = "6px";
|
|
2185
|
+
nameLabel.style.fontWeight = "500";
|
|
2186
|
+
nameLabel.style.fontSize = "12px";
|
|
2187
|
+
nameLabel.style.color = "#6b7280";
|
|
2188
|
+
const nameInput = document.createElement("input");
|
|
2189
|
+
nameInput.type = "text";
|
|
2190
|
+
nameInput.value = device.name || device.id;
|
|
2191
|
+
nameInput.style.width = "100%";
|
|
2192
|
+
nameInput.style.marginBottom = "12px";
|
|
2193
|
+
nameInput.style.padding = "8px 10px";
|
|
2194
|
+
nameInput.style.borderRadius = "6px";
|
|
2195
|
+
nameInput.style.fontSize = "14px";
|
|
2196
|
+
nameInput.style.boxSizing = "border-box";
|
|
2197
|
+
nameInput.style.transition = "border-color 0.2s ease";
|
|
2198
|
+
const apiTypeLabel = document.createElement("label");
|
|
2199
|
+
apiTypeLabel.textContent = "Device Type (API - Read Only)";
|
|
2200
|
+
apiTypeLabel.style.display = "block";
|
|
2201
|
+
apiTypeLabel.style.marginBottom = "6px";
|
|
2202
|
+
apiTypeLabel.style.fontWeight = "500";
|
|
2203
|
+
apiTypeLabel.style.fontSize = "12px";
|
|
2204
|
+
apiTypeLabel.style.color = "#6b7280";
|
|
2205
|
+
const apiTypeInput = document.createElement("input");
|
|
2206
|
+
apiTypeInput.type = "text";
|
|
2207
|
+
apiTypeInput.value = device.deviceType || device.type || "Unknown";
|
|
2208
|
+
apiTypeInput.readOnly = true;
|
|
2209
|
+
apiTypeInput.style.width = "100%";
|
|
2210
|
+
apiTypeInput.style.marginBottom = "12px";
|
|
2211
|
+
apiTypeInput.style.padding = "8px 10px";
|
|
2212
|
+
apiTypeInput.style.borderRadius = "6px";
|
|
2213
|
+
apiTypeInput.style.fontSize = "13px";
|
|
2214
|
+
apiTypeInput.style.opacity = "0.6";
|
|
2215
|
+
apiTypeInput.style.cursor = "not-allowed";
|
|
2216
|
+
apiTypeInput.style.boxSizing = "border-box";
|
|
2217
|
+
apiTypeInput.style.backgroundColor = "#f9fafb";
|
|
2218
|
+
Object.keys(DEVICE_TYPES).forEach((categoryName) => {
|
|
2219
|
+
const optgroup = document.createElement("optgroup");
|
|
2220
|
+
optgroup.label = categoryName;
|
|
2221
|
+
DEVICE_TYPES[categoryName].forEach((deviceType) => {
|
|
2222
|
+
const opt = document.createElement("option");
|
|
2223
|
+
opt.value = deviceType;
|
|
2224
|
+
opt.text = deviceType;
|
|
2225
|
+
const currentType = device.configDeviceType || device.deviceType || device.type || "";
|
|
2226
|
+
opt.selected = currentType === deviceType;
|
|
2227
|
+
optgroup.appendChild(opt);
|
|
2228
|
+
});
|
|
2229
|
+
typeSelect.appendChild(optgroup);
|
|
2230
|
+
});
|
|
2231
|
+
const connectionPrefLabel = document.createElement("label");
|
|
2232
|
+
connectionPrefLabel.textContent = "Connection Preference";
|
|
2233
|
+
connectionPrefLabel.style.display = "block";
|
|
2234
|
+
connectionPrefLabel.style.marginBottom = "6px";
|
|
2235
|
+
connectionPrefLabel.style.fontWeight = "500";
|
|
2236
|
+
connectionPrefLabel.style.fontSize = "12px";
|
|
2237
|
+
connectionPrefLabel.style.color = "#6b7280";
|
|
2238
|
+
const connectionPrefSelect = document.createElement("select");
|
|
2239
|
+
connectionPrefSelect.style.width = "100%";
|
|
2240
|
+
connectionPrefSelect.style.marginBottom = "12px";
|
|
2241
|
+
connectionPrefSelect.style.padding = "8px 10px";
|
|
2242
|
+
connectionPrefSelect.style.borderRadius = "6px";
|
|
2243
|
+
connectionPrefSelect.style.fontSize = "14px";
|
|
2244
|
+
connectionPrefSelect.style.boxSizing = "border-box";
|
|
2245
|
+
["auto", "ble", "openapi"].forEach((val) => {
|
|
2246
|
+
const opt = document.createElement("option");
|
|
2247
|
+
opt.value = val;
|
|
2248
|
+
opt.text = val.charAt(0).toUpperCase() + val.slice(1);
|
|
2249
|
+
opt.selected = (device.connectionPreference || "auto") === val;
|
|
2250
|
+
connectionPrefSelect.appendChild(opt);
|
|
2251
|
+
});
|
|
2252
|
+
const roomLabel = document.createElement("label");
|
|
2253
|
+
roomLabel.textContent = "Room";
|
|
2254
|
+
roomLabel.style.display = "block";
|
|
2255
|
+
roomLabel.style.marginBottom = "6px";
|
|
2256
|
+
roomLabel.style.fontWeight = "500";
|
|
2257
|
+
roomLabel.style.fontSize = "12px";
|
|
2258
|
+
roomLabel.style.color = "#6b7280";
|
|
2259
|
+
const roomInput = document.createElement("input");
|
|
2260
|
+
roomInput.type = "text";
|
|
2261
|
+
roomInput.value = device.room || "";
|
|
2262
|
+
roomInput.placeholder = "Optional room/location metadata";
|
|
2263
|
+
roomInput.style.width = "100%";
|
|
2264
|
+
roomInput.style.marginBottom = "12px";
|
|
2265
|
+
roomInput.style.padding = "8px 10px";
|
|
2266
|
+
roomInput.style.borderRadius = "6px";
|
|
2267
|
+
roomInput.style.fontSize = "14px";
|
|
2268
|
+
roomInput.style.boxSizing = "border-box";
|
|
2269
|
+
const encryptionKeyLabel = document.createElement("label");
|
|
2270
|
+
encryptionKeyLabel.textContent = "BLE Encryption Key (optional)";
|
|
2271
|
+
encryptionKeyLabel.style.display = "block";
|
|
2272
|
+
encryptionKeyLabel.style.marginBottom = "6px";
|
|
2273
|
+
encryptionKeyLabel.style.fontWeight = "500";
|
|
2274
|
+
encryptionKeyLabel.style.fontSize = "12px";
|
|
2275
|
+
encryptionKeyLabel.style.color = "#6b7280";
|
|
2276
|
+
const encryptionKeyInput = document.createElement("input");
|
|
2277
|
+
encryptionKeyInput.type = "password";
|
|
2278
|
+
encryptionKeyInput.value = device.encryptionKey || "";
|
|
2279
|
+
encryptionKeyInput.placeholder = "Paste device BLE encryption key";
|
|
2280
|
+
encryptionKeyInput.style.width = "100%";
|
|
2281
|
+
encryptionKeyInput.style.marginBottom = "12px";
|
|
2282
|
+
encryptionKeyInput.style.padding = "8px 10px";
|
|
2283
|
+
encryptionKeyInput.style.borderRadius = "6px";
|
|
2284
|
+
encryptionKeyInput.style.fontSize = "14px";
|
|
2285
|
+
encryptionKeyInput.style.boxSizing = "border-box";
|
|
2286
|
+
const keyIdLabel = document.createElement("label");
|
|
2287
|
+
keyIdLabel.textContent = "BLE Key ID (optional)";
|
|
2288
|
+
keyIdLabel.style.display = "block";
|
|
2289
|
+
keyIdLabel.style.marginBottom = "6px";
|
|
2290
|
+
keyIdLabel.style.fontWeight = "500";
|
|
2291
|
+
keyIdLabel.style.fontSize = "12px";
|
|
2292
|
+
keyIdLabel.style.color = "#6b7280";
|
|
2293
|
+
const keyIdInput = document.createElement("input");
|
|
2294
|
+
keyIdInput.type = "text";
|
|
2295
|
+
keyIdInput.value = device.keyId || "";
|
|
2296
|
+
keyIdInput.placeholder = "e.g. ff";
|
|
2297
|
+
keyIdInput.style.width = "100%";
|
|
2298
|
+
keyIdInput.style.marginBottom = "12px";
|
|
2299
|
+
keyIdInput.style.padding = "8px 10px";
|
|
2300
|
+
keyIdInput.style.borderRadius = "6px";
|
|
2301
|
+
keyIdInput.style.fontSize = "14px";
|
|
2302
|
+
keyIdInput.style.boxSizing = "border-box";
|
|
2303
|
+
const errorMessage = document.createElement("div");
|
|
2304
|
+
errorMessage.style.color = "var(--switchbot-red, #ef4444)";
|
|
2305
|
+
errorMessage.style.marginBottom = "12px";
|
|
2306
|
+
errorMessage.style.fontSize = "12px";
|
|
2307
|
+
errorMessage.style.display = "none";
|
|
2308
|
+
errorMessage.style.padding = "8px 10px";
|
|
2309
|
+
errorMessage.style.background = "var(--switchbot-red-light, #fee2e2)";
|
|
2310
|
+
errorMessage.style.borderRadius = "6px";
|
|
2311
|
+
errorMessage.style.fontWeight = "500";
|
|
2312
|
+
const buttons = document.createElement("div");
|
|
2313
|
+
buttons.style.display = "flex";
|
|
2314
|
+
buttons.style.gap = "10px";
|
|
2315
|
+
buttons.style.justifyContent = "flex-end";
|
|
2316
|
+
buttons.style.marginTop = "18px";
|
|
2317
|
+
buttons.style.paddingTop = "18px";
|
|
2318
|
+
buttons.style.borderTop = "1px solid rgba(0, 0, 0, 0.08)";
|
|
2319
|
+
const cancelBtn = document.createElement("button");
|
|
2320
|
+
cancelBtn.textContent = "Cancel";
|
|
2321
|
+
cancelBtn.className = "secondary";
|
|
2322
|
+
cancelBtn.style.background = "#6b7280";
|
|
2323
|
+
cancelBtn.style.padding = "8px 16px";
|
|
2324
|
+
cancelBtn.style.fontSize = "13px";
|
|
2325
|
+
cancelBtn.onclick = () => div.remove();
|
|
2326
|
+
const saveBtn = document.createElement("button");
|
|
2327
|
+
saveBtn.textContent = "Save";
|
|
2328
|
+
saveBtn.style.background = "var(--switchbot-red, #ef4444)";
|
|
2329
|
+
saveBtn.style.padding = "8px 20px";
|
|
2330
|
+
saveBtn.style.fontSize = "13px";
|
|
2331
|
+
saveBtn.onclick = async () => {
|
|
2332
|
+
try {
|
|
2333
|
+
const { updateDevice: updateDevice2, syncParentPluginConfigFromDisk: syncParentPluginConfigFromDisk2, fetchDevices: fetchDevices2 } = await Promise.resolve().then(() => (init_api(), api_exports));
|
|
2334
|
+
const { renderDeviceList: renderDeviceList2 } = await Promise.resolve().then(() => (init_render(), render_exports));
|
|
2335
|
+
const params = {
|
|
2336
|
+
deviceId: device.id,
|
|
2337
|
+
configDeviceName: nameInput.value || void 0,
|
|
2338
|
+
configDeviceType: typeSelect.value,
|
|
2339
|
+
connectionPreference: connectionPrefSelect.value,
|
|
2340
|
+
room: roomInput.value || void 0,
|
|
2341
|
+
encryptionKey: encryptionKeyInput.value || void 0,
|
|
2342
|
+
keyId: keyIdInput.value || void 0,
|
|
2343
|
+
refreshRate: Number(openApiRefreshInput.value) || 300,
|
|
2344
|
+
blePollingEnabled: blePollingEnabledInput.checked,
|
|
2345
|
+
blePollIntervalMs: Number(blePollIntervalInput.value) || 6e5
|
|
2346
|
+
};
|
|
2347
|
+
const options = {};
|
|
2348
|
+
if (params.connectionPreference !== void 0) {
|
|
2349
|
+
options.connectionPreference = params.connectionPreference;
|
|
2350
|
+
}
|
|
2351
|
+
if (params.room !== void 0) {
|
|
2352
|
+
options.room = params.room;
|
|
2353
|
+
}
|
|
2354
|
+
if (params.encryptionKey !== void 0) {
|
|
2355
|
+
options.encryptionKey = params.encryptionKey;
|
|
2356
|
+
}
|
|
2357
|
+
if (params.keyId !== void 0) {
|
|
2358
|
+
options.keyId = params.keyId;
|
|
2359
|
+
}
|
|
2360
|
+
if (params.refreshRate !== void 0) {
|
|
2361
|
+
options.refreshRate = params.refreshRate;
|
|
2362
|
+
}
|
|
2363
|
+
if (params.blePollingEnabled !== void 0) {
|
|
2364
|
+
options.blePollingEnabled = params.blePollingEnabled;
|
|
2365
|
+
}
|
|
2366
|
+
if (params.blePollIntervalMs !== void 0) {
|
|
2367
|
+
options.blePollIntervalMs = params.blePollIntervalMs;
|
|
2368
|
+
}
|
|
2369
|
+
await updateDevice2(
|
|
2370
|
+
params.deviceId,
|
|
2371
|
+
params.configDeviceName,
|
|
2372
|
+
params.configDeviceType,
|
|
2373
|
+
options
|
|
2374
|
+
);
|
|
2375
|
+
await syncParentPluginConfigFromDisk2();
|
|
2376
|
+
contentDiv.appendChild(openApiRefreshLabel);
|
|
2377
|
+
contentDiv.appendChild(openApiRefreshInput);
|
|
2378
|
+
uiLog.info("[Edit Device] Refreshing device list after update");
|
|
2379
|
+
const list = await fetchDevices2();
|
|
2380
|
+
renderDeviceList2(list);
|
|
2381
|
+
div.remove();
|
|
2382
|
+
} catch (e) {
|
|
2383
|
+
uiLog.error("Update error:", e);
|
|
2384
|
+
errorMessage.textContent = `Error: ${e instanceof Error ? e.message : "Failed to update device"}`;
|
|
2385
|
+
errorMessage.style.display = "block";
|
|
2386
|
+
}
|
|
2387
|
+
};
|
|
2388
|
+
buttons.appendChild(cancelBtn);
|
|
2389
|
+
buttons.appendChild(saveBtn);
|
|
2390
|
+
contentDiv.appendChild(nameLabel);
|
|
2391
|
+
contentDiv.appendChild(nameInput);
|
|
2392
|
+
contentDiv.appendChild(apiTypeLabel);
|
|
2393
|
+
contentDiv.appendChild(apiTypeInput);
|
|
2394
|
+
contentDiv.appendChild(typeLabel);
|
|
2395
|
+
contentDiv.appendChild(typeSelect);
|
|
2396
|
+
contentDiv.appendChild(connectionPrefLabel);
|
|
2397
|
+
contentDiv.appendChild(connectionPrefSelect);
|
|
2398
|
+
contentDiv.appendChild(roomLabel);
|
|
2399
|
+
contentDiv.appendChild(roomInput);
|
|
2400
|
+
contentDiv.appendChild(encryptionKeyLabel);
|
|
2401
|
+
contentDiv.appendChild(encryptionKeyInput);
|
|
2402
|
+
contentDiv.appendChild(keyIdLabel);
|
|
2403
|
+
contentDiv.appendChild(keyIdInput);
|
|
2404
|
+
contentDiv.appendChild(openApiRefreshLabel);
|
|
2405
|
+
contentDiv.appendChild(openApiRefreshInput);
|
|
2406
|
+
contentDiv.appendChild(blePollingEnabledLabel);
|
|
2407
|
+
contentDiv.appendChild(blePollingEnabledInput);
|
|
2408
|
+
contentDiv.appendChild(blePollIntervalLabel);
|
|
2409
|
+
contentDiv.appendChild(blePollIntervalInput);
|
|
2410
|
+
contentDiv.appendChild(errorMessage);
|
|
2411
|
+
contentDiv.appendChild(buttons);
|
|
2412
|
+
modal.appendChild(title);
|
|
2413
|
+
modal.appendChild(contentDiv);
|
|
2414
|
+
div.appendChild(modal);
|
|
2415
|
+
document.body.appendChild(div);
|
|
2416
|
+
nameInput.focus();
|
|
2417
|
+
}
|
|
2418
|
+
var init_modals = __esm({
|
|
2419
|
+
"src/homebridge-ui/public/js/modals.ts"() {
|
|
2420
|
+
"use strict";
|
|
2421
|
+
init_constants();
|
|
2422
|
+
init_logger();
|
|
2423
|
+
}
|
|
2424
|
+
});
|
|
2425
|
+
|
|
2426
|
+
// src/homebridge-ui/public/js/devices-delete.ts
|
|
2427
|
+
var devices_delete_exports = {};
|
|
2428
|
+
__export(devices_delete_exports, {
|
|
2429
|
+
deleteAllDevicesFromConfig: () => deleteAllDevicesFromConfig,
|
|
2430
|
+
deleteDeviceFromConfig: () => deleteDeviceFromConfig
|
|
2431
|
+
});
|
|
2432
|
+
async function confirmDeleteDialog(deviceNameOrId) {
|
|
2433
|
+
return new Promise((resolve) => {
|
|
2434
|
+
const overlay = document.createElement("div");
|
|
2435
|
+
overlay.style.position = "fixed";
|
|
2436
|
+
overlay.style.top = "0";
|
|
2437
|
+
overlay.style.left = "0";
|
|
2438
|
+
overlay.style.width = "100%";
|
|
2439
|
+
overlay.style.height = "100%";
|
|
2440
|
+
overlay.style.background = "rgba(0,0,0,0.6)";
|
|
2441
|
+
overlay.style.display = "flex";
|
|
2442
|
+
overlay.style.alignItems = "center";
|
|
2443
|
+
overlay.style.justifyContent = "center";
|
|
2444
|
+
overlay.style.zIndex = "10000";
|
|
2445
|
+
const modal = document.createElement("div");
|
|
2446
|
+
modal.style.background = getComputedStyle(document.body).backgroundColor;
|
|
2447
|
+
modal.style.color = getComputedStyle(document.body).color;
|
|
2448
|
+
modal.style.padding = "20px";
|
|
2449
|
+
modal.style.borderRadius = "10px";
|
|
2450
|
+
modal.style.minWidth = "340px";
|
|
2451
|
+
modal.style.maxWidth = "90vw";
|
|
2452
|
+
modal.style.boxShadow = "0 12px 40px rgba(0,0,0,0.35)";
|
|
2453
|
+
const title = document.createElement("h3");
|
|
2454
|
+
title.textContent = "Delete Device";
|
|
2455
|
+
title.style.margin = "0 0 10px 0";
|
|
2456
|
+
const message = document.createElement("p");
|
|
2457
|
+
message.textContent = `Remove "${deviceNameOrId}" from configuration?`;
|
|
2458
|
+
message.style.margin = "0 0 16px 0";
|
|
2459
|
+
const actions = document.createElement("div");
|
|
2460
|
+
actions.style.display = "flex";
|
|
2461
|
+
actions.style.justifyContent = "flex-end";
|
|
2462
|
+
actions.style.gap = "8px";
|
|
2463
|
+
const cancelBtn = document.createElement("button");
|
|
2464
|
+
cancelBtn.textContent = "Cancel";
|
|
2465
|
+
cancelBtn.style.background = "#6b7280";
|
|
2466
|
+
const deleteBtn = document.createElement("button");
|
|
2467
|
+
deleteBtn.textContent = "Delete";
|
|
2468
|
+
deleteBtn.style.background = "#ef4444";
|
|
2469
|
+
const cleanup = (result) => {
|
|
2470
|
+
overlay.remove();
|
|
2471
|
+
resolve(result);
|
|
2472
|
+
};
|
|
2473
|
+
cancelBtn.onclick = () => cleanup(false);
|
|
2474
|
+
deleteBtn.onclick = () => cleanup(true);
|
|
2475
|
+
overlay.addEventListener("click", (event) => {
|
|
2476
|
+
if (event.target === overlay) {
|
|
2477
|
+
cleanup(false);
|
|
2478
|
+
}
|
|
2479
|
+
});
|
|
2480
|
+
actions.appendChild(cancelBtn);
|
|
2481
|
+
actions.appendChild(deleteBtn);
|
|
2482
|
+
modal.appendChild(title);
|
|
2483
|
+
modal.appendChild(message);
|
|
2484
|
+
modal.appendChild(actions);
|
|
2485
|
+
overlay.appendChild(modal);
|
|
2486
|
+
document.body.appendChild(overlay);
|
|
2487
|
+
});
|
|
2488
|
+
}
|
|
2489
|
+
async function deleteDeviceFromConfig(deviceId, deviceName) {
|
|
2490
|
+
uiLog.info("Delete button clicked for device:", deviceId, deviceName);
|
|
2491
|
+
const confirmed = await confirmDeleteDialog(deviceName || deviceId);
|
|
2492
|
+
if (!confirmed) {
|
|
2493
|
+
uiLog.info("Delete cancelled by user");
|
|
2494
|
+
return;
|
|
2495
|
+
}
|
|
2496
|
+
try {
|
|
2497
|
+
showBusyUi();
|
|
2498
|
+
uiLog.info("Deleting device from config:", deviceId);
|
|
2499
|
+
const resp = await deleteDevice(deviceId);
|
|
2500
|
+
uiLog.info("Delete response:", resp);
|
|
2501
|
+
uiLog.info("Syncing parent config from disk...");
|
|
2502
|
+
const synced = await syncParentPluginConfigFromDisk(true);
|
|
2503
|
+
if (!synced) {
|
|
2504
|
+
toastWarning("Device deleted, but configuration sync failed");
|
|
2505
|
+
}
|
|
2506
|
+
uiLog.info("Refreshing device list...");
|
|
2507
|
+
const list = await fetchDevices();
|
|
2508
|
+
uiLog.info("Rendering devices:", list.length);
|
|
2509
|
+
renderDeviceList(list);
|
|
2510
|
+
uiLog.info("\u2713 Device deleted successfully");
|
|
2511
|
+
toastSuccess(`Device "${deviceName || deviceId}" deleted successfully`);
|
|
2512
|
+
} catch (e) {
|
|
2513
|
+
uiLog.error("Delete error:", e);
|
|
2514
|
+
toastError(e instanceof Error ? e.message : "Failed to delete device");
|
|
2515
|
+
} finally {
|
|
2516
|
+
hideBusyUi();
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
async function confirmDeleteAllDialog(deviceCount) {
|
|
2520
|
+
return new Promise((resolve) => {
|
|
2521
|
+
const overlay = document.createElement("div");
|
|
2522
|
+
overlay.style.position = "fixed";
|
|
2523
|
+
overlay.style.top = "0";
|
|
2524
|
+
overlay.style.left = "0";
|
|
2525
|
+
overlay.style.width = "100%";
|
|
2526
|
+
overlay.style.height = "100%";
|
|
2527
|
+
overlay.style.background = "rgba(0,0,0,0.7)";
|
|
2528
|
+
overlay.style.display = "flex";
|
|
2529
|
+
overlay.style.alignItems = "center";
|
|
2530
|
+
overlay.style.justifyContent = "center";
|
|
2531
|
+
overlay.style.zIndex = "10000";
|
|
2532
|
+
const modal = document.createElement("div");
|
|
2533
|
+
modal.style.background = getComputedStyle(document.body).backgroundColor;
|
|
2534
|
+
modal.style.color = getComputedStyle(document.body).color;
|
|
2535
|
+
modal.style.padding = "20px";
|
|
2536
|
+
modal.style.borderRadius = "10px";
|
|
2537
|
+
modal.style.minWidth = "380px";
|
|
2538
|
+
modal.style.maxWidth = "90vw";
|
|
2539
|
+
modal.style.boxShadow = "0 12px 40px rgba(0,0,0,0.35)";
|
|
2540
|
+
modal.style.borderTop = "3px solid #ef4444";
|
|
2541
|
+
const title = document.createElement("h3");
|
|
2542
|
+
title.textContent = "\u26A0\uFE0F Remove All Devices";
|
|
2543
|
+
title.style.margin = "0 0 12px 0";
|
|
2544
|
+
title.style.color = "#ef4444";
|
|
2545
|
+
const message = document.createElement("p");
|
|
2546
|
+
message.innerHTML = `Are you sure you want to remove <strong>all ${deviceCount} device(s)</strong> from your configuration?<br><br>This action cannot be undone.`;
|
|
2547
|
+
message.style.margin = "0 0 18px 0";
|
|
2548
|
+
message.style.lineHeight = "1.5";
|
|
2549
|
+
const actions = document.createElement("div");
|
|
2550
|
+
actions.style.display = "flex";
|
|
2551
|
+
actions.style.justifyContent = "flex-end";
|
|
2552
|
+
actions.style.gap = "10px";
|
|
2553
|
+
const cancelBtn = document.createElement("button");
|
|
2554
|
+
cancelBtn.textContent = "Cancel";
|
|
2555
|
+
cancelBtn.style.background = "#6b7280";
|
|
2556
|
+
cancelBtn.style.padding = "8px 16px";
|
|
2557
|
+
cancelBtn.style.fontSize = "13px";
|
|
2558
|
+
cancelBtn.className = "secondary";
|
|
2559
|
+
const deleteBtn = document.createElement("button");
|
|
2560
|
+
deleteBtn.textContent = "Remove All";
|
|
2561
|
+
deleteBtn.style.background = "#ef4444";
|
|
2562
|
+
deleteBtn.style.padding = "8px 20px";
|
|
2563
|
+
deleteBtn.style.fontSize = "13px";
|
|
2564
|
+
const cleanup = (result) => {
|
|
2565
|
+
overlay.remove();
|
|
2566
|
+
resolve(result);
|
|
2567
|
+
};
|
|
2568
|
+
cancelBtn.onclick = () => cleanup(false);
|
|
2569
|
+
deleteBtn.onclick = () => cleanup(true);
|
|
2570
|
+
overlay.addEventListener("click", (event) => {
|
|
2571
|
+
if (event.target === overlay) {
|
|
2572
|
+
cleanup(false);
|
|
2573
|
+
}
|
|
2574
|
+
});
|
|
2575
|
+
actions.appendChild(cancelBtn);
|
|
2576
|
+
actions.appendChild(deleteBtn);
|
|
2577
|
+
modal.appendChild(title);
|
|
2578
|
+
modal.appendChild(message);
|
|
2579
|
+
modal.appendChild(actions);
|
|
2580
|
+
overlay.appendChild(modal);
|
|
2581
|
+
document.body.appendChild(overlay);
|
|
2582
|
+
});
|
|
2583
|
+
}
|
|
2584
|
+
async function deleteAllDevicesFromConfig() {
|
|
2585
|
+
uiLog.info("Remove All Devices button clicked");
|
|
2586
|
+
try {
|
|
2587
|
+
const list = await fetchDevices();
|
|
2588
|
+
if (!list || list.length === 0) {
|
|
2589
|
+
toastWarning("No devices to remove");
|
|
2590
|
+
return;
|
|
2591
|
+
}
|
|
2592
|
+
const confirmed = await confirmDeleteAllDialog(list.length);
|
|
2593
|
+
if (!confirmed) {
|
|
2594
|
+
uiLog.info("Remove all cancelled by user");
|
|
2595
|
+
return;
|
|
2596
|
+
}
|
|
2597
|
+
showBusyUi();
|
|
2598
|
+
uiLog.info("Deleting all devices from config");
|
|
2599
|
+
const resp = await deleteAllDevices();
|
|
2600
|
+
uiLog.info("Delete all response:", resp);
|
|
2601
|
+
uiLog.info("Syncing parent config from disk...");
|
|
2602
|
+
const synced = await syncParentPluginConfigFromDisk(true);
|
|
2603
|
+
if (!synced) {
|
|
2604
|
+
toastWarning("Devices deleted, but configuration sync failed");
|
|
2605
|
+
}
|
|
2606
|
+
uiLog.info("Refreshing device list...");
|
|
2607
|
+
const updatedList = await fetchDevices();
|
|
2608
|
+
uiLog.info("Rendering devices:", updatedList.length);
|
|
2609
|
+
renderDeviceList(updatedList);
|
|
2610
|
+
const deletedCount = resp?.deletedCount || 0;
|
|
2611
|
+
uiLog.info(`\u2713 Removed ${deletedCount} device(s) successfully`);
|
|
2612
|
+
toastSuccess(`Removed ${deletedCount} device(s) successfully`);
|
|
2613
|
+
} catch (e) {
|
|
2614
|
+
uiLog.error("Delete all error:", e);
|
|
2615
|
+
toastError(e instanceof Error ? e.message : "Failed to delete all devices");
|
|
2616
|
+
} finally {
|
|
2617
|
+
hideBusyUi();
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
var init_devices_delete = __esm({
|
|
2621
|
+
"src/homebridge-ui/public/js/devices-delete.ts"() {
|
|
2622
|
+
"use strict";
|
|
2623
|
+
init_api();
|
|
2624
|
+
init_logger();
|
|
2625
|
+
init_modal();
|
|
2626
|
+
init_render();
|
|
2627
|
+
init_toast();
|
|
2628
|
+
}
|
|
2629
|
+
});
|
|
2630
|
+
|
|
2631
|
+
// src/homebridge-ui/public/js/render.ts
|
|
2632
|
+
var render_exports = {};
|
|
2633
|
+
__export(render_exports, {
|
|
2634
|
+
filterDevices: () => filterDevices,
|
|
2635
|
+
getDiscoveryPreferences: () => getDiscoveryPreferences,
|
|
2636
|
+
getRssiSignalQuality: () => getRssiSignalQuality,
|
|
2637
|
+
renderBadge: () => renderBadge,
|
|
2638
|
+
renderConnectionBadge: () => renderConnectionBadge,
|
|
2639
|
+
renderDeviceDetailsPanel: () => renderDeviceDetailsPanel,
|
|
2640
|
+
renderDeviceList: () => renderDeviceList,
|
|
2641
|
+
renderDiscoveredDevices: () => renderDiscoveredDevices,
|
|
2642
|
+
renderIRBadge: () => renderIRBadge,
|
|
2643
|
+
renderSignalBars: () => renderSignalBars,
|
|
2644
|
+
renderSignalQualityBadge: () => renderSignalQualityBadge,
|
|
2645
|
+
setDiscoveryPreferences: () => setDiscoveryPreferences,
|
|
2646
|
+
sortDevices: () => sortDevices
|
|
2647
|
+
});
|
|
2648
|
+
function getRssiSignalQuality(rssi) {
|
|
2649
|
+
if (!rssi || rssi === 0) {
|
|
2650
|
+
return {
|
|
2651
|
+
level: "unknown",
|
|
2652
|
+
color: "#999",
|
|
2653
|
+
bgColor: "#f5f5f5",
|
|
2654
|
+
description: "Signal strength unknown",
|
|
2655
|
+
bars: 0
|
|
2656
|
+
};
|
|
2657
|
+
}
|
|
2658
|
+
const dbm = Math.floor(rssi);
|
|
2659
|
+
if (dbm > -60) {
|
|
2660
|
+
return {
|
|
2661
|
+
level: "excellent",
|
|
2662
|
+
color: "#34a853",
|
|
2663
|
+
bgColor: "#e8f5e9",
|
|
2664
|
+
description: `Excellent (${dbm} dBm)`,
|
|
2665
|
+
bars: 4
|
|
2666
|
+
};
|
|
2667
|
+
} else if (dbm > -75) {
|
|
2668
|
+
return {
|
|
2669
|
+
level: "good",
|
|
2670
|
+
color: "#fbbc04",
|
|
2671
|
+
bgColor: "#fffde7",
|
|
2672
|
+
description: `Good (${dbm} dBm)`,
|
|
2673
|
+
bars: 3
|
|
2674
|
+
};
|
|
2675
|
+
} else if (dbm > -85) {
|
|
2676
|
+
return {
|
|
2677
|
+
level: "fair",
|
|
2678
|
+
color: "#ff9800",
|
|
2679
|
+
bgColor: "#fff3e0",
|
|
2680
|
+
description: `Fair (${dbm} dBm)`,
|
|
2681
|
+
bars: 2
|
|
2682
|
+
};
|
|
2683
|
+
} else {
|
|
2684
|
+
return {
|
|
2685
|
+
level: "poor",
|
|
2686
|
+
color: "#ea4335",
|
|
2687
|
+
bgColor: "#ffebee",
|
|
2688
|
+
description: `Poor (${dbm} dBm) - unreliable`,
|
|
2689
|
+
bars: 1
|
|
2690
|
+
};
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
function renderSignalBars(rssi) {
|
|
2694
|
+
const quality = getRssiSignalQuality(rssi);
|
|
2695
|
+
const container = document.createElement("span");
|
|
2696
|
+
container.style.display = "inline-flex";
|
|
2697
|
+
container.style.gap = "2px";
|
|
2698
|
+
container.style.alignItems = "center";
|
|
2699
|
+
container.style.marginLeft = "8px";
|
|
2700
|
+
container.style.fontSize = "12px";
|
|
2701
|
+
for (let i = 1; i <= 4; i++) {
|
|
2702
|
+
const bar = document.createElement("span");
|
|
2703
|
+
bar.style.height = `${i * 3}px`;
|
|
2704
|
+
bar.style.width = "3px";
|
|
2705
|
+
bar.style.borderRadius = "1px";
|
|
2706
|
+
bar.style.border = `1px solid ${quality.color}`;
|
|
2707
|
+
if (i <= quality.bars) {
|
|
2708
|
+
bar.style.backgroundColor = quality.color;
|
|
2709
|
+
} else {
|
|
2710
|
+
bar.style.backgroundColor = "transparent";
|
|
2711
|
+
}
|
|
2712
|
+
container.appendChild(bar);
|
|
2713
|
+
}
|
|
2714
|
+
container.title = quality.description;
|
|
2715
|
+
return container;
|
|
2716
|
+
}
|
|
2717
|
+
function renderSignalQualityBadge(rssi) {
|
|
2718
|
+
const quality = getRssiSignalQuality(rssi);
|
|
2719
|
+
const badge = document.createElement("span");
|
|
2720
|
+
badge.textContent = quality.level.charAt(0).toUpperCase() + quality.level.slice(1);
|
|
2721
|
+
badge.style.cssText = `
|
|
2722
|
+
background: ${quality.color};
|
|
2723
|
+
color: white;
|
|
2724
|
+
padding: 2px 6px;
|
|
2725
|
+
border-radius: 3px;
|
|
2726
|
+
font-size: 10px;
|
|
2727
|
+
font-weight: 600;
|
|
2728
|
+
margin-left: 8px;
|
|
2729
|
+
`;
|
|
2730
|
+
badge.title = quality.description;
|
|
2731
|
+
return badge;
|
|
2732
|
+
}
|
|
2733
|
+
function renderBadge(text, style) {
|
|
2734
|
+
const badge = document.createElement("span");
|
|
2735
|
+
badge.textContent = text;
|
|
2736
|
+
badge.style.cssText = style;
|
|
2737
|
+
return badge;
|
|
2738
|
+
}
|
|
2739
|
+
function renderConnectionBadge(connectionType) {
|
|
2740
|
+
if (!connectionType) {
|
|
2741
|
+
return null;
|
|
2742
|
+
}
|
|
2743
|
+
const badge = renderBadge(connectionType, "");
|
|
2744
|
+
if (connectionType === "BLE") {
|
|
2745
|
+
badge.style.cssText = "background: #4285f4; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: 600; margin-left: 8px;";
|
|
2746
|
+
} else if (connectionType === "Both") {
|
|
2747
|
+
badge.style.cssText = "background: #34a853; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: 600; margin-left: 8px;";
|
|
2748
|
+
} else {
|
|
2749
|
+
badge.style.cssText = "background: #9e9e9e; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: 600; margin-left: 8px;";
|
|
2750
|
+
}
|
|
2751
|
+
return badge;
|
|
2752
|
+
}
|
|
2753
|
+
function renderIRBadge() {
|
|
2754
|
+
return renderBadge(
|
|
2755
|
+
"IR",
|
|
2756
|
+
"background: #ff6b35; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: 600; margin-left: 8px;"
|
|
2757
|
+
);
|
|
2758
|
+
}
|
|
2759
|
+
function normalizeId2(value) {
|
|
2760
|
+
return String(value ?? "").trim().toLowerCase();
|
|
2761
|
+
}
|
|
2762
|
+
function scrollToConfiguredDevice(deviceId) {
|
|
2763
|
+
const normalizedId = normalizeId2(deviceId);
|
|
2764
|
+
const target = document.querySelector(`[data-device-id="${normalizedId}"]`);
|
|
2765
|
+
if (!target) {
|
|
2766
|
+
return;
|
|
2767
|
+
}
|
|
2768
|
+
target.scrollIntoView({ behavior: "smooth", block: "center" });
|
|
2769
|
+
const originalOutline = target.style.outline;
|
|
2770
|
+
const originalBackground = target.style.background;
|
|
2771
|
+
target.style.outline = "2px solid var(--switchbot-red, #ef4444)";
|
|
2772
|
+
target.style.background = "rgba(239, 68, 68, 0.08)";
|
|
2773
|
+
setTimeout(() => {
|
|
2774
|
+
target.style.outline = originalOutline;
|
|
2775
|
+
target.style.background = originalBackground;
|
|
2776
|
+
}, 1800);
|
|
2777
|
+
}
|
|
2778
|
+
function createConnectionTestControls(device) {
|
|
2779
|
+
const controls = document.createElement("div");
|
|
2780
|
+
controls.style.display = "inline-flex";
|
|
2781
|
+
controls.style.alignItems = "center";
|
|
2782
|
+
controls.style.gap = "6px";
|
|
2783
|
+
const button = document.createElement("button");
|
|
2784
|
+
button.textContent = "Test Connection";
|
|
2785
|
+
button.className = "secondary";
|
|
2786
|
+
button.style.padding = "4px 9px";
|
|
2787
|
+
button.style.fontSize = "11px";
|
|
2788
|
+
const status = document.createElement("span");
|
|
2789
|
+
status.style.fontSize = "10px";
|
|
2790
|
+
status.style.opacity = "0.85";
|
|
2791
|
+
status.style.whiteSpace = "normal";
|
|
2792
|
+
status.style.overflowWrap = "anywhere";
|
|
2793
|
+
button.onclick = async () => {
|
|
2794
|
+
const startedAt = Date.now();
|
|
2795
|
+
button.disabled = true;
|
|
2796
|
+
button.textContent = "Testing...";
|
|
2797
|
+
status.textContent = "Checking...";
|
|
2798
|
+
status.style.color = "#6b7280";
|
|
2799
|
+
try {
|
|
2800
|
+
const { testDeviceConnection: testDeviceConnection2 } = await Promise.resolve().then(() => (init_api(), api_exports));
|
|
2801
|
+
const result = await testDeviceConnection2({
|
|
2802
|
+
deviceId: String(device?.id || device?.deviceId || ""),
|
|
2803
|
+
connectionType: device?.connectionType,
|
|
2804
|
+
address: device?.address
|
|
2805
|
+
});
|
|
2806
|
+
const measuredLatency = Number(result?.latencyMs) > 0 ? Number(result.latencyMs) : Date.now() - startedAt;
|
|
2807
|
+
if (result?.success) {
|
|
2808
|
+
const method = result?.method || "Auto";
|
|
2809
|
+
status.textContent = `\u2713 ${method} \xB7 ${measuredLatency}ms`;
|
|
2810
|
+
status.style.color = "#16a34a";
|
|
2811
|
+
} else {
|
|
2812
|
+
const detail = result?.message ? ` \xB7 ${result.message}` : "";
|
|
2813
|
+
status.textContent = `\u2717 Failed \xB7 ${measuredLatency}ms${detail}`;
|
|
2814
|
+
status.style.color = "#dc2626";
|
|
2815
|
+
}
|
|
2816
|
+
} catch (e) {
|
|
2817
|
+
status.textContent = `\u2717 Failed \xB7 ${Date.now() - startedAt}ms`;
|
|
2818
|
+
status.style.color = "#dc2626";
|
|
2819
|
+
} finally {
|
|
2820
|
+
button.disabled = false;
|
|
2821
|
+
button.textContent = "Test Connection";
|
|
2822
|
+
}
|
|
2823
|
+
};
|
|
2824
|
+
controls.appendChild(button);
|
|
2825
|
+
controls.appendChild(status);
|
|
2826
|
+
return controls;
|
|
2827
|
+
}
|
|
2828
|
+
function formatLastSeen(value) {
|
|
2829
|
+
if (!value) {
|
|
2830
|
+
return "N/A";
|
|
2831
|
+
}
|
|
2832
|
+
try {
|
|
2833
|
+
const date = new Date(value);
|
|
2834
|
+
return Number.isNaN(date.getTime()) ? String(value) : date.toLocaleString();
|
|
2835
|
+
} catch (_e) {
|
|
2836
|
+
return String(value);
|
|
2837
|
+
}
|
|
2838
|
+
}
|
|
2839
|
+
function renderDeviceDetailsPanel(device) {
|
|
2840
|
+
const details = document.createElement("div");
|
|
2841
|
+
details.className = "device-details-panel";
|
|
2842
|
+
details.style.borderTop = "1px solid #ddd";
|
|
2843
|
+
details.style.padding = "8px";
|
|
2844
|
+
details.style.borderRadius = "4px";
|
|
2845
|
+
details.style.fontSize = "12px";
|
|
2846
|
+
details.style.marginTop = "4px";
|
|
2847
|
+
const batteryHistoryKey = `batteryHistory:${device?.id || device?.deviceId}`;
|
|
2848
|
+
let batteryHistory = [];
|
|
2849
|
+
try {
|
|
2850
|
+
const raw = localStorage.getItem(batteryHistoryKey);
|
|
2851
|
+
if (raw) {
|
|
2852
|
+
batteryHistory = JSON.parse(raw);
|
|
2853
|
+
}
|
|
2854
|
+
} catch (e) {
|
|
2855
|
+
}
|
|
2856
|
+
const now = Date.now();
|
|
2857
|
+
if (typeof device?.battery === "number") {
|
|
2858
|
+
const last = batteryHistory.at(-1);
|
|
2859
|
+
if (!last || last.value !== device.battery || now - last.ts > 60 * 60 * 1e3) {
|
|
2860
|
+
batteryHistory.push({ value: device.battery, ts: now });
|
|
2861
|
+
if (batteryHistory.length > 30) {
|
|
2862
|
+
batteryHistory = batteryHistory.slice(-30);
|
|
2863
|
+
}
|
|
2864
|
+
try {
|
|
2865
|
+
localStorage.setItem(batteryHistoryKey, JSON.stringify(batteryHistory));
|
|
2866
|
+
} catch (e) {
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
const rows = [
|
|
2871
|
+
{ label: "Name", value: String(device?.name || device?.configDeviceName || "N/A") },
|
|
2872
|
+
{ label: "Device ID", value: String(device?.id || device?.deviceId || "N/A"), copyable: !!(device?.id || device?.deviceId) },
|
|
2873
|
+
{ label: "MAC Address", value: String(device?.address || "N/A"), copyable: !!device?.address },
|
|
2874
|
+
{ label: "Device Type", value: String(device?.type || device?.configDeviceType || "N/A") },
|
|
2875
|
+
{ label: "Model", value: String(device?.model || "N/A") },
|
|
2876
|
+
{ label: "Hub ID", value: String(device?.hubDeviceId || "N/A") },
|
|
2877
|
+
{ label: "Battery", value: device?.battery !== void 0 && device?.battery !== null ? `${device.battery}%` : "N/A" },
|
|
2878
|
+
{ label: "Firmware", value: String(device?.version || device?.firmware || "N/A") },
|
|
2879
|
+
{ label: "Cloud Service", value: device?.enabled === false ? "Disabled" : "Enabled" },
|
|
2880
|
+
{ label: "Last Seen", value: formatLastSeen(device?.lastSeen || device?.lastseen || device?.updatedAt) }
|
|
2881
|
+
];
|
|
2882
|
+
for (const row of rows) {
|
|
2883
|
+
const line = document.createElement("div");
|
|
2884
|
+
line.style.display = "flex";
|
|
2885
|
+
line.style.alignItems = "center";
|
|
2886
|
+
line.style.justifyContent = "space-between";
|
|
2887
|
+
line.style.gap = "8px";
|
|
2888
|
+
line.style.padding = "2px 0";
|
|
2889
|
+
const label = document.createElement("span");
|
|
2890
|
+
label.style.fontWeight = "600";
|
|
2891
|
+
label.style.minWidth = "110px";
|
|
2892
|
+
label.textContent = `${row.label}:`;
|
|
2893
|
+
const valueWrap = document.createElement("span");
|
|
2894
|
+
valueWrap.style.display = "inline-flex";
|
|
2895
|
+
valueWrap.style.alignItems = "center";
|
|
2896
|
+
valueWrap.style.gap = "6px";
|
|
2897
|
+
valueWrap.style.flex = "1";
|
|
2898
|
+
valueWrap.style.justifyContent = "flex-end";
|
|
2899
|
+
valueWrap.style.minWidth = "0";
|
|
2900
|
+
const value = document.createElement("span");
|
|
2901
|
+
value.style.fontFamily = "monospace";
|
|
2902
|
+
value.style.fontSize = "11px";
|
|
2903
|
+
value.style.opacity = "0.9";
|
|
2904
|
+
value.style.whiteSpace = "normal";
|
|
2905
|
+
value.style.overflowWrap = "anywhere";
|
|
2906
|
+
value.style.wordBreak = "break-word";
|
|
2907
|
+
value.style.textAlign = "right";
|
|
2908
|
+
value.textContent = row.value;
|
|
2909
|
+
valueWrap.appendChild(value);
|
|
2910
|
+
if (row.copyable && row.value && row.value !== "N/A") {
|
|
2911
|
+
const copyBtn = document.createElement("button");
|
|
2912
|
+
copyBtn.textContent = "\u{1F4CB}";
|
|
2913
|
+
copyBtn.title = `Copy ${row.label}`;
|
|
2914
|
+
copyBtn.style.padding = "2px 6px";
|
|
2915
|
+
copyBtn.style.fontSize = "10px";
|
|
2916
|
+
copyBtn.style.lineHeight = "1";
|
|
2917
|
+
copyBtn.style.background = "#e5e7eb";
|
|
2918
|
+
copyBtn.style.color = "#111827";
|
|
2919
|
+
copyBtn.onclick = async () => {
|
|
2920
|
+
try {
|
|
2921
|
+
await navigator.clipboard.writeText(row.value);
|
|
2922
|
+
copyBtn.textContent = "\u2713";
|
|
2923
|
+
setTimeout(() => {
|
|
2924
|
+
copyBtn.textContent = "\u{1F4CB}";
|
|
2925
|
+
}, 1200);
|
|
2926
|
+
} catch (_e) {
|
|
2927
|
+
copyBtn.textContent = "!";
|
|
2928
|
+
setTimeout(() => {
|
|
2929
|
+
copyBtn.textContent = "\u{1F4CB}";
|
|
2930
|
+
}, 1200);
|
|
2931
|
+
}
|
|
2932
|
+
};
|
|
2933
|
+
valueWrap.appendChild(copyBtn);
|
|
2934
|
+
}
|
|
2935
|
+
line.appendChild(label);
|
|
2936
|
+
line.appendChild(valueWrap);
|
|
2937
|
+
details.appendChild(line);
|
|
2938
|
+
if (row.label === "Battery" && Array.isArray(batteryHistory) && batteryHistory.length > 1) {
|
|
2939
|
+
const chart = document.createElement("div");
|
|
2940
|
+
chart.style.margin = "2px 0 8px 0";
|
|
2941
|
+
chart.style.width = "100%";
|
|
2942
|
+
chart.style.height = "28px";
|
|
2943
|
+
chart.style.display = "flex";
|
|
2944
|
+
const w = 120;
|
|
2945
|
+
const h = 24;
|
|
2946
|
+
const pad = 2;
|
|
2947
|
+
const min = Math.min(...batteryHistory.map((b) => b.value), 100);
|
|
2948
|
+
const max = Math.max(...batteryHistory.map((b) => b.value), 0);
|
|
2949
|
+
const range = max - min || 1;
|
|
2950
|
+
const points = batteryHistory.map((b, i) => {
|
|
2951
|
+
const x = pad + i * (w - 2 * pad) / (batteryHistory.length - 1);
|
|
2952
|
+
const y = pad + (h - 2 * pad) * (1 - (b.value - min) / range);
|
|
2953
|
+
return `${x},${y}`;
|
|
2954
|
+
}).join(" ");
|
|
2955
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
2956
|
+
svg.setAttribute("width", String(w));
|
|
2957
|
+
svg.setAttribute("height", String(h));
|
|
2958
|
+
svg.setAttribute("viewBox", `0 0 ${w} ${h}`);
|
|
2959
|
+
svg.style.display = "block";
|
|
2960
|
+
svg.style.background = "#f3f4f6";
|
|
2961
|
+
svg.style.borderRadius = "3px";
|
|
2962
|
+
svg.style.marginTop = "2px";
|
|
2963
|
+
svg.style.boxShadow = "0 1px 2px #0001";
|
|
2964
|
+
const polyline = document.createElementNS("http://www.w3.org/2000/svg", "polyline");
|
|
2965
|
+
polyline.setAttribute("points", points);
|
|
2966
|
+
polyline.setAttribute("fill", "none");
|
|
2967
|
+
polyline.setAttribute("stroke", "#2563eb");
|
|
2968
|
+
polyline.setAttribute("stroke-width", "2");
|
|
2969
|
+
svg.appendChild(polyline);
|
|
2970
|
+
batteryHistory.forEach((b, i) => {
|
|
2971
|
+
const x = pad + i * (w - 2 * pad) / (batteryHistory.length - 1);
|
|
2972
|
+
const y = pad + (h - 2 * pad) * (1 - (b.value - min) / range);
|
|
2973
|
+
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
2974
|
+
circle.setAttribute("cx", String(x));
|
|
2975
|
+
circle.setAttribute("cy", String(y));
|
|
2976
|
+
circle.setAttribute("r", "2.5");
|
|
2977
|
+
circle.setAttribute("fill", "#2563eb");
|
|
2978
|
+
svg.appendChild(circle);
|
|
2979
|
+
});
|
|
2980
|
+
const minLabel = document.createElementNS("http://www.w3.org/2000/svg", "text");
|
|
2981
|
+
minLabel.setAttribute("x", "2");
|
|
2982
|
+
minLabel.setAttribute("y", String(h - 2));
|
|
2983
|
+
minLabel.setAttribute("font-size", "9");
|
|
2984
|
+
minLabel.setAttribute("fill", "#888");
|
|
2985
|
+
minLabel.textContent = `${min}%`;
|
|
2986
|
+
svg.appendChild(minLabel);
|
|
2987
|
+
const maxLabel = document.createElementNS("http://www.w3.org/2000/svg", "text");
|
|
2988
|
+
maxLabel.setAttribute("x", String(w - 18));
|
|
2989
|
+
maxLabel.setAttribute("y", "10");
|
|
2990
|
+
maxLabel.setAttribute("font-size", "9");
|
|
2991
|
+
maxLabel.setAttribute("fill", "#888");
|
|
2992
|
+
maxLabel.textContent = `${max}%`;
|
|
2993
|
+
svg.appendChild(maxLabel);
|
|
2994
|
+
chart.appendChild(svg);
|
|
2995
|
+
details.appendChild(chart);
|
|
2996
|
+
}
|
|
2997
|
+
}
|
|
2998
|
+
const featureKeys = [
|
|
2999
|
+
"airQuality",
|
|
3000
|
+
"pm25",
|
|
3001
|
+
"pm10",
|
|
3002
|
+
"voc",
|
|
3003
|
+
"co2",
|
|
3004
|
+
"humidity",
|
|
3005
|
+
"temperature",
|
|
3006
|
+
"preset",
|
|
3007
|
+
"mode",
|
|
3008
|
+
"presetMode",
|
|
3009
|
+
"direction",
|
|
3010
|
+
"calibration",
|
|
3011
|
+
"multiCommand",
|
|
3012
|
+
"extendedInfo",
|
|
3013
|
+
"segmentedControl",
|
|
3014
|
+
"features",
|
|
3015
|
+
"capabilities",
|
|
3016
|
+
"state"
|
|
3017
|
+
];
|
|
3018
|
+
const shown = new Set(rows.map((r) => r.label.toLowerCase().replace(SPACES_REGEX, "")));
|
|
3019
|
+
for (const key of featureKeys) {
|
|
3020
|
+
if (device && device[key] !== void 0 && !shown.has(key.toLowerCase())) {
|
|
3021
|
+
const line = document.createElement("div");
|
|
3022
|
+
line.style.display = "flex";
|
|
3023
|
+
line.style.alignItems = "center";
|
|
3024
|
+
line.style.justifyContent = "space-between";
|
|
3025
|
+
line.style.gap = "8px";
|
|
3026
|
+
line.style.padding = "2px 0";
|
|
3027
|
+
const label = document.createElement("span");
|
|
3028
|
+
label.style.fontWeight = "600";
|
|
3029
|
+
label.style.minWidth = "110px";
|
|
3030
|
+
label.textContent = `${key.replace(CAMELCASE_REGEX, " $1").replace(FIRST_CHAR_REGEX, (s) => s.toUpperCase())}:`;
|
|
3031
|
+
const value = document.createElement("span");
|
|
3032
|
+
value.style.fontFamily = "monospace";
|
|
3033
|
+
value.style.fontSize = "11px";
|
|
3034
|
+
value.style.opacity = "0.9";
|
|
3035
|
+
value.style.whiteSpace = "normal";
|
|
3036
|
+
value.style.overflowWrap = "anywhere";
|
|
3037
|
+
value.style.wordBreak = "break-word";
|
|
3038
|
+
value.style.textAlign = "right";
|
|
3039
|
+
value.textContent = typeof device[key] === "object" ? JSON.stringify(device[key]) : String(device[key]);
|
|
3040
|
+
line.appendChild(label);
|
|
3041
|
+
line.appendChild(value);
|
|
3042
|
+
details.appendChild(line);
|
|
3043
|
+
}
|
|
3044
|
+
}
|
|
3045
|
+
return details;
|
|
3046
|
+
}
|
|
3047
|
+
async function renderDiscoveredDevices(devices, options = {}) {
|
|
3048
|
+
const ul = document.createElement("ul");
|
|
3049
|
+
ul.className = "device-grid";
|
|
3050
|
+
ul.style.maxHeight = "400px";
|
|
3051
|
+
ul.style.overflowY = "auto";
|
|
3052
|
+
ul.style.marginTop = "12px";
|
|
3053
|
+
ul.style.padding = "0";
|
|
3054
|
+
ul.style.listStyle = "none";
|
|
3055
|
+
const { addDeviceToConfig: addDeviceToConfig3 } = await Promise.resolve().then(() => (init_discovery(), discovery_exports));
|
|
3056
|
+
const { loadConfiguredDevices: loadConfiguredDevices2 } = await Promise.resolve().then(() => (init_devices(), devices_exports));
|
|
3057
|
+
const configuredIds = options.configuredIds ?? /* @__PURE__ */ new Set();
|
|
3058
|
+
const selectedIds = options.selectedIds ?? /* @__PURE__ */ new Set();
|
|
3059
|
+
const onToggleSelect = options.onToggleSelect;
|
|
3060
|
+
for (const d of devices) {
|
|
3061
|
+
if (!d || !d.id && !d.deviceId || !d.name && !d.type) {
|
|
3062
|
+
console.warn("[SwitchBot][Discovery][renderDiscoveredDevices] Device missing required fields:", d);
|
|
3063
|
+
}
|
|
3064
|
+
const deviceId = normalizeId2(d.id);
|
|
3065
|
+
const alreadyAdded = configuredIds.has(deviceId);
|
|
3066
|
+
const li = document.createElement("li");
|
|
3067
|
+
li.className = "device-item";
|
|
3068
|
+
li.style.display = "flex";
|
|
3069
|
+
li.style.flexDirection = "column";
|
|
3070
|
+
li.style.alignItems = "stretch";
|
|
3071
|
+
li.style.justifyContent = "flex-start";
|
|
3072
|
+
li.style.padding = "5px 8px";
|
|
3073
|
+
li.style.marginBottom = "0";
|
|
3074
|
+
li.style.borderRadius = "5px";
|
|
3075
|
+
li.style.transition = "all 0.2s ease";
|
|
3076
|
+
const info = document.createElement("div");
|
|
3077
|
+
info.style.flex = "1 1 auto";
|
|
3078
|
+
info.style.width = "100%";
|
|
3079
|
+
info.style.minWidth = "0";
|
|
3080
|
+
const nameContainer = document.createElement("div");
|
|
3081
|
+
nameContainer.style.display = "flex";
|
|
3082
|
+
nameContainer.style.alignItems = "center";
|
|
3083
|
+
nameContainer.style.marginBottom = "0";
|
|
3084
|
+
nameContainer.style.flexWrap = "wrap";
|
|
3085
|
+
nameContainer.style.gap = "4px";
|
|
3086
|
+
const name = document.createElement("div");
|
|
3087
|
+
name.style.fontWeight = "500";
|
|
3088
|
+
name.style.fontSize = "13px";
|
|
3089
|
+
name.textContent = d.name || d.id;
|
|
3090
|
+
const selectCheckbox = document.createElement("input");
|
|
3091
|
+
selectCheckbox.type = "checkbox";
|
|
3092
|
+
selectCheckbox.style.width = "auto";
|
|
3093
|
+
selectCheckbox.style.margin = "0 2px 0 0";
|
|
3094
|
+
selectCheckbox.checked = selectedIds.has(deviceId);
|
|
3095
|
+
if (alreadyAdded) {
|
|
3096
|
+
selectCheckbox.disabled = true;
|
|
3097
|
+
selectCheckbox.title = "Already configured";
|
|
3098
|
+
}
|
|
3099
|
+
selectCheckbox.onchange = () => {
|
|
3100
|
+
onToggleSelect?.(d, selectCheckbox.checked);
|
|
3101
|
+
window.dispatchEvent(new CustomEvent("discovery-selection-changed"));
|
|
3102
|
+
};
|
|
3103
|
+
nameContainer.appendChild(selectCheckbox);
|
|
3104
|
+
nameContainer.appendChild(name);
|
|
3105
|
+
if (d.firmwareUpdateAvailable) {
|
|
3106
|
+
const fwBadge = document.createElement("span");
|
|
3107
|
+
fwBadge.textContent = "Update Available";
|
|
3108
|
+
fwBadge.style.cssText = "background: #fb923c; color: #111; padding: 1px 7px; border-radius: 3px; font-size: 10px; font-weight: 600; margin-left: 6px;";
|
|
3109
|
+
fwBadge.title = "A firmware update is available for this device.";
|
|
3110
|
+
nameContainer.appendChild(fwBadge);
|
|
3111
|
+
}
|
|
3112
|
+
let offline = false;
|
|
3113
|
+
const lastSeen = d.lastSeen || d.lastseen || d.updatedAt;
|
|
3114
|
+
if (typeof d.offline === "boolean") {
|
|
3115
|
+
offline = d.offline;
|
|
3116
|
+
} else if (lastSeen) {
|
|
3117
|
+
try {
|
|
3118
|
+
const last = new Date(lastSeen).getTime();
|
|
3119
|
+
if (!Number.isNaN(last)) {
|
|
3120
|
+
if (Date.now() - last > 1e3 * 60 * 60) {
|
|
3121
|
+
offline = true;
|
|
3122
|
+
}
|
|
3123
|
+
}
|
|
3124
|
+
} catch {
|
|
3125
|
+
}
|
|
3126
|
+
}
|
|
3127
|
+
if (offline) {
|
|
3128
|
+
const offlineBadge = document.createElement("span");
|
|
3129
|
+
offlineBadge.textContent = "Offline";
|
|
3130
|
+
offlineBadge.style.cssText = "background: #dc2626; color: white; padding: 1px 7px; border-radius: 3px; font-size: 10px; font-weight: 600; margin-left: 6px;";
|
|
3131
|
+
offlineBadge.title = "Device is offline or unreachable.";
|
|
3132
|
+
nameContainer.appendChild(offlineBadge);
|
|
3133
|
+
}
|
|
3134
|
+
const expandedDetails = document.createElement("div");
|
|
3135
|
+
expandedDetails.style.display = "none";
|
|
3136
|
+
expandedDetails.appendChild(renderDeviceDetailsPanel(d));
|
|
3137
|
+
const expandBtn = document.createElement("button");
|
|
3138
|
+
expandBtn.textContent = "\u25BE";
|
|
3139
|
+
expandBtn.title = "Show details";
|
|
3140
|
+
expandBtn.style.padding = "2px 6px";
|
|
3141
|
+
expandBtn.style.fontSize = "11px";
|
|
3142
|
+
expandBtn.style.marginLeft = "4px";
|
|
3143
|
+
expandBtn.style.background = "#e5e7eb";
|
|
3144
|
+
expandBtn.style.color = "#111827";
|
|
3145
|
+
expandBtn.style.transition = "transform 0.2s ease";
|
|
3146
|
+
expandBtn.onclick = () => {
|
|
3147
|
+
const isHidden = expandedDetails.style.display === "none";
|
|
3148
|
+
expandedDetails.style.display = isHidden ? "block" : "none";
|
|
3149
|
+
expandBtn.style.transform = isHidden ? "rotate(180deg)" : "rotate(0deg)";
|
|
3150
|
+
};
|
|
3151
|
+
nameContainer.appendChild(expandBtn);
|
|
3152
|
+
const duplicateBadge = document.createElement("span");
|
|
3153
|
+
duplicateBadge.textContent = alreadyAdded ? "\u2713 Already Added" : "\u2795 New Device";
|
|
3154
|
+
duplicateBadge.style.cssText = alreadyAdded ? "background: #16a34a; color: white; padding: 1px 5px; border-radius: 3px; font-size: 9px; font-weight: 600;" : "background: #2563eb; color: white; padding: 1px 5px; border-radius: 3px; font-size: 9px; font-weight: 600;";
|
|
3155
|
+
nameContainer.appendChild(duplicateBadge);
|
|
3156
|
+
if (d.connectionType) {
|
|
3157
|
+
const badge = renderConnectionBadge(d.connectionType);
|
|
3158
|
+
if (badge) {
|
|
3159
|
+
nameContainer.appendChild(badge);
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
3162
|
+
if (d.isIR) {
|
|
3163
|
+
nameContainer.appendChild(renderIRBadge());
|
|
3164
|
+
}
|
|
3165
|
+
if (d.rssi !== void 0 && d.rssi !== null && d.rssi !== 0) {
|
|
3166
|
+
nameContainer.appendChild(renderSignalBars(d.rssi));
|
|
3167
|
+
nameContainer.appendChild(renderSignalQualityBadge(d.rssi));
|
|
3168
|
+
}
|
|
3169
|
+
if (typeof d.battery === "number" && d.battery < 20) {
|
|
3170
|
+
const batteryWarn = document.createElement("span");
|
|
3171
|
+
batteryWarn.textContent = `\u26A0\uFE0F ${d.battery}%`;
|
|
3172
|
+
batteryWarn.style.cssText = d.battery < 10 ? "background: #dc2626; color: white; padding: 1px 6px; border-radius: 3px; font-size: 10px; font-weight: 600; margin-left: 6px;" : "background: #fbbf24; color: #111; padding: 1px 6px; border-radius: 3px; font-size: 10px; font-weight: 600; margin-left: 6px;";
|
|
3173
|
+
batteryWarn.title = d.battery < 10 ? "Battery critically low" : "Battery low";
|
|
3174
|
+
nameContainer.appendChild(batteryWarn);
|
|
3175
|
+
}
|
|
3176
|
+
if (!d || !d.id && !d.deviceId || !d.name && !d.type) {
|
|
3177
|
+
console.warn("[SwitchBot][Discovery][renderDeviceDetailsPanel] Device missing required fields:", d);
|
|
3178
|
+
}
|
|
3179
|
+
const details = document.createElement("div");
|
|
3180
|
+
details.style.fontSize = "10px";
|
|
3181
|
+
details.style.opacity = "0.7";
|
|
3182
|
+
details.style.marginTop = "0";
|
|
3183
|
+
details.style.fontFamily = "monospace";
|
|
3184
|
+
details.style.whiteSpace = "normal";
|
|
3185
|
+
details.style.overflowWrap = "anywhere";
|
|
3186
|
+
details.style.wordBreak = "break-word";
|
|
3187
|
+
let detailsText = `ID: ${d.id} | Type: ${d.type} | Model: ${d.model || "N/A"}`;
|
|
3188
|
+
if (d.hubDeviceId) {
|
|
3189
|
+
detailsText += ` | Hub: ${d.hubDeviceId}`;
|
|
3190
|
+
}
|
|
3191
|
+
if (d.address) {
|
|
3192
|
+
detailsText += ` | MAC: ${d.address}`;
|
|
3193
|
+
}
|
|
3194
|
+
details.textContent = detailsText;
|
|
3195
|
+
info.appendChild(nameContainer);
|
|
3196
|
+
info.appendChild(details);
|
|
3197
|
+
info.appendChild(expandedDetails);
|
|
3198
|
+
const addBtn = document.createElement("button");
|
|
3199
|
+
addBtn.textContent = alreadyAdded ? "Already Added" : "Add to Config";
|
|
3200
|
+
addBtn.style.marginLeft = "0";
|
|
3201
|
+
addBtn.style.marginTop = "2px";
|
|
3202
|
+
addBtn.style.padding = "4px 9px";
|
|
3203
|
+
addBtn.style.fontSize = "11px";
|
|
3204
|
+
addBtn.style.whiteSpace = "nowrap";
|
|
3205
|
+
addBtn.style.flexShrink = "0";
|
|
3206
|
+
addBtn.disabled = alreadyAdded;
|
|
3207
|
+
if (alreadyAdded) {
|
|
3208
|
+
addBtn.style.opacity = "0.65";
|
|
3209
|
+
addBtn.style.cursor = "not-allowed";
|
|
3210
|
+
addBtn.style.background = "#6b7280";
|
|
3211
|
+
}
|
|
3212
|
+
addBtn.onclick = async () => {
|
|
3213
|
+
if (alreadyAdded) {
|
|
3214
|
+
return;
|
|
3215
|
+
}
|
|
3216
|
+
await addDeviceToConfig3(d);
|
|
3217
|
+
};
|
|
3218
|
+
if (alreadyAdded) {
|
|
3219
|
+
const viewBtn = document.createElement("button");
|
|
3220
|
+
viewBtn.textContent = "View in Config";
|
|
3221
|
+
viewBtn.className = "secondary";
|
|
3222
|
+
viewBtn.style.marginLeft = "0";
|
|
3223
|
+
viewBtn.style.padding = "4px 9px";
|
|
3224
|
+
viewBtn.style.fontSize = "11px";
|
|
3225
|
+
viewBtn.onclick = async () => {
|
|
3226
|
+
await loadConfiguredDevices2();
|
|
3227
|
+
scrollToConfiguredDevice(d.id);
|
|
3228
|
+
};
|
|
3229
|
+
li.appendChild(info);
|
|
3230
|
+
const actions2 = document.createElement("div");
|
|
3231
|
+
actions2.className = "device-actions";
|
|
3232
|
+
actions2.style.display = "flex";
|
|
3233
|
+
actions2.style.alignItems = "center";
|
|
3234
|
+
actions2.style.flexWrap = "wrap";
|
|
3235
|
+
actions2.style.justifyContent = "flex-start";
|
|
3236
|
+
actions2.style.marginLeft = "0";
|
|
3237
|
+
actions2.style.width = "100%";
|
|
3238
|
+
actions2.style.marginTop = "2px";
|
|
3239
|
+
actions2.style.gap = "5px";
|
|
3240
|
+
actions2.appendChild(viewBtn);
|
|
3241
|
+
actions2.appendChild(addBtn);
|
|
3242
|
+
actions2.appendChild(createConnectionTestControls(d));
|
|
3243
|
+
li.appendChild(actions2);
|
|
3244
|
+
ul.appendChild(li);
|
|
3245
|
+
continue;
|
|
3246
|
+
}
|
|
3247
|
+
const actions = document.createElement("div");
|
|
3248
|
+
actions.className = "device-actions";
|
|
3249
|
+
actions.style.display = "flex";
|
|
3250
|
+
actions.style.flexWrap = "wrap";
|
|
3251
|
+
actions.style.justifyContent = "flex-start";
|
|
3252
|
+
actions.style.marginLeft = "0";
|
|
3253
|
+
actions.style.width = "100%";
|
|
3254
|
+
actions.style.marginTop = "2px";
|
|
3255
|
+
actions.style.gap = "5px";
|
|
3256
|
+
actions.appendChild(addBtn);
|
|
3257
|
+
actions.appendChild(createConnectionTestControls(d));
|
|
3258
|
+
li.appendChild(info);
|
|
3259
|
+
li.appendChild(actions);
|
|
3260
|
+
ul.appendChild(li);
|
|
3261
|
+
}
|
|
3262
|
+
return ul;
|
|
3263
|
+
}
|
|
3264
|
+
function filterDevices(devices, connectionType = "all", searchQuery = "") {
|
|
3265
|
+
let filtered = [...devices];
|
|
3266
|
+
if (connectionType !== "all") {
|
|
3267
|
+
filtered = filtered.filter((d) => {
|
|
3268
|
+
if (connectionType === "ir") {
|
|
3269
|
+
return d.isIR === true;
|
|
3270
|
+
}
|
|
3271
|
+
if (connectionType === "ble") {
|
|
3272
|
+
return d.connectionType === "BLE" || d.connectionType?.includes("BLE");
|
|
3273
|
+
}
|
|
3274
|
+
if (connectionType === "api") {
|
|
3275
|
+
return d.connectionType === "OpenAPI" || d.connectionType === "API" || d.connectionType?.includes("API");
|
|
3276
|
+
}
|
|
3277
|
+
if (connectionType === "both") {
|
|
3278
|
+
return d.connectionType === "Both" || d.connectionType?.includes("Both");
|
|
3279
|
+
}
|
|
3280
|
+
return true;
|
|
3281
|
+
});
|
|
3282
|
+
}
|
|
3283
|
+
if (searchQuery.trim()) {
|
|
3284
|
+
const query = searchQuery.toLowerCase();
|
|
3285
|
+
filtered = filtered.filter((d) => {
|
|
3286
|
+
const name = (d.name || "").toLowerCase();
|
|
3287
|
+
const id = (d.id || "").toLowerCase();
|
|
3288
|
+
const type = (d.type || "").toLowerCase();
|
|
3289
|
+
const model = (d.model || "").toLowerCase();
|
|
3290
|
+
return name.includes(query) || id.includes(query) || type.includes(query) || model.includes(query);
|
|
3291
|
+
});
|
|
3292
|
+
}
|
|
3293
|
+
return filtered;
|
|
3294
|
+
}
|
|
3295
|
+
function sortDevices(devices, sortBy = "name") {
|
|
3296
|
+
const sorted = [...devices];
|
|
3297
|
+
switch (sortBy) {
|
|
3298
|
+
case "signal": {
|
|
3299
|
+
sorted.sort((a, b) => {
|
|
3300
|
+
const aRssi = a.rssi || 0;
|
|
3301
|
+
const bRssi = b.rssi || 0;
|
|
3302
|
+
return bRssi - aRssi;
|
|
3303
|
+
});
|
|
3304
|
+
break;
|
|
3305
|
+
}
|
|
3306
|
+
case "type": {
|
|
3307
|
+
sorted.sort((a, b) => {
|
|
3308
|
+
const aType = (a.type || "").localeCompare(b.type || "");
|
|
3309
|
+
return aType;
|
|
3310
|
+
});
|
|
3311
|
+
break;
|
|
3312
|
+
}
|
|
3313
|
+
case "connection": {
|
|
3314
|
+
const connectionOrder = {
|
|
3315
|
+
Both: 0,
|
|
3316
|
+
BLE: 1,
|
|
3317
|
+
OpenAPI: 2,
|
|
3318
|
+
API: 2,
|
|
3319
|
+
Unknown: 3
|
|
3320
|
+
};
|
|
3321
|
+
sorted.sort((a, b) => {
|
|
3322
|
+
const aOrder = connectionOrder[a.connectionType || "Unknown"] ?? 3;
|
|
3323
|
+
const bOrder = connectionOrder[b.connectionType || "Unknown"] ?? 3;
|
|
3324
|
+
return aOrder - bOrder;
|
|
3325
|
+
});
|
|
3326
|
+
break;
|
|
3327
|
+
}
|
|
3328
|
+
case "name":
|
|
3329
|
+
default: {
|
|
3330
|
+
sorted.sort((a, b) => (a.name || a.id || "").localeCompare(b.name || b.id || ""));
|
|
3331
|
+
break;
|
|
3332
|
+
}
|
|
3333
|
+
}
|
|
3334
|
+
return sorted;
|
|
3335
|
+
}
|
|
3336
|
+
function getDiscoveryPreferences() {
|
|
3337
|
+
try {
|
|
3338
|
+
const stored = localStorage.getItem("discoveryPreferences");
|
|
3339
|
+
if (stored) {
|
|
3340
|
+
return JSON.parse(stored);
|
|
3341
|
+
}
|
|
3342
|
+
} catch (_e) {
|
|
3343
|
+
}
|
|
3344
|
+
return {
|
|
3345
|
+
connectionType: "all",
|
|
3346
|
+
sortBy: "name",
|
|
3347
|
+
searchQuery: ""
|
|
3348
|
+
};
|
|
3349
|
+
}
|
|
3350
|
+
function setDiscoveryPreferences(preferences) {
|
|
3351
|
+
try {
|
|
3352
|
+
localStorage.setItem("discoveryPreferences", JSON.stringify(preferences));
|
|
3353
|
+
} catch (_e) {
|
|
3354
|
+
}
|
|
3355
|
+
}
|
|
3356
|
+
function renderDeviceList(list) {
|
|
3357
|
+
const ul = document.getElementById("devices");
|
|
3358
|
+
const status = document.getElementById("status");
|
|
3359
|
+
const removeAllContainer = document.getElementById("removeAllContainer");
|
|
3360
|
+
if (!ul || !status) {
|
|
3361
|
+
return;
|
|
3362
|
+
}
|
|
3363
|
+
if (!list.length) {
|
|
3364
|
+
status.textContent = "No devices found in config.";
|
|
3365
|
+
ul.innerHTML = "";
|
|
3366
|
+
if (removeAllContainer) {
|
|
3367
|
+
removeAllContainer.style.display = "none";
|
|
3368
|
+
}
|
|
3369
|
+
return;
|
|
3370
|
+
}
|
|
3371
|
+
status.textContent = `Found ${list.length} device(s)`;
|
|
3372
|
+
ul.classList.add("device-grid");
|
|
3373
|
+
ul.innerHTML = "";
|
|
3374
|
+
if (removeAllContainer) {
|
|
3375
|
+
removeAllContainer.style.display = "block";
|
|
3376
|
+
}
|
|
3377
|
+
for (const d of list) {
|
|
3378
|
+
const li = document.createElement("li");
|
|
3379
|
+
li.className = "device-item";
|
|
3380
|
+
li.setAttribute("data-device-id", normalizeId2(d.id));
|
|
3381
|
+
li.style.display = "flex";
|
|
3382
|
+
li.style.flexDirection = "column";
|
|
3383
|
+
li.style.alignItems = "stretch";
|
|
3384
|
+
li.style.marginBottom = "0";
|
|
3385
|
+
const info = document.createElement("div");
|
|
3386
|
+
info.style.flex = "1 1 auto";
|
|
3387
|
+
info.style.width = "100%";
|
|
3388
|
+
info.style.minWidth = "0";
|
|
3389
|
+
const nameContainer = document.createElement("div");
|
|
3390
|
+
nameContainer.style.display = "flex";
|
|
3391
|
+
nameContainer.style.flexDirection = "column";
|
|
3392
|
+
nameContainer.style.alignItems = "flex-start";
|
|
3393
|
+
nameContainer.style.gap = "0";
|
|
3394
|
+
const name = document.createElement("div");
|
|
3395
|
+
name.style.fontWeight = "500";
|
|
3396
|
+
name.style.fontSize = "13px";
|
|
3397
|
+
name.textContent = d.name || d.id;
|
|
3398
|
+
const expandedDetails = document.createElement("div");
|
|
3399
|
+
expandedDetails.style.display = "none";
|
|
3400
|
+
expandedDetails.appendChild(renderDeviceDetailsPanel(d));
|
|
3401
|
+
const expandBtn = document.createElement("button");
|
|
3402
|
+
expandBtn.textContent = "\u25BE";
|
|
3403
|
+
expandBtn.title = "Show details";
|
|
3404
|
+
expandBtn.style.padding = "2px 6px";
|
|
3405
|
+
expandBtn.style.fontSize = "11px";
|
|
3406
|
+
expandBtn.style.marginLeft = "4px";
|
|
3407
|
+
expandBtn.style.background = "#e5e7eb";
|
|
3408
|
+
expandBtn.style.color = "#111827";
|
|
3409
|
+
expandBtn.style.transition = "transform 0.2s ease";
|
|
3410
|
+
expandBtn.onclick = () => {
|
|
3411
|
+
const isHidden = expandedDetails.style.display === "none";
|
|
3412
|
+
expandedDetails.style.display = isHidden ? "block" : "none";
|
|
3413
|
+
expandBtn.style.transform = isHidden ? "rotate(180deg)" : "rotate(0deg)";
|
|
3414
|
+
};
|
|
3415
|
+
const code = document.createElement("code");
|
|
3416
|
+
code.textContent = d.id;
|
|
3417
|
+
code.style.fontSize = "10px";
|
|
3418
|
+
code.style.opacity = "0.75";
|
|
3419
|
+
code.style.marginLeft = "0";
|
|
3420
|
+
code.style.whiteSpace = "normal";
|
|
3421
|
+
code.style.overflowWrap = "anywhere";
|
|
3422
|
+
code.style.wordBreak = "break-word";
|
|
3423
|
+
code.style.maxWidth = "100%";
|
|
3424
|
+
const headerRow = document.createElement("div");
|
|
3425
|
+
headerRow.style.display = "inline-flex";
|
|
3426
|
+
headerRow.style.alignItems = "center";
|
|
3427
|
+
headerRow.style.gap = "4px";
|
|
3428
|
+
headerRow.appendChild(name);
|
|
3429
|
+
headerRow.appendChild(expandBtn);
|
|
3430
|
+
nameContainer.appendChild(headerRow);
|
|
3431
|
+
nameContainer.appendChild(code);
|
|
3432
|
+
if (d.rssi !== void 0 && d.rssi !== null && d.rssi !== 0) {
|
|
3433
|
+
nameContainer.appendChild(renderSignalBars(d.rssi));
|
|
3434
|
+
nameContainer.appendChild(renderSignalQualityBadge(d.rssi));
|
|
3435
|
+
}
|
|
3436
|
+
const meta = document.createElement("div");
|
|
3437
|
+
meta.style.opacity = "0.75";
|
|
3438
|
+
meta.style.marginTop = "0";
|
|
3439
|
+
meta.style.fontSize = "11px";
|
|
3440
|
+
const typeText = d.type ? `type: ${d.type}` : "";
|
|
3441
|
+
const connText = d.connectionPreference ? `conn: ${d.connectionPreference}` : "";
|
|
3442
|
+
const roomText = d.room ? `room: ${d.room}` : "";
|
|
3443
|
+
meta.textContent = [typeText, connText, roomText].filter(Boolean).join(" | ");
|
|
3444
|
+
info.appendChild(nameContainer);
|
|
3445
|
+
info.appendChild(meta);
|
|
3446
|
+
info.appendChild(expandedDetails);
|
|
3447
|
+
const buttons = document.createElement("div");
|
|
3448
|
+
buttons.className = "device-actions";
|
|
3449
|
+
buttons.style.display = "flex";
|
|
3450
|
+
buttons.style.flexWrap = "wrap";
|
|
3451
|
+
buttons.style.justifyContent = "flex-start";
|
|
3452
|
+
buttons.style.marginLeft = "0";
|
|
3453
|
+
buttons.style.width = "100%";
|
|
3454
|
+
buttons.style.marginTop = "2px";
|
|
3455
|
+
buttons.style.gap = "5px";
|
|
3456
|
+
const editBtn = document.createElement("button");
|
|
3457
|
+
editBtn.textContent = "\u270F\uFE0F Edit";
|
|
3458
|
+
editBtn.style.padding = "4px 9px";
|
|
3459
|
+
editBtn.style.fontSize = "11px";
|
|
3460
|
+
editBtn.onclick = async () => {
|
|
3461
|
+
const { editDevice: editDevice2 } = await Promise.resolve().then(() => (init_modals(), modals_exports));
|
|
3462
|
+
await editDevice2(d);
|
|
3463
|
+
};
|
|
3464
|
+
const copyBtn = document.createElement("button");
|
|
3465
|
+
copyBtn.textContent = "Copy ID";
|
|
3466
|
+
copyBtn.style.padding = "4px 9px";
|
|
3467
|
+
copyBtn.style.fontSize = "11px";
|
|
3468
|
+
copyBtn.addEventListener("click", async () => {
|
|
3469
|
+
try {
|
|
3470
|
+
await navigator.clipboard.writeText(d.id);
|
|
3471
|
+
copyBtn.textContent = "Copied";
|
|
3472
|
+
copyBtn.classList.add("success");
|
|
3473
|
+
setTimeout(() => {
|
|
3474
|
+
copyBtn.textContent = "Copy ID";
|
|
3475
|
+
copyBtn.classList.remove("success");
|
|
3476
|
+
}, 1200);
|
|
3477
|
+
} catch (e) {
|
|
3478
|
+
copyBtn.textContent = "Failed";
|
|
3479
|
+
copyBtn.classList.add("error");
|
|
3480
|
+
setTimeout(() => {
|
|
3481
|
+
copyBtn.textContent = "Copy ID";
|
|
3482
|
+
copyBtn.classList.remove("error");
|
|
3483
|
+
}, 1200);
|
|
3484
|
+
}
|
|
3485
|
+
});
|
|
3486
|
+
const deleteBtn = document.createElement("button");
|
|
3487
|
+
deleteBtn.textContent = "\u{1F5D1}\uFE0F Delete";
|
|
3488
|
+
deleteBtn.style.padding = "4px 9px";
|
|
3489
|
+
deleteBtn.style.fontSize = "11px";
|
|
3490
|
+
deleteBtn.style.background = "#ef4444";
|
|
3491
|
+
deleteBtn.onclick = async () => {
|
|
3492
|
+
const { deleteDeviceFromConfig: deleteDeviceFromConfig2 } = await Promise.resolve().then(() => (init_devices_delete(), devices_delete_exports));
|
|
3493
|
+
await deleteDeviceFromConfig2(d.id || d.deviceId, d.name || d.id || d.deviceId);
|
|
3494
|
+
};
|
|
3495
|
+
buttons.appendChild(editBtn);
|
|
3496
|
+
buttons.appendChild(copyBtn);
|
|
3497
|
+
buttons.appendChild(createConnectionTestControls(d));
|
|
3498
|
+
buttons.appendChild(deleteBtn);
|
|
3499
|
+
li.appendChild(info);
|
|
3500
|
+
li.appendChild(buttons);
|
|
3501
|
+
ul.appendChild(li);
|
|
3502
|
+
}
|
|
3503
|
+
}
|
|
3504
|
+
var SPACES_REGEX, CAMELCASE_REGEX, FIRST_CHAR_REGEX;
|
|
3505
|
+
var init_render = __esm({
|
|
3506
|
+
"src/homebridge-ui/public/js/render.ts"() {
|
|
3507
|
+
"use strict";
|
|
3508
|
+
SPACES_REGEX = /\s/g;
|
|
3509
|
+
CAMELCASE_REGEX = /([A-Z])/g;
|
|
3510
|
+
FIRST_CHAR_REGEX = /^./;
|
|
3511
|
+
}
|
|
3512
|
+
});
|
|
3513
|
+
|
|
3514
|
+
// src/homebridge-ui/public/js/devices.ts
|
|
3515
|
+
var devices_exports = {};
|
|
3516
|
+
__export(devices_exports, {
|
|
3517
|
+
addDeviceToConfig: () => addDeviceToConfig2,
|
|
3518
|
+
initRemoveAllButton: () => initRemoveAllButton,
|
|
3519
|
+
loadConfiguredDevices: () => loadConfiguredDevices
|
|
3520
|
+
});
|
|
3521
|
+
async function addDeviceToConfig2(device, options = {}) {
|
|
3522
|
+
const { refresh = true, showStatus = true } = options;
|
|
3523
|
+
try {
|
|
3524
|
+
const { importDiscoveredDevice: importDiscoveredDevice2 } = await Promise.resolve().then(() => (init_modals(), modals_exports));
|
|
3525
|
+
const importValues = await importDiscoveredDevice2(device);
|
|
3526
|
+
if (!importValues || typeof importValues !== "object" || !importValues.configDeviceName || !importValues.configDeviceType) {
|
|
3527
|
+
return { added: false };
|
|
3528
|
+
}
|
|
3529
|
+
showBusyUi();
|
|
3530
|
+
uiLog.info("Adding device to config:", device);
|
|
3531
|
+
let safeName = importValues.configDeviceName;
|
|
3532
|
+
if (!safeName || safeName === "undefined") {
|
|
3533
|
+
safeName = device.name || device.id;
|
|
3534
|
+
uiLog.warn(`Device name was invalid ("${importValues.configDeviceName}"), using fallback: "${safeName}"`);
|
|
3535
|
+
}
|
|
3536
|
+
const resp = await addDevice(device.id, safeName, importValues.configDeviceType, {
|
|
3537
|
+
address: importValues.address,
|
|
3538
|
+
model: device.model,
|
|
3539
|
+
rssi: device.rssi,
|
|
3540
|
+
encryptionKey: importValues.encryptionKey,
|
|
3541
|
+
keyId: importValues.keyId
|
|
3542
|
+
});
|
|
3543
|
+
uiLog.info("Add device response:", resp);
|
|
3544
|
+
const alreadyExists = !!resp?.alreadyExists;
|
|
3545
|
+
const message = resp?.message || (alreadyExists ? `Device "${importValues.configDeviceName}" already in config` : `Device "${importValues.configDeviceName}" added successfully!`);
|
|
3546
|
+
if (alreadyExists) {
|
|
3547
|
+
toastInfo(message);
|
|
3548
|
+
} else {
|
|
3549
|
+
toastSuccess(message);
|
|
3550
|
+
}
|
|
3551
|
+
if (showStatus) {
|
|
3552
|
+
const status = document.getElementById("discoverStatus");
|
|
3553
|
+
if (status) {
|
|
3554
|
+
status.textContent = (alreadyExists ? "\u2022 " : "\u2713 ") + message;
|
|
3555
|
+
status.classList.remove("error");
|
|
3556
|
+
status.classList.add("success-msg");
|
|
3557
|
+
if (!alreadyExists) {
|
|
3558
|
+
const synced = await syncParentPluginConfigFromDisk(true);
|
|
3559
|
+
status.textContent += synced ? " - Config saved automatically." : " - Warning: config may not persist until you close/reopen settings.";
|
|
3560
|
+
if (synced) {
|
|
3561
|
+
toastSuccess("Configuration synced and saved automatically");
|
|
3562
|
+
} else {
|
|
3563
|
+
toastWarning("Configuration sync failed; close and reopen settings before Save");
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
}
|
|
3568
|
+
if (refresh) {
|
|
3569
|
+
await syncParentPluginConfigFromDisk(true);
|
|
3570
|
+
await loadConfiguredDevices();
|
|
3571
|
+
}
|
|
3572
|
+
return { added: !alreadyExists };
|
|
3573
|
+
} catch (e) {
|
|
3574
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
3575
|
+
uiLog.error("Add device error:", msg);
|
|
3576
|
+
const status = document.getElementById("discoverStatus");
|
|
3577
|
+
if (status) {
|
|
3578
|
+
status.textContent = `\u2717 Error: ${msg}`;
|
|
3579
|
+
status.classList.add("error");
|
|
3580
|
+
}
|
|
3581
|
+
toastError(msg);
|
|
3582
|
+
return { added: false };
|
|
3583
|
+
} finally {
|
|
3584
|
+
hideBusyUi();
|
|
3585
|
+
}
|
|
3586
|
+
}
|
|
3587
|
+
async function initRemoveAllButton() {
|
|
3588
|
+
const removeAllBtn = document.getElementById("removeAllBtn");
|
|
3589
|
+
if (!removeAllBtn) {
|
|
3590
|
+
return;
|
|
3591
|
+
}
|
|
3592
|
+
removeAllBtn.addEventListener("click", async () => {
|
|
3593
|
+
const { deleteAllDevicesFromConfig: deleteAllDevicesFromConfig2 } = await Promise.resolve().then(() => (init_devices_delete(), devices_delete_exports));
|
|
3594
|
+
await deleteAllDevicesFromConfig2();
|
|
3595
|
+
});
|
|
3596
|
+
}
|
|
3597
|
+
async function loadConfiguredDevices() {
|
|
3598
|
+
const list = await fetchDevices();
|
|
3599
|
+
renderDeviceList(list);
|
|
3600
|
+
}
|
|
3601
|
+
var init_devices = __esm({
|
|
3602
|
+
"src/homebridge-ui/public/js/devices.ts"() {
|
|
3603
|
+
"use strict";
|
|
3604
|
+
init_api();
|
|
3605
|
+
init_logger();
|
|
3606
|
+
init_modal();
|
|
3607
|
+
init_render();
|
|
3608
|
+
init_toast();
|
|
3609
|
+
}
|
|
3610
|
+
});
|
|
3611
|
+
|
|
3612
|
+
// src/homebridge-ui/public/js/credentials.ts
|
|
3613
|
+
init_logger();
|
|
3614
|
+
init_modal();
|
|
3615
|
+
init_toast();
|
|
3616
|
+
async function loadCredentialStatus() {
|
|
3617
|
+
try {
|
|
3618
|
+
if (typeof homebridge.getPluginConfig !== "function") {
|
|
3619
|
+
uiLog.error("Homebridge UI API not available");
|
|
3620
|
+
return;
|
|
3621
|
+
}
|
|
3622
|
+
const configArr = await homebridge.getPluginConfig();
|
|
3623
|
+
const config = Array.isArray(configArr) && configArr.length > 0 ? configArr[0] : {};
|
|
3624
|
+
const token = config.openApiToken || "";
|
|
3625
|
+
const secret = config.openApiSecret || "";
|
|
3626
|
+
const tokenStatus = document.getElementById("tokenStatus");
|
|
3627
|
+
const secretStatus = document.getElementById("secretStatus");
|
|
3628
|
+
if (!tokenStatus || !secretStatus) {
|
|
3629
|
+
return;
|
|
3630
|
+
}
|
|
3631
|
+
if (token) {
|
|
3632
|
+
tokenStatus.textContent = `\u2713 Configured (${token.length} characters)`;
|
|
3633
|
+
tokenStatus.classList.add("ok");
|
|
3634
|
+
} else {
|
|
3635
|
+
tokenStatus.textContent = "Not configured";
|
|
3636
|
+
tokenStatus.classList.remove("ok");
|
|
3637
|
+
}
|
|
3638
|
+
if (secret) {
|
|
3639
|
+
secretStatus.textContent = `\u2713 Configured (${secret.length} characters)`;
|
|
3640
|
+
secretStatus.classList.add("ok");
|
|
3641
|
+
} else {
|
|
3642
|
+
secretStatus.textContent = "Not configured";
|
|
3643
|
+
secretStatus.classList.remove("ok");
|
|
3644
|
+
}
|
|
3645
|
+
} catch (e) {
|
|
3646
|
+
uiLog.error("Error loading credentials:", e);
|
|
3647
|
+
}
|
|
3648
|
+
}
|
|
3649
|
+
async function saveCredentials() {
|
|
3650
|
+
const token = document.getElementById("token")?.value;
|
|
3651
|
+
const secret = document.getElementById("secret")?.value;
|
|
3652
|
+
const saveStatus = document.getElementById("saveStatus");
|
|
3653
|
+
const saveBtn = document.getElementById("saveBtn");
|
|
3654
|
+
if (!saveStatus || !saveBtn) {
|
|
3655
|
+
return;
|
|
3656
|
+
}
|
|
3657
|
+
if (!token || !secret) {
|
|
3658
|
+
saveStatus.textContent = "Please enter both token and secret";
|
|
3659
|
+
saveStatus.classList.add("error");
|
|
3660
|
+
toastWarning("Please enter both token and secret");
|
|
3661
|
+
return;
|
|
3662
|
+
}
|
|
3663
|
+
try {
|
|
3664
|
+
showBusyUi();
|
|
3665
|
+
saveBtn.disabled = true;
|
|
3666
|
+
saveBtn.textContent = "Saving...";
|
|
3667
|
+
uiLog.info("Saving credentials...");
|
|
3668
|
+
if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") {
|
|
3669
|
+
throw new TypeError("Homebridge UI API not available");
|
|
3670
|
+
}
|
|
3671
|
+
const configArr = await homebridge.getPluginConfig();
|
|
3672
|
+
if (!Array.isArray(configArr) || configArr.length === 0) {
|
|
3673
|
+
throw new Error("No plugin config found");
|
|
3674
|
+
}
|
|
3675
|
+
const config = configArr[0];
|
|
3676
|
+
config.openApiToken = token;
|
|
3677
|
+
config.openApiSecret = secret;
|
|
3678
|
+
await homebridge.updatePluginConfig([config]);
|
|
3679
|
+
if (typeof homebridge.savePluginConfig === "function") {
|
|
3680
|
+
await homebridge.savePluginConfig();
|
|
3681
|
+
}
|
|
3682
|
+
saveStatus.textContent = `\u2713 Credentials saved successfully`;
|
|
3683
|
+
saveStatus.classList.remove("error");
|
|
3684
|
+
saveStatus.classList.add("success-msg");
|
|
3685
|
+
toastSuccess("Credentials saved successfully");
|
|
3686
|
+
document.getElementById("token").value = "";
|
|
3687
|
+
document.getElementById("secret").value = "";
|
|
3688
|
+
setTimeout(loadCredentialStatus, 500);
|
|
3689
|
+
setTimeout(() => {
|
|
3690
|
+
saveStatus.textContent = "";
|
|
3691
|
+
saveStatus.classList.remove("success-msg");
|
|
3692
|
+
}, 3e3);
|
|
3693
|
+
} catch (e) {
|
|
3694
|
+
uiLog.error("Save error:", e);
|
|
3695
|
+
saveStatus.textContent = `Error: ${e instanceof Error ? e.message : "Failed to save"}`;
|
|
3696
|
+
saveStatus.classList.add("error");
|
|
3697
|
+
toastError(e instanceof Error ? e.message : "Failed to save credentials");
|
|
3698
|
+
} finally {
|
|
3699
|
+
hideBusyUi();
|
|
3700
|
+
saveBtn.disabled = false;
|
|
3701
|
+
saveBtn.textContent = "Save Credentials";
|
|
3702
|
+
}
|
|
3703
|
+
}
|
|
3704
|
+
|
|
3705
|
+
// src/homebridge-ui/public/js/app.ts
|
|
3706
|
+
init_devices();
|
|
3707
|
+
init_discovery();
|
|
3708
|
+
window.loadCredentialStatus = loadCredentialStatus;
|
|
3709
|
+
window.saveCredentials = saveCredentials;
|
|
3710
|
+
window.discoverDevices = discoverDevices2;
|
|
3711
|
+
async function init() {
|
|
3712
|
+
await loadCredentialStatus();
|
|
3713
|
+
await initializeDiscoverySettings();
|
|
3714
|
+
await loadConfiguredDevices();
|
|
3715
|
+
await initRemoveAllButton();
|
|
3716
|
+
}
|
|
3717
|
+
if (document.readyState === "loading") {
|
|
3718
|
+
document.addEventListener("DOMContentLoaded", init);
|
|
3719
|
+
} else {
|
|
3720
|
+
init();
|
|
3721
|
+
}
|
|
3722
|
+
//# sourceMappingURL=app.js.map
|