From 7d77a79ad8b65476f1646b10e235f507b27e9c0d Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Fri, 15 Aug 2025 17:04:54 +0100 Subject: [PATCH] Fix for update before Webhook ID (re-)created. --- source/HomeAssistantApp.mc | 35 ++++++++++++++++++++--------------- source/Settings.mc | 24 ++++++++++++++++++++---- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/source/HomeAssistantApp.mc b/source/HomeAssistantApp.mc index 1ba112d..ded8dde 100644 --- a/source/HomeAssistantApp.mc +++ b/source/HomeAssistantApp.mc @@ -311,7 +311,7 @@ class HomeAssistantApp extends Application.AppBase { private function buildMenu(menu as Lang.Dictionary) { mHaMenu = new HomeAssistantView(menu, null); mQuitTimer.begin(); - if (!Settings.getWebhookId().equals("")) { + if (!Settings.getWebhookId().equals("") && !Settings.getClearWebhookId()) { startUpdates(); } // If not, this will be done via a chain in Settings.webhook() and mWebhookManager.requestWebhookId() that registers the sensors. } @@ -399,20 +399,25 @@ class HomeAssistantApp extends Application.AppBase { case 200: status = WatchUi.loadResource($.Rez.Strings.Available) as Lang.String; // System.println("mItemsToUpdate: " + mItemsToUpdate); - if (mItemsToUpdate != null) { - for (var i = 0; i < mItemsToUpdate.size(); i++) { - var item = mItemsToUpdate[i]; - var state = data.get(i.toString()); - item.updateState(state); - if (item instanceof HomeAssistantToggleMenuItem) { - (item as HomeAssistantToggleMenuItem).updateToggleState(data.get(i.toString() + "t")); + if (data == null) { + // Simulation and real device behave differently, hence 2nd NoJson error message for "data == null". + ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoJson) as Lang.String); + } else { + if (mItemsToUpdate != null) { + for (var i = 0; i < mItemsToUpdate.size(); i++) { + var item = mItemsToUpdate[i]; + var state = data.get(i.toString()); + item.updateState(state); + if (item instanceof HomeAssistantToggleMenuItem) { + (item as HomeAssistantToggleMenuItem).updateToggleState(data.get(i.toString() + "t")); + } + } + var delay = Settings.getPollDelay(); + if (delay > 0) { + mUpdateTimer.start(method(:updateMenuItems), delay, false); + } else { + updateMenuItems(); } - } - var delay = Settings.getPollDelay(); - if (delay > 0) { - mUpdateTimer.start(method(:updateMenuItems), delay, false); - } else { - updateMenuItems(); } } break; @@ -816,7 +821,7 @@ class HomeAssistantApp extends Application.AppBase { mGlanceTimer = null; fetchMenuConfig(); fetchApiStatus(); - if (Settings.getWebhookId() != null && !Settings.getWebhookId().equals("")) { + if (!Settings.getWebhookId().equals("") && !Settings.getClearWebhookId()) { fetchGlanceContent(); } } diff --git a/source/Settings.mc b/source/Settings.mc index 510c097..5f89a03 100644 --- a/source/Settings.mc +++ b/source/Settings.mc @@ -141,7 +141,11 @@ class Settings { //! @return The API Key // static function getApiKey() as Lang.String { - return mApiKey; + if (mApiKey == null) { + return ""; + } else { + return mApiKey; + } } //! Get the Webhook ID supplied as part of the Settings. @@ -149,7 +153,11 @@ class Settings { //! @return The Webhook ID // static function getWebhookId() as Lang.String { - return mWebhookId; + if (mWebhookId == null) { + return ""; + } else { + return mWebhookId; + } } //! Set the Webhook ID supplied as part of the Settings. @@ -173,7 +181,11 @@ class Settings { //! @return The API URL // static function getApiUrl() as Lang.String { - return mApiUrl; + if (mApiUrl == null) { + return ""; + } else { + return mApiUrl; + } } //! Get the menu configuration URL supplied as part of the Settings. @@ -181,7 +193,11 @@ class Settings { //! @return The menu configuration URL // static function getConfigUrl() as Lang.String { - return mConfigUrl; + if (mConfigUrl == null) { + return ""; + } else { + return mConfigUrl; + } } //! Get the menu cache Boolean option supplied as part of the Settings.