Compare commits

...

4 Commits

Author SHA1 Message Date
Philip Abbey
46740fac76 Update HISTORY.md
Amended v3.1 test to include the bug fix.
2025-08-15 13:39:56 +01:00
Philip Abbey
649f0f250d 264 add custom https headers for eg cloudflare tunnels (#272)
Additional settings to provide a custom HTTP header so that the
application can work with Cloudflare's Web Application Firewall (WAF).
2025-08-15 13:09:27 +01:00
Philip Abbey
5a0bd98ddb Candidate bug fix for crash opening app from glance
Changed the function parameters on WebhookManager.onReturnRegisterWebhookSensor() to remove the last optional parameter that was causing the error message and this appears to keep the application happy on start up. Looks like an API request race between the glance's last call and the app first call.

Co-Authored-By: __JosephAbbey <me@josephabbey.dev>
2025-08-15 12:56:22 +01:00
Philip Abbey
ce81c6af0a Review comments
Co-Authored-By: Lars Pöpperl <45465820+tispokes@users.noreply.github.com>
2025-08-11 20:07:10 +01:00
6 changed files with 64 additions and 65 deletions

View File

@@ -8,4 +8,4 @@
"bin": true,
"export": true
}
}
}

View File

@@ -46,4 +46,4 @@
| 2.31 | Adding [two new options](./examples/Actions.md#exit-on-tap) to the menu items: 1) The ability to disable a menu item, e.g. temporarily for seasonal changes, 2) The option to exit after a menu item has been select. |
| 2.32 | Bug fix for a breaking change extracting options caused by the need to rearrange function parameters for an [annoying compiler error](https://github.com/house-of-abbey/GarminHomeAssistant/issues/253). |
| 3.0 | First version with the ability to use [Wi-Fi or LTE](Wi-Fi.md) instead of Bluetooth but with limited functionality, thanks to [@vincentezw](https://github.com/vincentezw). |
| 3.1 | Added the ability for users to provide custom HTTP headers for their Home Assistant server. Improved German language translations. Removed all groups in settings as the SDK is buggy. |
| 3.1 | Added the ability for users to provide custom HTTP headers for their Home Assistant server. Improved German language translations. Removed all groups in settings as the SDK is buggy. Fixed a bug with templates in glances causing application crash on startup. |

View File

@@ -29,3 +29,9 @@ The authors of the Garmin Home Assistant application do not use, and hence do no
## Credits
With thanks to Lars Pöpperl ([@tispokes](https://github.com/tispokes)) for contributing to this solution.
## References
* [Using Cloudflare ZeroTrust and mTLS to securely access Home Assistant via the internet](https://kcore.org/2024/06/28/using-cloudflare-zerotrust-and-mtls-with-home-assistant-via-the-internet/)
* [Home Assistant Add-on: Cloudflared](https://github.com/brenner-tobias/addon-cloudflared)

View File

@@ -25,8 +25,8 @@ using Toybox.Timer;
//
(:glance, :background)
class HomeAssistantApp extends Application.AppBase {
private var mApiStatus as Lang.String?;
private var mHasToast as Lang.Boolean = false;
private var mApiStatus as Lang.String?;
private var mMenuStatus as Lang.String?;
private var mHaMenu as HomeAssistantView?;
private var mGlanceTemplate as Lang.String? = null;
@@ -36,14 +36,13 @@ class HomeAssistantApp extends Application.AppBase {
private var mUpdateTimer as Timer.Timer?;
// Array initialised by onReturnFetchMenuConfig()
private var mItemsToUpdate as Lang.Array<HomeAssistantToggleMenuItem or HomeAssistantTapMenuItem or HomeAssistantGroupMenuItem>?;
private var mIsGlance as Lang.Boolean = false;
private var mIsApp as Lang.Boolean = false; // Or Widget
private var mUpdating as Lang.Boolean = false; // Don't start a second chain of updates
private var mTemplates as Lang.Dictionary = {};
private var mNotifiedNoBle as Lang.Boolean = false;
//! Class Constructor
//
//
function initialize() {
AppBase.initialize();
// ATTENTION when adding stuff into this block:
@@ -155,7 +154,6 @@ class HomeAssistantApp extends Application.AppBase {
//! @param responseCode Response code.
//! @param data Response data.
//
(:glance)
function onReturnFetchMenuConfig(
responseCode as Lang.Number,
data as Null or Lang.Dictionary or Lang.String
@@ -168,35 +166,35 @@ class HomeAssistantApp extends Application.AppBase {
case Communications.BLE_HOST_TIMEOUT:
case Communications.BLE_CONNECTION_UNAVAILABLE:
// System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoPhone) as Lang.String);
}
break;
case Communications.BLE_QUEUE_FULL:
// System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.ApiFlood) as Lang.String);
}
break;
case Communications.NETWORK_REQUEST_TIMED_OUT:
// System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoResponse) as Lang.String);
}
break;
case Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE:
// System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: INVALID_HTTP_BODY_IN_NETWORK_RESPONSE, check JSON is returned.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoJson) as Lang.String);
}
break;
case 404:
// System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: 404, page not found. Check Configuration URL setting.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.ConfigUrlNotFound) as Lang.String);
}
break;
@@ -212,7 +210,7 @@ class HomeAssistantApp extends Application.AppBase {
mMenuStatus = WatchUi.loadResource($.Rez.Strings.Available) as Lang.String;
}
}
if (mIsGlance) {
if (!mIsApp) {
glanceTemplate(data);
} else {
if (data == null) {
@@ -226,7 +224,7 @@ class HomeAssistantApp extends Application.AppBase {
default:
// System.println("HomeAssistantApp onReturnFetchMenuConfig(): Unhandled HTTP response code = " + responseCode);
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr) as Lang.String + responseCode);
}
break;
@@ -251,7 +249,6 @@ class HomeAssistantApp extends Application.AppBase {
//! @return Return true if the menu came from the cache, otherwise false. This is because fetching
//! the menu when not in the cache is asynchronous and affects how the views are managed.
//
(:glance)
function fetchMenuConfig() as Lang.Boolean {
// System.println("Menu URL = " + Settings.getConfigUrl());
if (Settings.getConfigUrl().equals("")) {
@@ -275,7 +272,7 @@ class HomeAssistantApp extends Application.AppBase {
errorRez = $.Rez.Strings.Unavailable;
}
// System.println("HomeAssistantApp fetchMenuConfig(): No Phone connection, skipping API call.");
if (mIsGlance) {
if (!mIsApp) {
WatchUi.requestUpdate();
} else {
ErrorView.show(WatchUi.loadResource(errorRez) as Lang.String);
@@ -296,7 +293,7 @@ class HomeAssistantApp extends Application.AppBase {
} else {
mMenuStatus = WatchUi.loadResource($.Rez.Strings.Cached) as Lang.String;
WatchUi.requestUpdate();
if (mIsGlance) {
if (!mIsApp) {
glanceTemplate(menu);
} else {
buildMenu(menu);
@@ -519,7 +516,6 @@ class HomeAssistantApp extends Application.AppBase {
//! @param responseCode Response code.
//! @param data Response data.
//
(:glance)
function onReturnFetchApiStatus(
responseCode as Lang.Number,
data as Null or Lang.Dictionary or Lang.String
@@ -532,35 +528,35 @@ class HomeAssistantApp extends Application.AppBase {
case Communications.BLE_HOST_TIMEOUT:
case Communications.BLE_CONNECTION_UNAVAILABLE:
// System.println("HomeAssistantApp onReturnFetchApiStatus() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoPhone) as Lang.String);
}
break;
case Communications.BLE_QUEUE_FULL:
// System.println("HomeAssistantApp onReturnFetchApiStatus() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.ApiFlood) as Lang.String);
}
break;
case Communications.NETWORK_REQUEST_TIMED_OUT:
// System.println("HomeAssistantApp onReturnFetchApiStatus() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoResponse) as Lang.String);
}
break;
case Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE:
// System.println("HomeAssistantApp onReturnFetchApiStatus() Response Code: INVALID_HTTP_BODY_IN_NETWORK_RESPONSE, check JSON is returned.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoJson) as Lang.String);
}
break;
case 404:
// System.println("HomeAssistantApp onReturnFetchApiStatus() Response Code: 404, page not found. Check Configuration URL setting.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.ConfigUrlNotFound) as Lang.String);
}
break;
@@ -569,7 +565,7 @@ class HomeAssistantApp extends Application.AppBase {
if ((data != null) && data.get("message").equals("API running.")) {
mApiStatus = WatchUi.loadResource($.Rez.Strings.Available) as Lang.String;
} else {
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show("API " + mApiStatus + ".");
}
}
@@ -577,7 +573,7 @@ class HomeAssistantApp extends Application.AppBase {
default:
// System.println("HomeAssistantApp onReturnFetchApiStatus(): Unhandled HTTP response code = " + responseCode);
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr) as Lang.String + responseCode);
}
}
@@ -586,7 +582,6 @@ class HomeAssistantApp extends Application.AppBase {
//! Construct the GET request to test the API status, is it accessible?
//
(:glance)
function fetchApiStatus() as Void {
var phoneConnected = System.getDeviceSettings().phoneConnected;
var connectionAvailable = System.getDeviceSettings().connectionAvailable;
@@ -596,13 +591,13 @@ class HomeAssistantApp extends Application.AppBase {
mApiStatus = WatchUi.loadResource($.Rez.Strings.Unconfigured) as Lang.String;
WatchUi.requestUpdate();
} else {
if (! mIsGlance && Settings.getWifiLteExecutionEnabled() && (! phoneConnected || ! connectionAvailable)) {
if ( mIsApp && Settings.getWifiLteExecutionEnabled() && (! phoneConnected || ! connectionAvailable)) {
// System.println("HomeAssistantApp fetchApiStatus(): In-app Wifi mode (No Phone and Internet connection), early return.");
return;
} else if (! phoneConnected) {
// System.println("HomeAssistantApp fetchApiStatus(): No Phone connection, skipping API call.");
mApiStatus = WatchUi.loadResource($.Rez.Strings.Unavailable) as Lang.String;
if (mIsGlance) {
if (!mIsApp) {
WatchUi.requestUpdate();
} else {
System.println("we here");
@@ -611,7 +606,7 @@ class HomeAssistantApp extends Application.AppBase {
} else if (! connectionAvailable) {
// System.println("HomeAssistantApp fetchApiStatus(): No Internet connection, skipping API call.");
mApiStatus = WatchUi.loadResource($.Rez.Strings.Unavailable) as Lang.String;
if (mIsGlance) {
if (!mIsApp) {
WatchUi.requestUpdate();
} else {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoInternet) as Lang.String);
@@ -639,7 +634,6 @@ class HomeAssistantApp extends Application.AppBase {
//! @param responseCode Response code.
//! @param data Response data.
//
(:glance)
function onReturnFetchGlanceContent(
responseCode as Lang.Number,
data as Null or Lang.Dictionary or Lang.String
@@ -651,35 +645,35 @@ class HomeAssistantApp extends Application.AppBase {
case Communications.BLE_HOST_TIMEOUT:
case Communications.BLE_CONNECTION_UNAVAILABLE:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoPhone) as Lang.String);
}
break;
case Communications.BLE_QUEUE_FULL:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.ApiFlood) as Lang.String);
}
break;
case Communications.NETWORK_REQUEST_TIMED_OUT:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoResponse) as Lang.String);
}
break;
case Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: INVALID_HTTP_BODY_IN_NETWORK_RESPONSE, check JSON is returned.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoJson) as Lang.String);
}
break;
case 404:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: 404, page not found. Check Configuration URL setting.");
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.ConfigUrlNotFound) as Lang.String);
}
break;
@@ -692,7 +686,7 @@ class HomeAssistantApp extends Application.AppBase {
default:
// System.println("HomeAssistantApp onReturnFetchGlanceContent(): Unhandled HTTP response code = " + responseCode);
if (!mIsGlance) {
if (mIsApp) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr) as Lang.String + responseCode);
}
}
@@ -701,7 +695,6 @@ class HomeAssistantApp extends Application.AppBase {
//! Construct the GET request to convert the optional glance template to text for display.
//
(:glance)
function fetchGlanceContent() as Void {
if (mGlanceTemplate != null) {
// https://developers.home-assistant.io/docs/api/native-app-integration/sending-data/#render-templates
@@ -739,7 +732,6 @@ class HomeAssistantApp extends Application.AppBase {
//!
//! @return A string describing the API status
//
(:glance)
function getApiStatus() as Lang.String {
return mApiStatus;
}
@@ -748,7 +740,6 @@ class HomeAssistantApp extends Application.AppBase {
//!
//! @return A string describing the Menu status
//
(:glance)
function getMenuStatus() as Lang.String {
return mMenuStatus;
}
@@ -758,7 +749,6 @@ class HomeAssistantApp extends Application.AppBase {
//!
//! @return A string derived from the glance template (or null)
//
(:glance)
function getGlanceText() as Lang.String? {
return mGlanceText;
}
@@ -802,7 +792,7 @@ class HomeAssistantApp extends Application.AppBase {
//! @return The glance view
//
function getGlanceView() as [ WatchUi.GlanceView ] or [ WatchUi.GlanceView, WatchUi.GlanceViewDelegate ] or Null {
mIsGlance = true;
mIsApp = false; // A bit unnecessary given the default
mApiStatus = WatchUi.loadResource($.Rez.Strings.Checking) as Lang.String;
mMenuStatus = WatchUi.loadResource($.Rez.Strings.Checking) as Lang.String;
Settings.update();
@@ -826,7 +816,9 @@ class HomeAssistantApp extends Application.AppBase {
mGlanceTimer = null;
fetchMenuConfig();
fetchApiStatus();
fetchGlanceContent();
if (Settings.getWebhookId() != null && !Settings.getWebhookId().equals("")) {
fetchGlanceContent();
}
}
//! Code for when the application settings are updated.
@@ -844,6 +836,8 @@ class HomeAssistantApp extends Application.AppBase {
}
//! Determine is we are a glance or the full application. Glances should be considered to be separate applications.
//!
//! @return We are an application (if not we're a glance)
//
function getIsApp() as Lang.Boolean {
return mIsApp;
@@ -859,6 +853,8 @@ class HomeAssistantApp extends Application.AppBase {
}
//! Global function to return the application object.
//!
//! @return The application object.
//
(:glance, :background)
function getApp() as HomeAssistantApp {

View File

@@ -297,7 +297,7 @@ class Settings {
//! @return The augmented HTTP header options.
//
static function augmentHttpHeaders(options as Lang.Dictionary) {
// Use 'm.length() > 0' here in preference to 'm != ""'. The latter makes the app crash on device but not in simulation.
// Use 'm.length() > 0' here in preference to 'm != ""' or '.equals("")'. They make the App crash on device but not in simulation.
if (mUserHeaderName != null && mUserHeaderName.length() > 0 && mUserHeaderValue != null && mUserHeaderValue.length() > 0) {
options[mUserHeaderName] = mUserHeaderValue;
}

View File

@@ -25,7 +25,9 @@ using Toybox.WatchUi;
//!
//! Reference: https://developers.home-assistant.io/docs/api/native-app-integration
//
(:glance)
class WebhookManager {
private var mSensors as Lang.Array<Lang.Object> = [];
//! Callback for requesting a Webhook ID.
//!
@@ -130,11 +132,7 @@ class WebhookManager {
//! @param sensors The remaining sensors to be processed. The list of sensors is iterated through
//! until empty. Each POST request creating one sensor on the local Home Assistant.
//
function onReturnRegisterWebhookSensor(
responseCode as Lang.Number,
data as Null or Lang.Dictionary or Lang.String,
sensors as Lang.Array<Lang.Object>
) as Void {
function onReturnRegisterWebhookSensor(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
switch (responseCode) {
case Communications.BLE_HOST_TIMEOUT:
case Communications.BLE_CONNECTION_UNAVAILABLE:
@@ -176,13 +174,14 @@ class WebhookManager {
case 200:
case 201:
if (data instanceof Lang.Dictionary) {
var d = data as Lang.Dictionary;
var d = data as Lang.Dictionary;
var b = d.get("success") as Lang.Boolean?;
if (b != null and b != false) {
if (sensors.size() == 0) {
mSensors = mSensors.slice(1, null);
if (mSensors.size() == 0) {
getApp().startUpdates();
} else {
registerWebhookSensor(sensors);
registerWebhookSensor();
}
} else {
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Failure, no 'success'.");
@@ -213,11 +212,10 @@ class WebhookManager {
}
}
//! Local method to send the POST request to register a number of sensors.
//!
//! @param sensors An array of sensors, e.g. As created by `registerWebhookSensors()`.
//! Local method to send the POST request to register a number of sensors. The sensors are taken from the class variable
//! mSensors created by registerWebhookSensors().
//
private function registerWebhookSensor(sensors as Lang.Array<Lang.Object>) {
private function registerWebhookSensor() {
var url = Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId();
// System.println("WebhookManager registerWebhookSensor(): Registering webhook sensor: " + sensor.toString());
// System.println("WebhookManager registerWebhookSensor(): URL=" + url);
@@ -226,15 +224,14 @@ class WebhookManager {
url,
{
"type" => "register_sensor",
"data" => sensors[0]
"data" => mSensors[0]
},
{
:method => Communications.HTTP_REQUEST_METHOD_POST,
:headers => Settings.augmentHttpHeaders({
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON
}),
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
:context => sensors.slice(1, null)
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
},
method(:onReturnRegisterWebhookSensor)
);
@@ -245,7 +242,7 @@ class WebhookManager {
function registerWebhookSensors() {
var heartRate = Activity.getActivityInfo().currentHeartRate;
var sensors = [
mSensors = [
{
"device_class" => "battery",
"name" => "Battery Level",
@@ -283,7 +280,7 @@ class WebhookManager {
if (Toybox has :ActivityMonitor) {
// System.println("WebhookManager registerWebhookSensors(): has ActivityMonitor class");
var activityInfo = ActivityMonitor.getInfo();
sensors.add({
mSensors.add({
"name" => "Steps today",
"state" => activityInfo.steps == null ? "unknown" : activityInfo.steps,
"type" => "sensor",
@@ -294,7 +291,7 @@ class WebhookManager {
});
if (ActivityMonitor.Info has :floorsClimbed) {
sensors.add({
mSensors.add({
"name" => "Floors climbed today",
"state" => activityInfo.floorsClimbed == null ? "unknown" : activityInfo.floorsClimbed,
"type" => "sensor",
@@ -306,7 +303,7 @@ class WebhookManager {
}
if (ActivityMonitor.Info has :floorsDescended) {
sensors.add({
mSensors.add({
"name" => "Floors descended today",
"state" => activityInfo.floorsDescended == null ? "unknown" : activityInfo.floorsDescended,
"type" => "sensor",
@@ -318,7 +315,7 @@ class WebhookManager {
}
if (ActivityMonitor.Info has :respirationRate) {
sensors.add({
mSensors.add({
"name" => "Respiration rate",
"state" => activityInfo.respirationRate == null ? "unknown" : activityInfo.respirationRate,
"type" => "sensor",
@@ -345,14 +342,14 @@ class WebhookManager {
activity = -1;
sub_activity = -1;
}
sensors.add({
mSensors.add({
"name" => "Activity",
"state" => activity,
"type" => "sensor",
"unique_id" => "activity",
"disabled" => !Settings.isSensorsLevelEnabled()
});
sensors.add({
mSensors.add({
"name" => "Sub-activity",
"state" => sub_activity,
"type" => "sensor",
@@ -361,7 +358,7 @@ class WebhookManager {
});
}
registerWebhookSensor(sensors);
registerWebhookSensor();
}
}