feat: persist and cache all known devices; discovery only adds/updates

- Store.mergeDevices(): updates existing by UDN, adds new, keeps offline devices
- platform.js: merges discovered into cache; registers cached-offline devices
  in HomeKit so they remain visible; only removes truly orphaned accessories
- server.js: discover endpoint merges and returns full known device list

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
SRS IT
2026-03-28 22:28:11 -04:00
parent e52b3578dc
commit e8b365e5a7
3 changed files with 49 additions and 10 deletions
+18 -7
View File
@@ -114,25 +114,36 @@ class WemoPlatform {
this.log.info(`Found ${discovered.length} Wemo device(s)`);
// Save discovered device list for the custom UI
this._store.saveDevices(discovered.map((d) => ({
// Merge discovered devices into the cached list — keep offline devices too
const freshForStore = discovered.map((d) => ({
host: d.host,
port: d.port,
udn: d.udn ?? `${d.host}:${d.port}`,
friendlyName: d.friendlyName ?? d.host,
productModel: d.productModel ?? 'Wemo Device',
firmwareVersion: d.firmwareVersion ?? null,
})));
}));
const allKnown = this._store.mergeDevices(freshForStore);
// Register newly discovered devices in HomeKit
for (const device of discovered) {
this._registerDevice(device, pollInterval);
}
// Remove stale accessories (devices no longer discovered)
const activeUUIDs = new Set(discovered.map((d) => this._uuidForDevice(d)));
// Register previously cached devices that weren't discovered (may be offline)
// so HomeKit still knows about them
const discoveredUDNs = new Set(discovered.map((d) => d.udn ?? `${d.host}:${d.port}`));
for (const cached of allKnown) {
if (!discoveredUDNs.has(cached.udn)) {
this.log.info(`Device offline/not found during discovery, keeping cached: ${cached.friendlyName}`);
this._registerDevice(cached, pollInterval);
}
}
// Remove orphaned accessories — those with no device context at all
for (const [uuid, acc] of this._accessories) {
if (!activeUUIDs.has(uuid)) {
this.log.info('Removing stale accessory: ' + acc.displayName);
if (!acc.context?.device?.host) {
this.log.info('Removing orphaned accessory (no device context): ' + acc.displayName);
this._handlers.get(uuid)?.stopPolling();
this._handlers.delete(uuid);
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [acc]);