From 42d1a7233c6c4fdce02b239608e4c2fd7f8c1d35 Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Sun, 18 Aug 2024 12:04:34 +0100 Subject: [PATCH 1/3] Update Settings.mc Test the phone is connected before attempting WebHook HTTP requests. --- source/Settings.mc | 70 ++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/source/Settings.mc b/source/Settings.mc index 8a598d4..404f163 100644 --- a/source/Settings.mc +++ b/source/Settings.mc @@ -70,42 +70,46 @@ class Settings { // Manage this inside the application or widget only (not a glance or background service process) if (mIsApp) { if (mHasService) { - mWebhookManager = new WebhookManager(); - if (getWebhookId().equals("")) { - // System.println("Settings update(): Doing full webhook & sensor creation."); - mWebhookManager.requestWebhookId(); - } else { - // System.println("Settings update(): Doing just sensor creation."); - // We already have a Webhook ID, so just enable or disable the sensor in Home Assistant. - // Its a multiple step process, hence starting at step 0. - mWebhookManager.registerWebhookSensor({ - "device_class" => "battery", - "name" => "Battery Level", - "state" => System.getSystemStats().battery, - "type" => "sensor", - "unique_id" => "battery_level", - "unit_of_measurement" => "%", - "state_class" => "measurement", - "entity_category" => "diagnostic", - "disabled" => !Settings.isSensorsLevelEnabled() - }, 0); - } - if (mIsSensorsLevelEnabled) { - // Create the timed activity - if ((Background.getTemporalEventRegisteredTime() == null) or - (Background.getTemporalEventRegisteredTime() != (mBatteryRefreshRate * 60))) { - Background.registerForTemporalEvent(new Time.Duration(mBatteryRefreshRate * 60)); // Convert to seconds - Background.registerForActivityCompletedEvent(); + if (System.getDeviceSettings().phoneConnected) { + mWebhookManager = new WebhookManager(); + if (getWebhookId().equals("")) { + // System.println("Settings update(): Doing full webhook & sensor creation."); + mWebhookManager.requestWebhookId(); + } else { + // System.println("Settings update(): Doing just sensor creation."); + // We already have a Webhook ID, so just enable or disable the sensor in Home Assistant. + // Its a multiple step process, hence starting at step 0. + mWebhookManager.registerWebhookSensor({ + "device_class" => "battery", + "name" => "Battery Level", + "state" => System.getSystemStats().battery, + "type" => "sensor", + "unique_id" => "battery_level", + "unit_of_measurement" => "%", + "state_class" => "measurement", + "entity_category" => "diagnostic", + "disabled" => !Settings.isSensorsLevelEnabled() + }, 0); } - } else if (Background.getTemporalEventRegisteredTime() != null) { - Background.deleteTemporalEvent(); - Background.deleteActivityCompletedEvent(); + if (mIsSensorsLevelEnabled) { + // Create the timed activity + if ((Background.getTemporalEventRegisteredTime() == null) or + (Background.getTemporalEventRegisteredTime() != (mBatteryRefreshRate * 60))) { + Background.registerForTemporalEvent(new Time.Duration(mBatteryRefreshRate * 60)); // Convert to seconds + Background.registerForActivityCompletedEvent(); + } + } else if (Background.getTemporalEventRegisteredTime() != null) { + Background.deleteTemporalEvent(); + Background.deleteActivityCompletedEvent(); + } + } else { + // Explicitly disable the background event which persists when the application closes. + // If !mHasService disable the Settings option as user feedback + unsetIsSensorsLevelEnabled(); + unsetWebhookId(); } } else { - // Explicitly disable the background event which persists when the application closes. - // If !mHasService disable the Settings option as user feedback - unsetIsSensorsLevelEnabled(); - unsetWebhookId(); + ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoPhone) as Lang.String); } } // System.println("Settings update(): getTemporalEventRegisteredTime() = " + Background.getTemporalEventRegisteredTime()); From 7c7130367fa37940f63f513bd52d003651f90cba Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Sun, 18 Aug 2024 12:26:45 +0100 Subject: [PATCH 2/3] Update Settings.mc Rearranged 'else' clauses. --- source/Settings.mc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/Settings.mc b/source/Settings.mc index 404f163..c7f0e90 100644 --- a/source/Settings.mc +++ b/source/Settings.mc @@ -103,13 +103,13 @@ class Settings { Background.deleteActivityCompletedEvent(); } } else { - // Explicitly disable the background event which persists when the application closes. - // If !mHasService disable the Settings option as user feedback - unsetIsSensorsLevelEnabled(); - unsetWebhookId(); + ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoPhone) as Lang.String); } } else { - ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoPhone) as Lang.String); + // Explicitly disable the background event which persists when the application closes. + // If !mHasService disable the Settings option as user feedback + unsetIsSensorsLevelEnabled(); + unsetWebhookId(); } } // System.println("Settings update(): getTemporalEventRegisteredTime() = " + Background.getTemporalEventRegisteredTime()); From 51081ee2e6f918b838e76f2f4bbbb926444cff8e Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Sun, 18 Aug 2024 13:03:30 +0100 Subject: [PATCH 3/3] Update HISTORY.md Added v2.16 --- HISTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.md b/HISTORY.md index 1e436f5..f433cb5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -28,3 +28,4 @@ | 2.13 | Moved the template status queries to Webhooks in order to fix the situation where an account is a non-privileged user. Added telemetry update on activity completion to make automations more timely at the end of an activity. When using a polling delay, there is no longer a startup delay for status updates and an action will trigger an immediate round of updates. | | 2.14 | Cautionary bug fix for the background service code where refactorisation spoilt some API level guard clauses. | | 2.15 | Better support for templates by isolating erroneous returns and marking the menu item. | +| 2.16 | Bug fix for lack of phone connection when starting the application. |