From 128626ba80af0b1d2085f8b22e3f8b3937654bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Meireles?= Date: Wed, 19 Jan 2022 10:19:27 +0000 Subject: [PATCH] harden offline initialization in order to get rid of #90 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: António Meireles --- src/platform.ts | 4 ++-- src/viera.ts | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/platform.ts b/src/platform.ts index dfd3863..187c207 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -153,8 +153,8 @@ class VieramaticPlatform implements DynamicPlatformPlugin { return { value: new VieramaticPlatformAccessory(this, accessory, device) } } catch (error) { - this.log.error('device:', JSON.stringify(device)) - this.log.error('specs:', JSON.stringify(tv.specs)) + this.log.error('device:', JSON.stringify(device, undefined, 2)) + this.log.error('specs:', JSON.stringify(tv.specs, undefined, 2)) return { error: error as Error } } } diff --git a/src/viera.ts b/src/viera.ts index a97369c..5f9648b 100644 --- a/src/viera.ts +++ b/src/viera.ts @@ -104,14 +104,23 @@ class VieraTV implements VieraTV { tv.specs = await tv.#getSpecs() settings.bootstrap ??= false if (!settings.bootstrap) { - if (isEmpty(tv.specs) && settings.cached) { + if (isEmpty(tv.specs) && settings.cached && !isEmpty(settings.cached)) { tv.log.warn(`Unable to fetch specs from TV at '${ip}'.`) tv.log.warn('Using the previously cached ones:\n\n', JSON.stringify(settings.cached)) - if (settings.cached.requiresEncryption) { - const err = `IGNORING '${ip}' as we do not support offline initialization, from cache, for models that require encryption.` - return { error: Error(err) } + const err = `IGNORING '${ip}' as we do not support offline initialization, from cache, for models that require encryption.` + if (settings.cached.requiresEncryption) return { error: Error(err) } + + tv.specs = settings.cached + } + if (isEmpty(tv.specs)) { + tv.log.error( + 'please fill a bug at https://github.com/AntonioMeireles/homebridge-vieramatic/issues with data bellow' + ) + return { + error: Error( + `${ip}: offline initialization failure!\n${JSON.stringify(settings, undefined, 2)}` + ) } - tv.specs ??= settings.cached } if (tv.specs.requiresEncryption) {