UNPKG

@switchbot/homebridge-switchbot

Version:

The SwitchBot plugin allows you to access your SwitchBot device(s) from HomeKit.

1,355 lines (1,348 loc) 145 kB
var __defProp = Object.defineProperty; var __getOwnPropNames = Object.getOwnPropertyNames; var __esm = (fn, res) => function __init() { return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/homebridge-ui/public/js/logger.ts var PREFIX, uiLog; var init_logger = __esm({ "src/homebridge-ui/public/js/logger.ts"() { "use strict"; PREFIX = "[SwitchBot UI/html]"; uiLog = { info: (message, ...parameters) => { console.log(PREFIX, message, ...parameters); }, warn: (message, ...parameters) => { console.warn(PREFIX, message, ...parameters); }, error: (message, ...parameters) => { console.error(PREFIX, message, ...parameters); }, debug: (message, ...parameters) => { console.debug(PREFIX, message, ...parameters); } }; } }); // src/homebridge-ui/public/js/types.ts var init_types = __esm({ "src/homebridge-ui/public/js/types.ts"() { "use strict"; } }); // src/homebridge-ui/public/js/modal.ts function callUiMethod(name, ...args) { try { if (typeof homebridge?.[name] === "function") { uiLog.info(`[callUiMethod] Invoking homebridge.${String(name)}()`); const fn = homebridge?.[name]; if (typeof fn === "function") { uiLog.info(`[callUiMethod] Invoking homebridge.${String(name)}()`); fn.apply(homebridge, args); } else { uiLog.warn(`[callUiMethod] homebridge[${String(name)}] is not a function.`); } } else { uiLog.warn(`[callUiMethod] homebridge[${String(name)}] is not a function.`); } } catch (e) { uiLog.warn(`Homebridge UI method ${String(name)} failed:`, e); } } function showBusyUi() { callUiMethod("disableSaveButton"); callUiMethod("showSpinner"); } function hideBusyUi() { callUiMethod("hideSpinner"); callUiMethod("enableSaveButton"); } var init_modal = __esm({ "src/homebridge-ui/public/js/modal.ts"() { "use strict"; init_types(); init_logger(); } }); // src/homebridge-ui/public/js/toast.ts function showToast(method, message, title = "SwitchBot") { try { const hb = typeof window !== "undefined" ? window.homebridge : void 0; const toast = hb && typeof hb.toast === "object" ? hb.toast : void 0; if (toast && typeof toast[method] === "function") { try { toast[method](message, title); return; } catch (err) { uiLog.warn(`Toast ${method} threw:`, err); } } uiLog.info(`[Toast:${method}] ${title} - ${message}`); } catch (e) { uiLog.warn(`Toast ${method} outer error:`, e); uiLog.info(`[Toast:${method}] ${title} - ${message}`); } } function toastSuccess(message, title) { showToast("success", message, title); } function toastError(message, title) { showToast("error", message, title); } function toastWarning(message, title) { showToast("warning", message, title); } function toastInfo(message, title) { showToast("info", message, title); } var init_toast = __esm({ "src/homebridge-ui/public/js/toast.ts"() { "use strict"; init_types(); init_logger(); } }); // src/device-types.js function getValidDeviceTypes() { const validTypes = /* @__PURE__ */ new Set(); for (const category of Object.values(DEVICE_TYPES)) { for (const type of category) { validTypes.add(type); } } return validTypes; } function normalizeDeviceType(deviceType) { if (!deviceType || typeof deviceType !== "string") { return null; } const trimmed = deviceType.trim(); const lowercase = trimmed.toLowerCase(); const validTypes = getValidDeviceTypes(); if (validTypes.has(trimmed)) { return trimmed; } const normalized = DEVICE_TYPE_NORMALIZATION_MAP[lowercase]; if (normalized && validTypes.has(normalized)) { return normalized; } return null; } function isValidDeviceType(deviceType) { if (!deviceType || typeof deviceType !== "string") { return false; } const validTypes = getValidDeviceTypes(); return validTypes.has(deviceType.trim()); } var DEVICE_TYPES, DEVICE_TYPE_NORMALIZATION_MAP; var init_device_types = __esm({ "src/device-types.js"() { "use strict"; DEVICE_TYPES = { "Window Coverings": ["Blind Tilt", "Curtain", "Curtain3", "Roller Shade"], "Locks & Access": [ "Keypad", "Keypad Touch", "Keypad Vision", "Keypad Vision Pro", "Lock Vision Pro", "Lock Lite", "Smart Lock", "Smart Lock Pro", "Smart Lock Ultra", "Video Doorbell" ], "Sensors": ["Contact Sensor", "Motion Sensor", "Presence Sensor", "Water Detector"], "Lighting": [ "Candle Warmer Lamp", "Ceiling Light", "Ceiling Light Pro", "Color Bulb", "Floor Lamp", "RGBIC Neon Rope Light", "RGBIC Neon Wire Rope Light", "RGBICWW Floor Lamp", "RGBICWW Strip Light", "Strip Light", "Strip Light 3" ], "Climate Control": [ "Air Purifier PM2.5", "Air Purifier Table PM2.5", "Air Purifier VOC", "Air Purifier Table VOC", "Battery Circulator Fan", "Circulator Fan", "Humidifier", "Humidifier2", "Meter", "MeterPlus", "Meter Plus", "MeterPro", "Meter Pro", "MeterPro(CO2)", "Meter Pro (CO2)", "Smart Radiator Thermostat", "Standing Circulator Fan", "WoIOSensor" ], "Plugs & Switches": [ "Garage Door Opener", "Plug", "Plug Mini (EU)", "Plug Mini (JP)", "Plug Mini (US)", "Relay Switch 1", "Relay Switch 1PM", "Relay Switch 2PM" ], "Robot Vacuums": [ "K10+", "K10+ Pro", "Robot Vacuum Cleaner K10+ Pro Combo", "Robot Vacuum Cleaner K11+", "Robot Vacuum Cleaner K20 Plus Pro", "Robot Vacuum Cleaner S1", "Robot Vacuum Cleaner S1 Plus", "Robot Vacuum Cleaner S10", "Robot Vacuum Cleaner S20" ], "Hubs": ["AI Hub", "Hub", "Hub 2", "Hub 3", "Hub Mini", "Hub Plus"], "Cameras": [ "Indoor Cam", "Pan/Tilt Cam", "Pan/Tilt Cam 2K", "Pan/Tilt Cam Plus 2K", "Pan/Tilt Cam Plus 3K" ], "IR Devices": [ "Air Conditioner", "Air Purifier", "Camera", "DVD", "Fan", "Light", "Others", "Projector", "Set Top Box", "Speaker", "Streamer", "TV", "Vacuum Cleaner", "Water Heater" ], "Other Devices": ["AI Art Frame", "Bot", "Home Climate Panel", "Remote", "remote with screen"] }; DEVICE_TYPE_NORMALIZATION_MAP = { // --- node-switchbot v4 normalization additions --- "hub mini": "Hub Mini", "hub 3": "Hub 3", "keypad": "Keypad", "plug mini": "Plug Mini (US)", // fallback to US if region not specified "art frame": "AI Art Frame", "rgbicww": "RGBICWW Strip Light", "lock vision": "Lock Vision Pro", // alias for new lock vision "lock pro": "Smart Lock Pro", "lock lite": "Lock Lite", "circulator fan": "Circulator Fan", "smart thermostat radiator": "Smart Radiator Thermostat", "climate panel": "Home Climate Panel", "evaporative humidifier": "Humidifier", // --- end node-switchbot v4 additions --- // Only keep the last occurrence for each key, all values canonical "air purifier pm2.5": "Air Purifier PM2.5", "pan/tilt cam plus 3k": "Pan/Tilt Cam Plus 3K", "remote with screen": "Remote with Screen", "ai hub": "AI Hub", "water detector": "Water Detector", "video doorbell": "Video Doorbell", "smart radiator thermostat": "Smart Radiator Thermostat", "woiosensor": "WoIOSensor", "garage door opener": "Garage Door Opener", "air purifier table pm2.5": "Air Purifier Table PM2.5", "air purifier voc": "Air Purifier VOC", "air purifier table voc": "Air Purifier Table VOC", "plug mini (eu)": "Plug Mini (EU)", // Only last occurrence for each key is kept above. Removed duplicates here. // Climate control conversions "humidifier2": "Humidifier2", "battery circulator fan": "Battery Circulator Fan", "standing circulator fan": "Standing Circulator Fan", // Lock/keypad conversions "smart lock": "Smart Lock", "smart lock pro": "Smart Lock Pro", "smart lock ultra": "Smart Lock Ultra", "keypad touch": "Keypad Touch", "keypad vision": "Keypad Vision", "keypad vision pro": "Keypad Vision Pro", // Light conversions "color bulb": "Color Bulb", "ceiling light": "Ceiling Light", "ceiling light pro": "Ceiling Light Pro", "candle warmer lamp": "Candle Warmer Lamp", "floor lamp": "Floor Lamp", "rgbic neon rope light": "RGBIC Neon Rope Light", "rgbic neon wire rope light": "RGBIC Neon Wire Rope Light", "rgbicww floor lamp": "RGBICWW Floor Lamp", "rgbicww strip light": "RGBICWW Strip Light", "strip light": "Strip Light", "strip light 3": "Strip Light 3", // Vacuum conversions "robot vacuum cleaner s1": "Robot Vacuum Cleaner S1", "robot vacuum cleaner s1 plus": "Robot Vacuum Cleaner S1 Plus", "robot vacuum cleaner s10": "Robot Vacuum Cleaner S10", "robot vacuum cleaner s20": "Robot Vacuum Cleaner S20", "robot vacuum cleaner k10+ pro combo": "Robot Vacuum Cleaner K10+ Pro Combo", "robot vacuum cleaner k11+": "Robot Vacuum Cleaner K11+", "robot vacuum cleaner k20 plus pro": "Robot Vacuum Cleaner K20 Plus Pro", // Exact device type mappings (API format → canonical format) "relay switch 1": "Relay Switch 1", "blind tilt": "Blind Tilt", "roller shade": "Roller Shade", "curtain3": "Curtain3", "hub 2": "Hub 2", "meterplus": "MeterPlus", "meterpro": "MeterPro", "meterpro(co2)": "MeterPro(CO2)", "walletfinder": "WalletFinder", "k10+": "K10+", "k10+ pro (wosweeperminipro)": "K10+ Pro (wosweeperminipro)", // Handle spaced variants from config files (normalize back to canonical type) "meter pro": "Meter Pro", "meter pro (co2)": "Meter Pro (CO2)", "meter plus": "Meter Plus", "relay switch 1 pm": "Relay Switch 1PM", "relay switch 2 pm": "Relay Switch 2PM", "plug mini eu": "Plug Mini (EU)", "plug mini jp": "Plug Mini (JP)", "plug mini us": "Plug Mini (US)", // Migration mappings for invalid/legacy device types "lock vision pro": "Lock Vision Pro", // Valid alias; map to canonical // 'lock vision': 'Keypad Vision', // Invalid type (removed, now alias above) "lock touch": "Keypad Touch", // Invalid type // Additional normalization for new/unknown types from logs "woplugus": "Plug Mini (US)" // Removed duplicate keys below, only last occurrence kept // 'plug mini us': 'plug mini (us)', // duplicate, removed // 'plug us': 'plug mini (us)', // duplicate, removed // 'plug': 'plug', // duplicate, removed // 'air purifier pm2.5': 'air purifier pm2.5', // duplicate, removed // 'rgbic neon wire rope light': 'rgbic neon wire rope light', // duplicate, removed // 'candle warmer lamp': 'candle warmer lamp', // duplicate, removed // 'pan/tilt cam plus 3k': 'pan/tilt cam plus 3k', // duplicate, removed // 'remote with screen': 'remote with screen', // duplicate, removed // 'ai hub': 'ai hub', // duplicate, removed // 'lock vision pro': 'lock vision pro', // duplicate, remove this line // Add any other device types from logs as needed }; } }); // src/homebridge-ui/public/js/api.ts var api_exports = {}; __export(api_exports, { addDevice: () => addDevice, addDevicesInBulk: () => addDevicesInBulk, deleteAllDevices: () => deleteAllDevices, deleteDevice: () => deleteDevice, discoverDevices: () => discoverDevices, fetchBluetoothStatus: () => fetchBluetoothStatus, fetchCredentialStatus: () => fetchCredentialStatus, fetchDevices: () => fetchDevices, normalizeBulkAddDevicesResponse: () => normalizeBulkAddDevicesResponse, saveCredentials: () => saveCredentials2, syncParentPluginConfigFromDisk: () => syncParentPluginConfigFromDisk, testDeviceConnection: () => testDeviceConnection, updateDevice: () => updateDevice, validateAndFixDeviceTypes: () => validateAndFixDeviceTypes }); async function fetchDevices() { try { if (typeof homebridge.getPluginConfig !== "function") { throw new TypeError("Homebridge UI API not available"); } const configArr = await homebridge.getPluginConfig(); const config = Array.isArray(configArr) && configArr.length > 0 ? configArr.find(isSwitchBotPlatformConfig) : null; if (!config || !Array.isArray(config.devices)) { return []; } return config.devices; } catch (e) { const msg = e instanceof Error ? e.message : String(e); uiLog.error("Error fetching devices:", msg); return []; } } function validateAndFixDeviceTypes(devices) { const errors = []; for (const d of devices) { if (!isValidDeviceType(d.configDeviceType)) { const fixed = normalizeDeviceType(d.configDeviceType); if (fixed) { d.configDeviceType = fixed; } else { errors.push({ deviceId: d.deviceId, name: d.configDeviceName, type: d.configDeviceType }); } } } return errors; } function isSwitchBotPlatformConfig(block) { const platformName = String(block?.platform || block?.name || "").toLowerCase(); return platformName === "switchbot" || platformName === "@switchbot/homebridge-switchbot" || platformName.includes("switchbot"); } async function syncParentPluginConfigFromDisk(autoSave = false) { try { if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") { uiLog.warn("Parent config sync API not available"); return false; } const pluginConfigBlocks = await homebridge.getPluginConfig(); if (!Array.isArray(pluginConfigBlocks) || !pluginConfigBlocks.length) { uiLog.warn("No plugin config blocks returned from Homebridge"); return false; } const index = pluginConfigBlocks.findIndex((block) => isSwitchBotPlatformConfig(block)); if (index < 0) { uiLog.warn("SwitchBot platform block not found in Homebridge plugin config"); return false; } const errors = validateAndFixDeviceTypes(pluginConfigBlocks[index].devices || []); if (errors.length > 0) { toastError(`Invalid device types found: ${errors.map((e) => `${e.name} (${e.type})`).join(", ")}`); return false; } await homebridge.updatePluginConfig(pluginConfigBlocks); if (autoSave && typeof homebridge.savePluginConfig === "function") { uiLog.info("Auto-saving config to disk..."); await homebridge.savePluginConfig(); uiLog.info("Config saved successfully"); } return true; } catch (e) { uiLog.warn("Failed to sync parent plugin config cache:", e); return false; } } async function fetchCredentialStatus() { try { const resp = await homebridge.request("/credentials", {}); uiLog.info("Load credentials response:", resp); if (!resp || resp.success === false) { uiLog.error("Failed to load credentials:", resp); return null; } return resp.data || {}; } catch (e) { uiLog.error("Error loading credentials:", e); return null; } } async function saveCredentials2(token, secret) { uiLog.info("Saving credentials..."); const resp = await homebridge.request("/credentials", { token, secret }); uiLog.info("Save response:", resp); if (!resp || resp.success === false) { throw new Error(resp?.message || "Save failed"); } return resp.data || resp; } async function discoverDevices(mode = "all", options) { const resp = await homebridge.request("/discover", { mode, ...options }); uiLog.info("Discover response:", resp); if (!resp || resp.success === false) { throw new Error(resp?.data?.message || "Discovery failed"); } return resp.data || []; } async function fetchBluetoothStatus() { try { const resp = await homebridge.request("/ble-status", {}); if (!resp || resp.success === false) { return { available: false, message: "Bluetooth status unavailable" }; } return resp.data || { available: false, message: "Bluetooth status unavailable" }; } catch (_e) { return { available: false, message: "Bluetooth status unavailable" }; } } async function testDeviceConnection(payload) { const resp = await homebridge.request("/test-connection", payload); if (!resp || resp.success === false) { throw new Error(resp?.data?.message || "Connection test failed"); } return resp.data || { success: false, deviceId: payload.deviceId, method: "Auto", latencyMs: 0, message: "Connection test failed" }; } async function addDevice(deviceId, name, type, options) { if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") { throw new TypeError("Homebridge UI API not available"); } const configArr = await homebridge.getPluginConfig(); const idx = Array.isArray(configArr) ? configArr.findIndex(isSwitchBotPlatformConfig) : -1; if (idx === -1) { throw new Error("SwitchBot config not found"); } const config = configArr[idx]; if (!Array.isArray(config.devices)) { config.devices = []; } const normalizedDeviceId = String(deviceId).trim().toLowerCase(); const exists = config.devices.some((d) => String(d.deviceId ?? d.id ?? "").trim().toLowerCase() === normalizedDeviceId); if (exists) { return { alreadyExists: true, message: "Device already in config" }; } const newDevice = { deviceId, configDeviceName: name, configDeviceType: type }; if (options?.address) { newDevice.address = options.address; } if (options?.model) { newDevice.model = options.model; } if (options?.rssi !== void 0 && options?.rssi !== null && options?.rssi !== 0) { newDevice.rssi = options.rssi; } if (options?.encryptionKey) { newDevice.encryptionKey = options.encryptionKey; } if (options?.keyId) { newDevice.keyId = options.keyId; } config.devices.push(newDevice); await homebridge.updatePluginConfig(configArr); if (typeof homebridge.savePluginConfig === "function") { await homebridge.savePluginConfig(); } return { added: true, message: `Device "${name}" added successfully` }; } async function addDevicesInBulk(devices) { const resp = await homebridge.request("/add-devices", { devices }); uiLog.info("Bulk add response:", resp); if (!resp || resp.success === false) { throw new Error(resp?.data?.message || "Bulk add failed"); } return normalizeBulkAddDevicesResponse(resp); } function normalizeBulkAddDevicesResponse(resp) { const payload = resp?.data && typeof resp.data === "object" ? resp.data : resp; const addedCount = Number(payload?.addedCount ?? payload?.added ?? 0); const skippedCount = Number(payload?.skippedCount ?? payload?.skipped ?? 0); const updatedCount = Number(payload?.updatedCount ?? payload?.updated ?? 0); return { ...payload, success: resp?.success ?? true, addedCount, skippedCount, updatedCount }; } async function updateDevice(deviceId, configDeviceName, configDeviceType, options) { if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") { throw new TypeError("Homebridge UI API not available"); } const configArr = await homebridge.getPluginConfig(); const idx = Array.isArray(configArr) ? configArr.findIndex(isSwitchBotPlatformConfig) : -1; if (idx === -1) { throw new Error("SwitchBot config not found"); } const config = configArr[idx]; if (!Array.isArray(config.devices)) { throw new TypeError("No devices array in config"); } const normalizedDeviceId = String(deviceId).trim().toLowerCase(); const device = config.devices.find((d) => String(d.deviceId ?? d.id ?? "").trim().toLowerCase() === normalizedDeviceId); if (!device) { throw new Error("Device not found in config"); } if (configDeviceName) { device.configDeviceName = configDeviceName; } if (configDeviceType) { device.configDeviceType = configDeviceType; } if (options) { Object.assign(device, options); } await homebridge.updatePluginConfig(configArr); if (typeof homebridge.savePluginConfig === "function") { await homebridge.savePluginConfig(); } return { updated: true, message: `Device updated successfully` }; } async function deleteDevice(deviceId) { if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") { throw new TypeError("Homebridge UI API not available"); } const configArr = await homebridge.getPluginConfig(); const idx = Array.isArray(configArr) ? configArr.findIndex(isSwitchBotPlatformConfig) : -1; if (idx === -1) { throw new Error("SwitchBot config not found"); } const config = configArr[idx]; if (!Array.isArray(config.devices)) { throw new TypeError("No devices array in config"); } const normalizedDeviceId = String(deviceId).trim().toLowerCase(); const before = config.devices.length; config.devices = config.devices.filter((d) => String(d.deviceId ?? d.id ?? "").trim().toLowerCase() !== normalizedDeviceId); config.devices = config.devices.filter((d) => d && typeof d === "object" && d.deviceId && d.configDeviceType); if (config.devices.length === before) { throw new Error("Device not found in config"); } await homebridge.updatePluginConfig(configArr); if (typeof homebridge.savePluginConfig === "function") { await homebridge.savePluginConfig(); } return { deleted: true, message: `Device removed from config` }; } async function deleteAllDevices() { if (typeof homebridge.getPluginConfig !== "function" || typeof homebridge.updatePluginConfig !== "function") { throw new TypeError("Homebridge UI API not available"); } const configArr = await homebridge.getPluginConfig(); let idx = Array.isArray(configArr) ? configArr.findIndex(isSwitchBotPlatformConfig) : -1; if (idx === -1) { const newBlock = { platform: "SwitchBot", devices: [] }; configArr.push(newBlock); idx = configArr.length - 1; } const config = configArr[idx]; if (!Array.isArray(config.devices)) { config.devices = []; } const deletedCount = config.devices.length; config.devices = []; if (!config.platform) { config.platform = "SwitchBot"; } if (!config.name) { config.name = "SwitchBot"; } await homebridge.updatePluginConfig(configArr); if (typeof homebridge.savePluginConfig === "function") { await homebridge.savePluginConfig(); } return { deleted: true, deletedCount, message: `Removed ${deletedCount} device(s) from config` }; } var init_api = __esm({ "src/homebridge-ui/public/js/api.ts"() { "use strict"; init_device_types(); init_types(); init_logger(); init_toast(); } }); // src/homebridge-ui/public/js/discovery.ts var discovery_exports = {}; __export(discovery_exports, { addDeviceToConfig: () => addDeviceToConfig, discoverDevices: () => discoverDevices2, initializeDiscoverySettings: () => initializeDiscoverySettings }); function normalizeId(value) { return String(value ?? "").trim().toLowerCase(); } function dedupeById(devices) { return devices.filter((d, index, arr) => !!d?.id && arr.findIndex((x) => x?.id === d.id) === index); } function mergeDiscoveredDevices(existingDevices, incomingDevices) { const deviceMap = /* @__PURE__ */ new Map(); for (const d of dedupeById(existingDevices)) { deviceMap.set(d.id, { ...d }); } for (const d of dedupeById(incomingDevices)) { const current = deviceMap.get(d.id); if (current) { let nextConnectionType = current.connectionType; if (current.connectionType && d.connectionType && current.connectionType !== d.connectionType) { const types = [current.connectionType, d.connectionType].sort().join(","); if (types === "BLE,OpenAPI" || types === "OpenAPI,BLE") { nextConnectionType = "Both"; } } deviceMap.set(d.id, { ...current, ...d, connectionType: nextConnectionType }); } else { deviceMap.set(d.id, { ...d }); } } const merged = [...deviceMap.values()]; if (merged.length > 0) { console.warn("[SwitchBot][Discovery][mergeDiscoveredDevices] Merged device sample:", merged[0]); console.warn("[SwitchBot][Discovery][mergeDiscoveredDevices] Total merged devices:", merged.length); } return merged; } function setDiscoveryCache(devices) { try { const payload = { timestamp: Date.now(), devices }; localStorage.setItem(DISCOVERY_CACHE_KEY, JSON.stringify(payload)); } catch (_e) { } } function clearDiscoveryCache() { try { localStorage.removeItem(DISCOVERY_CACHE_KEY); } catch (_e) { } } function getDiscoveryCache(validOnly = true) { try { const stored = localStorage.getItem(DISCOVERY_CACHE_KEY); if (!stored) { return null; } const payload = JSON.parse(stored); if (!payload || !Array.isArray(payload.devices) || typeof payload.timestamp !== "number") { return null; } const age = Date.now() - payload.timestamp; if (validOnly && age > DISCOVERY_CACHE_TTL_MS) { return null; } return payload; } catch (_e) { return null; } } function getDiscoveryAutoRefreshSeconds() { try { const stored = localStorage.getItem(DISCOVERY_AUTO_REFRESH_KEY); const value = Number(stored || 0); return Number.isFinite(value) && value >= 0 ? value : 0; } catch (_e) { return 0; } } function setDiscoveryAutoRefreshSeconds(value) { try { localStorage.setItem(DISCOVERY_AUTO_REFRESH_KEY, String(Math.max(0, value))); } catch (_e) { } } function getDiscoveryHideAddedPreference() { try { return localStorage.getItem(DISCOVERY_HIDE_ADDED_KEY) === "true"; } catch (_e) { return false; } } function setDiscoveryHideAddedPreference(value) { try { localStorage.setItem(DISCOVERY_HIDE_ADDED_KEY, String(value)); } catch (_e) { } } function formatElapsedShort(ms) { const totalSeconds = Math.max(0, Math.floor(ms / 1e3)); if (totalSeconds < 60) { return `${totalSeconds}s ago`; } const minutes = Math.floor(totalSeconds / 60); if (minutes < 60) { return `${minutes}m ago`; } const hours = Math.floor(minutes / 60); if (hours < 24) { return `${hours}h ago`; } const days = Math.floor(hours / 24); return `${days}d ago`; } function updateLastScannedStatus() { const lastScannedStatus = document.getElementById("lastScannedStatus"); if (!lastScannedStatus) { return; } const cache = getDiscoveryCache(false); if (!cache) { lastScannedStatus.textContent = "Last scanned: never"; return; } const ageMs = Date.now() - cache.timestamp; const timestampText = new Date(cache.timestamp).toLocaleString(); const stale = ageMs > DISCOVERY_CACHE_TTL_MS; lastScannedStatus.textContent = stale ? `Last scanned: ${formatElapsedShort(ageMs)} (${timestampText}, cache expired)` : `Last scanned: ${formatElapsedShort(ageMs)} (${timestampText})`; } async function renderCachedDiscoveryResults() { const cache = getDiscoveryCache(true); const list = document.getElementById("discoveredList"); if (!cache || !list || !cache.devices.length) { return; } list.style.display = "block"; if (!window._discoverySelectedIds) { window._discoverySelectedIds = /* @__PURE__ */ new Set(); } await updateDiscoveryView( cache.devices, getDiscoveryPreferences(), getDiscoveryGroupByPreference(), getDiscoveryHideAddedPreference(), window._discoverySelectedIds ); } function getDiscoveryBleSettings() { try { const stored = localStorage.getItem(DISCOVERY_BLE_SETTINGS_KEY); if (!stored) { return { bleEnabled: true, bleScanDurationSeconds: 5, bleTimeoutSeconds: 8 }; } const parsed = JSON.parse(stored); return { bleEnabled: parsed?.bleEnabled !== false, bleScanDurationSeconds: Math.max(3, Math.min(15, Number(parsed?.bleScanDurationSeconds || 5))), bleTimeoutSeconds: Math.max(3, Math.min(30, Number(parsed?.bleTimeoutSeconds || 8))) }; } catch (_e) { return { bleEnabled: true, bleScanDurationSeconds: 5, bleTimeoutSeconds: 8 }; } } function setDiscoveryBleSettings(settings) { try { localStorage.setItem(DISCOVERY_BLE_SETTINGS_KEY, JSON.stringify(settings)); } catch (_e) { } } async function initializeDiscoverySettings() { const scanSelect = document.getElementById("bleScanDurationSelect"); const timeoutInput = document.getElementById("bleTimeoutInput"); const disableBleCheckbox = document.getElementById("disableBleScanCheckbox"); const scanSetting = document.getElementById("bleScanSetting"); const timeoutSetting = document.getElementById("bleTimeoutSetting"); const bluetoothStatus = document.getElementById("bluetoothStatus"); const autoRefreshSelect = document.getElementById("autoRefreshIntervalSelect"); const refreshBtn = document.getElementById("refreshDiscoverBtn"); const current = getDiscoveryBleSettings(); if (scanSelect) { scanSelect.value = String(current.bleScanDurationSeconds); } if (timeoutInput) { timeoutInput.value = String(current.bleTimeoutSeconds); } if (disableBleCheckbox) { disableBleCheckbox.checked = !current.bleEnabled; } if (autoRefreshSelect) { autoRefreshSelect.value = String(getDiscoveryAutoRefreshSeconds()); } const updateBleSettingVisibility = () => { const disabled = !!disableBleCheckbox?.checked; if (scanSetting) { scanSetting.style.display = disabled ? "none" : "inline-flex"; } if (timeoutSetting) { timeoutSetting.style.display = disabled ? "none" : "inline-flex"; } }; const persistFromControls = () => { const next = { bleEnabled: !(disableBleCheckbox?.checked ?? false), bleScanDurationSeconds: Math.max(3, Math.min(15, Number(scanSelect?.value || 5))), bleTimeoutSeconds: Math.max(3, Math.min(30, Number(timeoutInput?.value || 8))) }; setDiscoveryBleSettings(next); }; scanSelect?.addEventListener("change", persistFromControls); timeoutInput?.addEventListener("change", persistFromControls); disableBleCheckbox?.addEventListener("change", () => { persistFromControls(); updateBleSettingVisibility(); }); updateBleSettingVisibility(); if (bluetoothStatus) { const status = await fetchBluetoothStatus(); bluetoothStatus.textContent = status.available ? `Bluetooth: available (${status.message})` : `Bluetooth: unavailable (${status.message})`; } updateLastScannedStatus(); if (discoveryLastScannedTimer) { clearInterval(discoveryLastScannedTimer); } discoveryLastScannedTimer = setInterval(updateLastScannedStatus, 15e3); refreshBtn?.addEventListener("click", () => { void discoverDevices2(); }); const applyAutoRefresh = () => { const seconds = Math.max(0, Number(autoRefreshSelect?.value || 0)); setDiscoveryAutoRefreshSeconds(seconds); if (discoveryAutoRefreshTimer) { clearInterval(discoveryAutoRefreshTimer); discoveryAutoRefreshTimer = null; } if (seconds > 0) { discoveryAutoRefreshTimer = setInterval(() => { const discoverBtn = document.getElementById("discoverBtn"); if (!discoverBtn || discoverBtn.disabled || document.hidden) { return; } void discoverDevices2(); }, seconds * 1e3); } }; autoRefreshSelect?.addEventListener("change", applyAutoRefresh); applyAutoRefresh(); await renderCachedDiscoveryResults(); } function getDiscoveryGroupByPreference() { try { const stored = localStorage.getItem(DISCOVERY_GROUP_BY_KEY); if (stored === "hub" || stored === "type") { return stored; } return "type"; } catch (_e) { return "type"; } } function setDiscoveryGroupByPreference(groupBy) { try { localStorage.setItem(DISCOVERY_GROUP_BY_KEY, groupBy); } catch (_e) { } } function getDiscoveryGroupExpandedState() { try { const stored = localStorage.getItem(DISCOVERY_GROUP_EXPANDED_KEY); if (!stored) { return {}; } const parsed = JSON.parse(stored); return typeof parsed === "object" && parsed ? parsed : {}; } catch (_e) { return {}; } } function setDiscoveryGroupExpandedState(state) { try { localStorage.setItem(DISCOVERY_GROUP_EXPANDED_KEY, JSON.stringify(state)); } catch (_e) { } } function isDiscoveryGroupExpanded(groupKey) { const state = getDiscoveryGroupExpandedState(); return state[groupKey] !== false; } function setDiscoveryGroupExpanded(groupKey, expanded) { const state = getDiscoveryGroupExpandedState(); state[groupKey] = expanded; setDiscoveryGroupExpandedState(state); } async function discoverDevices2() { const btn = document.getElementById("discoverBtn"); const status = document.getElementById("discoverStatus"); const phaseProgress = document.getElementById("discoverPhaseProgress"); const phaseFill = document.getElementById("discoverPhaseFill"); const phaseLabel = document.getElementById("discoverPhaseLabel"); const list = document.getElementById("discoveredList"); const autoAddAll = document.getElementById("autoAddAllCheckbox")?.checked; const scanSelect = document.getElementById("bleScanDurationSelect"); const timeoutInput = document.getElementById("bleTimeoutInput"); const disableBleCheckbox = document.getElementById("disableBleScanCheckbox"); if (!btn) { console.error("[SwitchBot][Discovery] discoverDevices: discoverBtn not found in DOM"); return; } if (!status) { console.error("[SwitchBot][Discovery] discoverDevices: discoverStatus not found in DOM"); return; } if (!list) { console.error("[SwitchBot][Discovery] discoverDevices: discoveredList container not found in DOM"); toastError("Discovery UI error: device list container missing. Please reload the page."); return; } const spinnerFrames = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"]; let spinnerIndex = 0; const startedAt = Date.now(); let phaseStartedAt = startedAt; let phase = "Preparing discovery..."; const setPhase = (nextPhase) => { phase = nextPhase; phaseStartedAt = Date.now(); }; const getPhasePercent = (phaseName) => { if (phaseName.includes("Scanning BLE")) { return 35; } if (phaseName.includes("Fetching OpenAPI")) { return 75; } if (phaseName.includes("Complete")) { return 100; } return 10; }; const renderProgress = () => { const totalSeconds = Math.max(0, Math.floor((Date.now() - startedAt) / 1e3)); const phaseSeconds = Math.max(0, Math.floor((Date.now() - phaseStartedAt) / 1e3)); const frame = spinnerFrames[spinnerIndex % spinnerFrames.length]; spinnerIndex += 1; status.textContent = `${frame} ${phase} (${phaseSeconds}s, ${totalSeconds}s total)`; if (phaseProgress) { phaseProgress.style.display = "block"; } if (phaseFill) { phaseFill.style.width = `${getPhasePercent(phase)}%`; } if (phaseLabel) { phaseLabel.textContent = phase; } }; const progressTimer = setInterval(renderProgress, 250); let discoveredDevices = []; const preferences = getDiscoveryPreferences(); let groupBy = getDiscoveryGroupByPreference(); let hideAdded = getDiscoveryHideAddedPreference(); if (!window._discoverySelectedIds) { window._discoverySelectedIds = /* @__PURE__ */ new Set(); } const selectedIds = window._discoverySelectedIds; let controlsInitialized = false; async function batchSetDeviceEnabled(selectedIds2, enabled) { if (typeof homebridge.getPluginConfig !== "function") { throw new TypeError("homebridge.getPluginConfig is not available"); } const configArr = await homebridge.getPluginConfig(); const platformIdx = Array.isArray(configArr) ? configArr.findIndex((c) => (c.platform || c.name || "").toLowerCase().includes("switchbot")) : -1; if (platformIdx === -1) { throw new Error("SwitchBot platform config not found"); } const platformConfig = configArr[platformIdx]; if (!Array.isArray(platformConfig.devices)) { throw new TypeError("No devices array in config"); } let changed = false; for (const dev of platformConfig.devices) { const id = String(dev.deviceId || dev.id || "").trim().toLowerCase(); if (selectedIds2.has(id)) { if (dev.enabled !== enabled) { dev.enabled = enabled; changed = true; } } } if (changed) { if (typeof homebridge.updatePluginConfig === "function") { await homebridge.updatePluginConfig(configArr); } else { throw new TypeError("homebridge.updatePluginConfig is not available"); } if (typeof homebridge.savePluginConfig === "function") { await homebridge.savePluginConfig(); } } } const ensureDiscoveryControls = async () => { const selectAllBtn = document.createElement("button"); selectAllBtn.textContent = "Select All"; selectAllBtn.style.fontSize = "13px"; selectAllBtn.style.padding = "6px 18px"; selectAllBtn.style.borderRadius = "6px"; selectAllBtn.style.background = "#f3f4f6"; selectAllBtn.style.color = "#1d4ed8"; selectAllBtn.style.border = "1px solid #d1d5db"; selectAllBtn.style.cursor = "pointer"; selectAllBtn.style.marginRight = "8px"; selectAllBtn.onclick = () => { for (const d of discoveredDevices) { selectedIds.add(normalizeId(d.id)); } window.dispatchEvent(new Event("discovery-selection-changed")); void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds); }; const deselectAllBtn = document.createElement("button"); deselectAllBtn.textContent = "Deselect All"; deselectAllBtn.style.fontSize = "13px"; deselectAllBtn.style.padding = "6px 18px"; deselectAllBtn.style.borderRadius = "6px"; deselectAllBtn.style.background = "#f3f4f6"; deselectAllBtn.style.color = "#ef4444"; deselectAllBtn.style.border = "1px solid #d1d5db"; deselectAllBtn.style.cursor = "pointer"; deselectAllBtn.onclick = () => { for (const d of discoveredDevices) { selectedIds.delete(normalizeId(d.id)); } window.dispatchEvent(new Event("discovery-selection-changed")); void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds); }; const selectControlsRow = document.createElement("div"); selectControlsRow.style.display = "flex"; selectControlsRow.style.gap = "10px"; selectControlsRow.style.margin = "0 0 10px 0"; selectControlsRow.appendChild(selectAllBtn); selectControlsRow.appendChild(deselectAllBtn); if (controlsInitialized) { return; } const controlsDiv = document.createElement("div"); controlsDiv.style.cssText = "margin-bottom: 12px; display: flex; gap: 12px; flex-wrap: wrap; align-items: center;"; const filterLabel = document.createElement("label"); filterLabel.style.fontSize = "12px"; filterLabel.style.fontWeight = "500"; filterLabel.textContent = "Filter:"; const filterGroup = document.createElement("div"); filterGroup.style.display = "flex"; filterGroup.style.gap = "4px"; const filterOptions = [ { label: "All", value: "all" }, { label: "BLE", value: "ble" }, { label: "API", value: "api" }, { label: "Both", value: "both" }, { label: "IR", value: "ir" } ]; for (const option of filterOptions) { const filterBtn = document.createElement("button"); filterBtn.textContent = option.label; filterBtn.style.padding = "4px 8px"; filterBtn.style.fontSize = "11px"; filterBtn.style.borderRadius = "3px"; filterBtn.style.cursor = "pointer"; filterBtn.style.border = preferences.connectionType === option.value ? "2px solid #007AFF" : "1px solid #ccc"; filterBtn.style.backgroundColor = preferences.connectionType === option.value ? "#f0f7ff" : "#fff"; filterBtn.style.color = preferences.connectionType === option.value ? "#1d4ed8" : "#374151"; filterBtn.onclick = () => { preferences.connectionType = option.value; setDiscoveryPreferences(preferences); void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds); Array.prototype.forEach.call(filterGroup.querySelectorAll("button"), (b) => { b.style.border = "1px solid #ccc"; b.style.backgroundColor = "#fff"; b.style.color = "#374151"; }); filterBtn.style.border = "2px solid #007AFF"; filterBtn.style.backgroundColor = "#f0f7ff"; filterBtn.style.color = "#1d4ed8"; }; filterGroup.appendChild(filterBtn); } const sortLabel = document.createElement("label"); sortLabel.style.fontSize = "12px"; sortLabel.style.fontWeight = "500"; sortLabel.style.marginLeft = "8px"; sortLabel.textContent = "Sort:"; const sortSelect = document.createElement("select"); sortSelect.style.fontSize = "11px"; sortSelect.style.padding = "4px 8px"; sortSelect.style.borderRadius = "3px"; sortSelect.value = preferences.sortBy; const sortOptions = [ { label: "Name", value: "name" }, { label: "Signal Strength", value: "signal" }, { label: "Type", value: "type" }, { label: "Connection", value: "connection" } ]; for (const opt of sortOptions) { const sortOption = document.createElement("option"); sortOption.value = opt.value; sortOption.textContent = opt.label; sortSelect.appendChild(sortOption); } sortSelect.onchange = () => { preferences.sortBy = sortSelect.value; setDiscoveryPreferences(preferences); void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds); }; const groupSelect = document.createElement("select"); groupSelect.style.fontSize = "11px"; groupSelect.style.padding = "4px 8px"; groupSelect.style.borderRadius = "3px"; if (!localStorage.getItem(DISCOVERY_GROUP_BY_KEY)) { groupSelect.value = "type"; } else { groupSelect.value = groupBy; } const groupLabel = document.createElement("label"); groupLabel.style.fontSize = "12px"; groupLabel.style.fontWeight = "500"; groupLabel.style.marginLeft = "8px"; const groupLabelTextMap = { connection: "Connection", hub: "Hub", type: "Device Type" }; groupLabel.textContent = `Group: ${groupLabelTextMap[groupSelect.value] || "Connection"}`; const groupOptions = [ { label: "Connection", value: "connection" }, { label: "Hub", value: "hub" }, { label: "Device Type", value: "type" } ]; for (const opt of groupOptions) { const groupOption = document.createElement("option"); groupOption.value = opt.value; groupOption.textContent = opt.label; groupSelect.appendChild(groupOption); } groupSelect.onchange = () => { groupBy = groupSelect.value; setDiscoveryGroupByPreference(groupBy); groupLabel.textContent = `Group: ${groupLabelTextMap[groupSelect.value] || "Connection"}`; void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds); }; const hideAddedLabel = document.createElement("label"); hideAddedLabel.style.display = "inline-flex"; hideAddedLabel.style.alignItems = "center"; hideAddedLabel.style.gap = "4px"; hideAddedLabel.style.fontSize = "11px"; hideAddedLabel.style.marginLeft = "8px"; const hideAddedCheckbox = document.createElement("input"); hideAddedCheckbox.type = "checkbox"; hideAddedCheckbox.checked = hideAdded; hideAddedCheckbox.style.margin = "0"; hideAddedCheckbox.style.width = "auto"; const hideAddedText = document.createElement("span"); hideAddedText.textContent = "Hide Added"; hideAddedLabel.appendChild(hideAddedCheckbox); hideAddedLabel.appendChild(hideAddedText); hideAddedCheckbox.onchange = () => { hideAdded = hideAddedCheckbox.checked; setDiscoveryHideAddedPreference(hideAdded); void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds); }; const searchInput = document.createElement("input"); searchInput.type = "text"; searchInput.placeholder = "Search by name, ID, or type..."; searchInput.style.fontSize = "13px"; searchInput.style.padding = "8px 16px"; searchInput.style.borderRadius = "6px"; searchInput.style.border = "1px solid #ccc"; searchInput.style.flex = "1 1 0%"; searchInput.style.minWidth = "120px"; searchInput.style.maxWidth = "100%"; searchInput.style.width = "100%"; searchInput.value = preferences.searchQuery; let searchTimeout; searchInput.oninput = () => { clearTimeout(searchTimeout); searchTimeout = setTimeout(() => { preferences.searchQuery = searchInput.value; setDiscoveryPreferences(preferences); void updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds); }, 300); }; const actionBtnStyle = { fontSize: "16px", padding: "10px 0", borderRadius: "10px", margin: "0 12px 0 0", width: "100%", maxWidth: "220px", fontWeight: "bold", background: "#ef4444", color: "#fff", border: "none", cursor: "pointer", boxShadow: "0 2px 8px #0001", transition: "background 0.2s", outline: "none", display: "block" }; const addSelectedBtn = document.createElement("button"); addSelectedBtn.textContent = "Add Selected to Config"; Object.assign(addSelectedBtn.style, actionBtnStyle); addSelectedBtn.disabled = true; addSelectedBtn.onclick = async () => { if (!selectedIds.size) { return; } addSelectedBtn.disabled = true; addSelectedBtn.textContent = "Adding..."; try { showBusyUi(); const selectedDevices = discoveredDevices.filter((d) => selectedIds.has(normalizeId(d.id))); const bulkResult = await addDevicesInBulk(selectedDevices.map((d) => ({ deviceId: d.id, name: d.name, type: d.type, rssi: d.rssi, address: d.address, model: d.model }))); uiLog.info("Batch add response:", bulkResult); if (!bulkResult || bulkResult.success === false) { throw new Error(bulkResult?.data?.message || "Batch add failed"); } const addedCount = bulkResult?.addedCount ?? bulkResult?.data?.addedCount ?? 0; const skippedCount = bulkResult?.skippedCount ?? bulkResult?.data?.skippedCount ?? 0; toastSuccess(`Added ${addedCount} device(s)${skippedCount > 0 ? ` (${skippedCount} skipped)` : ""}`); await loadConfiguredDevices(); selectedIds.clear(); addSelectedBtn.disabled = true; addSelectedBtn.textContent = "Add Selected"; await updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds); } catch (e) { uiLog.error("Batch add error:", e); toastError(e instanceof Error ? e.message : "Failed to add devices"); addSelectedBtn.disabled = false; addSelectedBtn.textContent = "Add Selected"; } finally { hideBusyUi(); } }; const enableSelectedBtn = document.createElement("button"); enableSelectedBtn.textContent = "Enable Selected"; Object.assign(enableSelectedBtn.style, actionBtnStyle); enableSelectedBtn.disabled = true; enableSelectedBtn.onclick = async () => { if (!selectedIds.size) { return; } enableSelectedBtn.disabled = true; enableSelectedBtn.textContent = "Enabling..."; try { showBusyUi(); await batchSetDeviceEnabled(selectedIds, true); toastSuccess("Selected devices enabled"); await loadConfiguredDevices(); enableSelectedBtn.disabled = true; enableSelectedBtn.textContent = "Enable Selected"; await updateDiscoveryView(discoveredDevices, preferences, groupBy, hideAdded, selectedIds); } catch (e) { uiLog.error("Batch enable error:", e); toastError(e instanceof Error ? e.message : "Failed to enable devices"); enableSelectedBtn.disabled = false; enableSelectedBtn.textContent = "Enable Selected"; } finally { hideBusyUi(); } }; const disableSelectedBtn = document.createElement("button"); disableSelectedBtn.textContent = "Disable Selected"; Object.assign(disableSelectedBtn.style, actionBtnStyle); disableSelectedBtn.disabled = true; disableSelectedBtn.onclick = async () => { if (!selectedIds.size) { return; } disableSelectedBtn.disabled = true; disableSelectedBtn.textContent = "Disabling..."; try { showBusyUi(); await batchSetDeviceEnabled(selectedIds, false); toastSuccess("Selected devices disabled"); await loadConfiguredDevices(); disableSelectedBtn.disabled = true; disableSelectedBtn.textContent = "Disable Selected"; await updateDiscover