From 876496953700f4c3b9733345539ea7c344331093 Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Thu, 16 Nov 2023 19:51:10 +0100 Subject: [PATCH 01/14] Leaner UI spike --- resources/settings/properties.xml | 2 + resources/settings/settings.xml | 9 ++ source/HomeAssistantIconMenuItem.mc | 165 ++++++++++++++++++++++++ source/HomeAssistantView.mc | 96 +++++++++++--- source/HomeAssistantViewIconMenuItem.mc | 54 ++++++++ 5 files changed, 305 insertions(+), 21 deletions(-) create mode 100644 source/HomeAssistantIconMenuItem.mc create mode 100644 source/HomeAssistantViewIconMenuItem.mc diff --git a/resources/settings/properties.xml b/resources/settings/properties.xml index f2e3a27..0829069 100644 --- a/resources/settings/properties.xml +++ b/resources/settings/properties.xml @@ -23,4 +23,6 @@ + + diff --git a/resources/settings/settings.xml b/resources/settings/settings.xml index 1ea3ca6..1b0ef4a 100644 --- a/resources/settings/settings.xml +++ b/resources/settings/settings.xml @@ -42,4 +42,13 @@ type="alphaNumeric" /> + + + + diff --git a/source/HomeAssistantIconMenuItem.mc b/source/HomeAssistantIconMenuItem.mc new file mode 100644 index 0000000..853cd1b --- /dev/null +++ b/source/HomeAssistantIconMenuItem.mc @@ -0,0 +1,165 @@ +//----------------------------------------------------------------------------------- +// +// Distributed under MIT Licence +// See https://github.com/house-of-abbey/GarminHomeAssistant/blob/main/LICENSE. +// +//----------------------------------------------------------------------------------- +// +// GarminHomeAssistant is a Garmin IQ application written in Monkey C and routinely +// tested on a Venu 2 device. The source code is provided at: +// https://github.com/house-of-abbey/GarminHomeAssistant. +// +// P A Abbey & J D Abbey, 31 October 2023 +// +// +// Description: +// +// Menu button that triggers a service. +// +//----------------------------------------------------------------------------------- + +using Toybox.Lang; +using Toybox.WatchUi; +using Toybox.Graphics; +using Toybox.Application.Properties; + +class HomeAssistantIconMenuItem extends WatchUi.IconMenuItem { + hidden var mApiKey as Lang.String; + hidden var strNoInternet as Lang.String; + hidden var strApiFlood as Lang.String; + hidden var strApiUrlNotFound as Lang.String; + hidden var strUnhandledHttpErr as Lang.String; + hidden var mService as Lang.String; + + function initialize( + label as Lang.String or Lang.Symbol, + subLabel as Lang.String or Lang.Symbol or Null, + identifier as Lang.Object or Null, + service as Lang.String or Null, + icon as Graphics.BitmapType or WatchUi.Drawable, + options as { + :alignment as WatchUi.MenuItem.Alignment + } or Null + ) { + strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet); + strApiFlood = WatchUi.loadResource($.Rez.Strings.ApiFlood); + strApiUrlNotFound = WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound); + strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr); + mApiKey = Properties.getValue("api_key"); + mService = service; + WatchUi.IconMenuItem.initialize( + label, + subLabel, + identifier, + icon, + options + ); + } + + // Callback function after completing the POST request to call a script. + // + function onReturnExecScript(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { + if (Globals.scDebug) { + System.println("HomeAssistantIconMenuItem onReturnExecScript() Response Code: " + responseCode); + System.println("HomeAssistantIconMenuItem onReturnExecScript() Response Data: " + data); + } + if (responseCode == Communications.BLE_QUEUE_FULL) { + if (Globals.scDebug) { + System.println("HomeAssistantIconMenuItem onReturnExecScript() Response Code: BLE_QUEUE_FULL, API calls too rapid."); + } + var cw = WatchUi.getCurrentView(); + if (!(cw[0] instanceof ErrorView)) { + // Avoid pushing multiple ErrorViews + WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP); + } + } else if (responseCode == 404) { + if (Globals.scDebug) { + System.println("HomeAssistantIconMenuItem onReturnExecScript() Response Code: 404, page not found. Check API URL setting."); + } + WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP); + } else if (responseCode == 200) { + if (Globals.scDebug) { + System.println("HomeAssistantIconMenuItem onReturnExecScript(): Service executed."); + } + var d = data as Lang.Array; + var toast = "Executed"; + for(var i = 0; i < d.size(); i++) { + if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) { + toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String; + } + } + if (WatchUi has :showToast) { + WatchUi.showToast(toast, null); + } else { + new Alert({ + :timeout => Globals.scAlertTimeout, + :font => Graphics.FONT_MEDIUM, + :text => toast, + :fgcolor => Graphics.COLOR_WHITE, + :bgcolor => Graphics.COLOR_BLACK + }).pushView(WatchUi.SLIDE_IMMEDIATE); + } + } else { + if (Globals.scDebug) { + System.println("HomeAssistantIconMenuItem onReturnExecScript(): Unhandled HTTP response code = " + responseCode); + } + WatchUi.pushView(new ErrorView(strUnhandledHttpErr + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP); + } + } + + function execScript() as Void { + var options = { + :method => Communications.HTTP_REQUEST_METHOD_POST, + :headers => { + "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, + "Authorization" => "Bearer " + mApiKey + }, + :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON + }; + if (System.getDeviceSettings().phoneConnected && System.getDeviceSettings().connectionAvailable) { + // Updated SDK and got a new error + // ERROR: venu: Cannot find symbol ':substring' on type 'PolyType'. + var id = mIdentifier as Lang.String; + if (mService == null) { + var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + id.substring(0, id.find(".")) + "/" + id.substring(id.find(".")+1, null); + if (Globals.scDebug) { + System.println("HomeAssistantIconMenuItem execScript() URL=" + url); + System.println("HomeAssistantIconMenuItem execScript() mIdentifier=" + mIdentifier); + } + Communications.makeWebRequest( + url, + null, + options, + method(:onReturnExecScript) + ); + } else { + var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null); + if (Globals.scDebug) { + System.println("HomeAssistantIconMenuItem execScript() URL=" + url); + System.println("HomeAssistantIconMenuItem execScript() mService=" + mService); + } + Communications.makeWebRequest( + url, + { + "entity_id" => id + }, + options, + method(:onReturnExecScript) + ); + } + if (Attention has :vibrate) { + Attention.vibrate([ + new Attention.VibeProfile(50, 100), // On for 100ms + new Attention.VibeProfile( 0, 100), // Off for 100ms + new Attention.VibeProfile(50, 100) // On for 100ms + ]); + } + } else { + if (Globals.scDebug) { + System.println("HomeAssistantIconMenuItem execScript(): No Internet connection, skipping API call."); + } + WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); + } + } + +} diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc index fe74847..4c26d4b 100644 --- a/source/HomeAssistantView.mc +++ b/source/HomeAssistantView.mc @@ -18,15 +18,17 @@ // //----------------------------------------------------------------------------------- +using Toybox.Application; using Toybox.Lang; using Toybox.Graphics; using Toybox.WatchUi; class HomeAssistantView extends WatchUi.Menu2 { - hidden var strMenuItemTap as Lang.String; + // List of items that need to have their status updated periodically - hidden var mListToggleItems = []; - hidden var mListMenuItems = []; + hidden var mListToggleItems = []; + hidden var mListMenuItems = []; + hidden var mListIconMenuItems = []; function initialize( definition as Lang.Dictionary, @@ -36,11 +38,17 @@ class HomeAssistantView extends WatchUi.Menu2 { :theme as WatchUi.MenuTheme or Null } or Null ) { - strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap); - var toggle_obj = { - :enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String, - :disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String - }; + + var toggle_obj = null; + var strMenuItemTap = null; + + if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == false){ + toggle_obj = { + :enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String, + :disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String + }; + strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap); + } if (options == null) { options = { @@ -69,19 +77,45 @@ class HomeAssistantView extends WatchUi.Menu2 { addItem(item); mListToggleItems.add(item); } else if (type.equals("tap") && service != null) { - addItem( - new HomeAssistantMenuItem( - name, - strMenuItemTap, - entity, - service, - null - ) - ); + if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == true){ + + var icon = new WatchUi.Bitmap({ + :rezId=>Rez.Drawables.ErrorIcon + }); + + var alignement = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT}; + + addItem( + new HomeAssistantIconMenuItem( + name, + strMenuItemTap, + entity, + service, + icon, + alignement + ) + ); + } else { + addItem( + new HomeAssistantMenuItem( + name, + strMenuItemTap, + entity, + service, + null + ) + ); + } } else if (type.equals("group")) { - var item = new HomeAssistantViewMenuItem(items[i]); - addItem(item); - mListMenuItems.add(item); + if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == true){ + var item = new HomeAssistantViewIconMenuItem(items[i]); + addItem(item); + mListIconMenuItems.add(item); + } else { + var item = new HomeAssistantViewMenuItem(items[i]); + addItem(item); + mListMenuItems.add(item); + } } } } @@ -89,10 +123,17 @@ class HomeAssistantView extends WatchUi.Menu2 { function getItemsToUpdate() as Lang.Array { var fullList = []; + var lmi = mListMenuItems as Lang.Array; for(var i = 0; i < lmi.size(); i++) { fullList.addAll(lmi[i].getMenuView().getItemsToUpdate()); } + + var limi = mListMenuItems as Lang.Array; + for(var i = 0; i < limi.size(); i++) { + fullList.addAll(limi[i].getMenuView().getItemsToUpdate()); + } + return fullList.addAll(mListToggleItems); } @@ -125,6 +166,12 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate { System.println(haItem.getLabel() + " " + haItem.getId()); } haItem.execScript(); + } else if (item instanceof HomeAssistantIconMenuItem) { + var haItem = item as HomeAssistantIconMenuItem; + if (Globals.scDebug) { + System.println(haItem.getLabel() + " " + haItem.getId()); + } + haItem.execScript(); } else if (item instanceof HomeAssistantViewMenuItem) { var haMenuItem = item as HomeAssistantViewMenuItem; if (Globals.scDebug) { @@ -132,6 +179,13 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate { } // No delegate state to be amended, so re-use 'self'. WatchUi.pushView(haMenuItem.getMenuView(), self, WatchUi.SLIDE_LEFT); + } else if (item instanceof HomeAssistantViewIconMenuItem) { + var haMenuItem = item as HomeAssistantViewIconMenuItem; + if (Globals.scDebug) { + System.println("IconMenu: " + haMenuItem.getLabel() + " " + haMenuItem.getId()); + } + // No delegate state to be amended, so re-use 'self'. + WatchUi.pushView(haMenuItem.getMenuView(), self, WatchUi.SLIDE_LEFT); } else { if (Globals.scDebug) { System.println(item.getLabel() + " " + item.getId()); @@ -143,4 +197,4 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate { WatchUi.popView(WatchUi.SLIDE_RIGHT); } -} \ No newline at end of file +} diff --git a/source/HomeAssistantViewIconMenuItem.mc b/source/HomeAssistantViewIconMenuItem.mc new file mode 100644 index 0000000..f2156b3 --- /dev/null +++ b/source/HomeAssistantViewIconMenuItem.mc @@ -0,0 +1,54 @@ +//----------------------------------------------------------------------------------- +// +// Distributed under MIT Licence +// See https://github.com/house-of-abbey/GarminHomeAssistant/blob/main/LICENSE. +// +//----------------------------------------------------------------------------------- +// +// GarminHomeAssistant is a Garmin IQ application written in Monkey C and routinely +// tested on a Venu 2 device. The source code is provided at: +// https://github.com/house-of-abbey/GarminHomeAssistant. +// +// P A Abbey & J D Abbey, 31 October 2023 +// +// +// Description: +// +// Menu button that opens a sub-menu. +// +//----------------------------------------------------------------------------------- + +using Toybox.Lang; +using Toybox.WatchUi; + +class HomeAssistantViewIconMenuItem extends WatchUi.IconMenuItem { + hidden var mMenu as HomeAssistantView; + + function initialize(definition as Lang.Dictionary) { + var label = definition.get("name") as Lang.String; + var identifier = definition.get("entity") as Lang.String; + + var icon = new WatchUi.Bitmap({ + :rezId=>Rez.Drawables.LauncherIcon, + :locX=>WatchUi.LAYOUT_HALIGN_CENTER, + :locY=>WatchUi.LAYOUT_VALIGN_CENTER + }); + + var alignement = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT}; + + WatchUi.IconMenuItem.initialize( + label, + null, + identifier, + icon, + alignement + ); + + mMenu = new HomeAssistantView(definition, null); + } + + function getMenuView() as HomeAssistantView { + return mMenu; + } + +} From 2c09ab71a879c56631b92d6651c761952f1c3171 Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Thu, 16 Nov 2023 21:23:36 +0100 Subject: [PATCH 02/14] Lean UI spike use correct icons for menu and tap --- resources-icons-18/drawables.xml | 4 +++- resources-icons-18/menu.svg | 1 + resources-icons-18/tap.svg | 1 + resources-icons-21/drawables.xml | 4 +++- resources-icons-21/menu.svg | 1 + resources-icons-21/tap.svg | 1 + resources-icons-24/drawables.xml | 4 +++- resources-icons-24/menu.svg | 1 + resources-icons-24/tap.svg | 1 + resources-icons-26/drawables.xml | 4 +++- resources-icons-26/menu.svg | 1 + resources-icons-26/tap.svg | 1 + resources-icons-28/drawables.xml | 4 +++- resources-icons-28/menu.svg | 1 + resources-icons-28/tap.svg | 1 + resources-icons-30/drawables.xml | 4 +++- resources-icons-30/menu.svg | 1 + resources-icons-30/tap.svg | 1 + resources-icons-32/drawables.xml | 4 +++- resources-icons-32/menu.svg | 1 + resources-icons-32/tap.svg | 1 + resources-icons-38/drawables.xml | 4 +++- resources-icons-38/menu.svg | 1 + resources-icons-38/tap.svg | 1 + resources-icons-42/drawables.xml | 4 +++- resources-icons-42/menu.svg | 1 + resources-icons-42/tap.svg | 1 + resources-icons-46/drawables.xml | 4 +++- resources-icons-46/menu.svg | 1 + resources-icons-46/tap.svg | 1 + resources-icons-48/drawables.xml | 4 +++- resources-icons-48/menu.svg | 1 + resources-icons-48/tap.svg | 1 + resources-icons-53/drawables.xml | 4 +++- resources-icons-53/menu.svg | 1 + resources-icons-53/tap.svg | 1 + source/HomeAssistantView.mc | 2 +- source/HomeAssistantViewIconMenuItem.mc | 2 +- 38 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 resources-icons-18/menu.svg create mode 100644 resources-icons-18/tap.svg create mode 100644 resources-icons-21/menu.svg create mode 100644 resources-icons-21/tap.svg create mode 100644 resources-icons-24/menu.svg create mode 100644 resources-icons-24/tap.svg create mode 100644 resources-icons-26/menu.svg create mode 100644 resources-icons-26/tap.svg create mode 100644 resources-icons-28/menu.svg create mode 100644 resources-icons-28/tap.svg create mode 100644 resources-icons-30/menu.svg create mode 100644 resources-icons-30/tap.svg create mode 100644 resources-icons-32/menu.svg create mode 100644 resources-icons-32/tap.svg create mode 100644 resources-icons-38/menu.svg create mode 100644 resources-icons-38/tap.svg create mode 100644 resources-icons-42/menu.svg create mode 100644 resources-icons-42/tap.svg create mode 100644 resources-icons-46/menu.svg create mode 100644 resources-icons-46/tap.svg create mode 100644 resources-icons-48/menu.svg create mode 100644 resources-icons-48/tap.svg create mode 100644 resources-icons-53/menu.svg create mode 100644 resources-icons-53/tap.svg diff --git a/resources-icons-18/drawables.xml b/resources-icons-18/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-18/drawables.xml +++ b/resources-icons-18/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-18/menu.svg b/resources-icons-18/menu.svg new file mode 100644 index 0000000..64c4a2b --- /dev/null +++ b/resources-icons-18/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-18/tap.svg b/resources-icons-18/tap.svg new file mode 100644 index 0000000..6d52a9a --- /dev/null +++ b/resources-icons-18/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-21/drawables.xml b/resources-icons-21/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-21/drawables.xml +++ b/resources-icons-21/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-21/menu.svg b/resources-icons-21/menu.svg new file mode 100644 index 0000000..24eaba1 --- /dev/null +++ b/resources-icons-21/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-21/tap.svg b/resources-icons-21/tap.svg new file mode 100644 index 0000000..2fcd199 --- /dev/null +++ b/resources-icons-21/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-24/drawables.xml b/resources-icons-24/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-24/drawables.xml +++ b/resources-icons-24/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-24/menu.svg b/resources-icons-24/menu.svg new file mode 100644 index 0000000..8cb36fe --- /dev/null +++ b/resources-icons-24/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-24/tap.svg b/resources-icons-24/tap.svg new file mode 100644 index 0000000..419d5b6 --- /dev/null +++ b/resources-icons-24/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-26/drawables.xml b/resources-icons-26/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-26/drawables.xml +++ b/resources-icons-26/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-26/menu.svg b/resources-icons-26/menu.svg new file mode 100644 index 0000000..46fb466 --- /dev/null +++ b/resources-icons-26/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-26/tap.svg b/resources-icons-26/tap.svg new file mode 100644 index 0000000..7e87966 --- /dev/null +++ b/resources-icons-26/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-28/drawables.xml b/resources-icons-28/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-28/drawables.xml +++ b/resources-icons-28/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-28/menu.svg b/resources-icons-28/menu.svg new file mode 100644 index 0000000..e73cac4 --- /dev/null +++ b/resources-icons-28/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-28/tap.svg b/resources-icons-28/tap.svg new file mode 100644 index 0000000..b4414e8 --- /dev/null +++ b/resources-icons-28/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-30/drawables.xml b/resources-icons-30/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-30/drawables.xml +++ b/resources-icons-30/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-30/menu.svg b/resources-icons-30/menu.svg new file mode 100644 index 0000000..85193e9 --- /dev/null +++ b/resources-icons-30/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-30/tap.svg b/resources-icons-30/tap.svg new file mode 100644 index 0000000..1cfa7a7 --- /dev/null +++ b/resources-icons-30/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-32/drawables.xml b/resources-icons-32/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-32/drawables.xml +++ b/resources-icons-32/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-32/menu.svg b/resources-icons-32/menu.svg new file mode 100644 index 0000000..bcf9830 --- /dev/null +++ b/resources-icons-32/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-32/tap.svg b/resources-icons-32/tap.svg new file mode 100644 index 0000000..41439e7 --- /dev/null +++ b/resources-icons-32/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-38/drawables.xml b/resources-icons-38/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-38/drawables.xml +++ b/resources-icons-38/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-38/menu.svg b/resources-icons-38/menu.svg new file mode 100644 index 0000000..c2457d1 --- /dev/null +++ b/resources-icons-38/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-38/tap.svg b/resources-icons-38/tap.svg new file mode 100644 index 0000000..e5f0083 --- /dev/null +++ b/resources-icons-38/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-42/drawables.xml b/resources-icons-42/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-42/drawables.xml +++ b/resources-icons-42/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-42/menu.svg b/resources-icons-42/menu.svg new file mode 100644 index 0000000..5c3e26b --- /dev/null +++ b/resources-icons-42/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-42/tap.svg b/resources-icons-42/tap.svg new file mode 100644 index 0000000..14bdc61 --- /dev/null +++ b/resources-icons-42/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-46/drawables.xml b/resources-icons-46/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-46/drawables.xml +++ b/resources-icons-46/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-46/menu.svg b/resources-icons-46/menu.svg new file mode 100644 index 0000000..ce93b85 --- /dev/null +++ b/resources-icons-46/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-46/tap.svg b/resources-icons-46/tap.svg new file mode 100644 index 0000000..c08e8b8 --- /dev/null +++ b/resources-icons-46/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-48/drawables.xml b/resources-icons-48/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-48/drawables.xml +++ b/resources-icons-48/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-48/menu.svg b/resources-icons-48/menu.svg new file mode 100644 index 0000000..d51eb4d --- /dev/null +++ b/resources-icons-48/menu.svg @@ -0,0 +1 @@ + diff --git a/resources-icons-48/tap.svg b/resources-icons-48/tap.svg new file mode 100644 index 0000000..e59004b --- /dev/null +++ b/resources-icons-48/tap.svg @@ -0,0 +1 @@ + diff --git a/resources-icons-53/drawables.xml b/resources-icons-53/drawables.xml index 320d05b..3d491a1 100644 --- a/resources-icons-53/drawables.xml +++ b/resources-icons-53/drawables.xml @@ -17,4 +17,6 @@ - \ No newline at end of file + + + diff --git a/resources-icons-53/menu.svg b/resources-icons-53/menu.svg new file mode 100644 index 0000000..da15674 --- /dev/null +++ b/resources-icons-53/menu.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources-icons-53/tap.svg b/resources-icons-53/tap.svg new file mode 100644 index 0000000..1fb7d45 --- /dev/null +++ b/resources-icons-53/tap.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc index 4c26d4b..60bf135 100644 --- a/source/HomeAssistantView.mc +++ b/source/HomeAssistantView.mc @@ -80,7 +80,7 @@ class HomeAssistantView extends WatchUi.Menu2 { if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == true){ var icon = new WatchUi.Bitmap({ - :rezId=>Rez.Drawables.ErrorIcon + :rezId=>Rez.Drawables.TapIcon }); var alignement = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT}; diff --git a/source/HomeAssistantViewIconMenuItem.mc b/source/HomeAssistantViewIconMenuItem.mc index f2156b3..63333b8 100644 --- a/source/HomeAssistantViewIconMenuItem.mc +++ b/source/HomeAssistantViewIconMenuItem.mc @@ -29,7 +29,7 @@ class HomeAssistantViewIconMenuItem extends WatchUi.IconMenuItem { var identifier = definition.get("entity") as Lang.String; var icon = new WatchUi.Bitmap({ - :rezId=>Rez.Drawables.LauncherIcon, + :rezId=>Rez.Drawables.MenuIcon, :locX=>WatchUi.LAYOUT_HALIGN_CENTER, :locY=>WatchUi.LAYOUT_VALIGN_CENTER }); From 081a41737f487308c775125ac5030311624ecf41 Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Fri, 17 Nov 2023 00:57:54 +0100 Subject: [PATCH 03/14] Bugfix --- source/HomeAssistantView.mc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc index 60bf135..df0a826 100644 --- a/source/HomeAssistantView.mc +++ b/source/HomeAssistantView.mc @@ -129,7 +129,7 @@ class HomeAssistantView extends WatchUi.Menu2 { fullList.addAll(lmi[i].getMenuView().getItemsToUpdate()); } - var limi = mListMenuItems as Lang.Array; + var limi = mListIconMenuItems as Lang.Array; for(var i = 0; i < limi.size(); i++) { fullList.addAll(limi[i].getMenuView().getItemsToUpdate()); } From d93aa78686af140a85fe8413f4753000eaa75f18 Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Fri, 17 Nov 2023 01:29:57 +0100 Subject: [PATCH 04/14] Refactored MenuItems creation --- source/HomeAssistantMenuItemFactory.mc | 105 ++++++++++++++++++++++++ source/HomeAssistantView.mc | 78 +++--------------- source/HomeAssistantViewIconMenuItem.mc | 4 +- 3 files changed, 120 insertions(+), 67 deletions(-) create mode 100644 source/HomeAssistantMenuItemFactory.mc diff --git a/source/HomeAssistantMenuItemFactory.mc b/source/HomeAssistantMenuItemFactory.mc new file mode 100644 index 0000000..5703829 --- /dev/null +++ b/source/HomeAssistantMenuItemFactory.mc @@ -0,0 +1,105 @@ +//----------------------------------------------------------------------------------- +// +// Distributed under MIT Licence +// See https://github.com/house-of-abbey/GarminHomeAssistant/blob/main/LICENSE. +// +//----------------------------------------------------------------------------------- +// +// GarminHomeAssistant is a Garmin IQ application written in Monkey C and routinely +// tested on a Venu 2 device. The source code is provided at: +// https://github.com/house-of-abbey/GarminHomeAssistant. +// +// P A Abbey & J D Abbey & SomeoneOnEarth, 17 November 2023 +// +// +// Description: +// +// MenuItems Factory. +// +//----------------------------------------------------------------------------------- + +using Toybox.Application; +using Toybox.Lang; +using Toybox.WatchUi; + +class HomeAssistantMenuItemFactory { + + private var mRightLabelAlignement; + private var mLabelToggle; + private var strMenuItemTap; + private var bLeanDesign; + + private var mTapIcon; + + private static var instance; + + private function initialize() { + mLabelToggle = { + :enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String, + :disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String + }; + + bLeanDesign = Application.Properties.getValue("lean_ui") as Lang.Boolean; + + mRightLabelAlignement = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT}; + + strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap); + mTapIcon = new WatchUi.Bitmap({ + :rezId=>$.Rez.Drawables.TapIcon + }); + + } + + static function create() { + if (instance == null) { + instance = new HomeAssistantMenuItemFactory(); + } + return instance; + } + + function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem{ + var subLabel = null; + + if (bLeanDesign == false){ + subLabel=mLabelToggle; + } + + return new HomeAssistantToggleMenuItem( + label, + subLabel, + identifier, + false, + null + ); + } + + function tap(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null, service as Lang.String or Null) as WatchUi.MenuItem{ + if (bLeanDesign) { + return new HomeAssistantIconMenuItem( + label, + null, + identifier, + service, + mTapIcon, + mRightLabelAlignement + ); + + } else { + return new HomeAssistantMenuItem( + label, + strMenuItemTap, + identifier, + service, + null + ); + } + } + + function group(definition as Lang.Dictionary) as WatchUi.MenuItem{ + if (bLeanDesign) { + return new HomeAssistantViewIconMenuItem(definition); + } else { + return new HomeAssistantViewMenuItem(definition); + } + } +} diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc index df0a826..a2c9ead 100644 --- a/source/HomeAssistantView.mc +++ b/source/HomeAssistantView.mc @@ -28,7 +28,6 @@ class HomeAssistantView extends WatchUi.Menu2 { // List of items that need to have their status updated periodically hidden var mListToggleItems = []; hidden var mListMenuItems = []; - hidden var mListIconMenuItems = []; function initialize( definition as Lang.Dictionary, @@ -38,17 +37,6 @@ class HomeAssistantView extends WatchUi.Menu2 { :theme as WatchUi.MenuTheme or Null } or Null ) { - - var toggle_obj = null; - var strMenuItemTap = null; - - if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == false){ - toggle_obj = { - :enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String, - :disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String - }; - strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap); - } if (options == null) { options = { @@ -67,55 +55,15 @@ class HomeAssistantView extends WatchUi.Menu2 { var service = items[i].get("service") as Lang.String or Null; if (type != null && name != null && entity != null) { if (type.equals("toggle")) { - var item = new HomeAssistantToggleMenuItem( - name, - toggle_obj, - entity, - false, - null - ); + var item = HomeAssistantMenuItemFactory.create().toggle(name, entity); addItem(item); mListToggleItems.add(item); } else if (type.equals("tap") && service != null) { - if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == true){ - - var icon = new WatchUi.Bitmap({ - :rezId=>Rez.Drawables.TapIcon - }); - - var alignement = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT}; - - addItem( - new HomeAssistantIconMenuItem( - name, - strMenuItemTap, - entity, - service, - icon, - alignement - ) - ); - } else { - addItem( - new HomeAssistantMenuItem( - name, - strMenuItemTap, - entity, - service, - null - ) - ); - } + addItem( HomeAssistantMenuItemFactory.create().tap(name, entity, service)); } else if (type.equals("group")) { - if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == true){ - var item = new HomeAssistantViewIconMenuItem(items[i]); - addItem(item); - mListIconMenuItems.add(item); - } else { - var item = new HomeAssistantViewMenuItem(items[i]); - addItem(item); - mListMenuItems.add(item); - } + var item = HomeAssistantMenuItemFactory.create().group(items[i]); + addItem(item); + mListMenuItems.add(item); } } } @@ -124,14 +72,14 @@ class HomeAssistantView extends WatchUi.Menu2 { function getItemsToUpdate() as Lang.Array { var fullList = []; - var lmi = mListMenuItems as Lang.Array; - for(var i = 0; i < lmi.size(); i++) { - fullList.addAll(lmi[i].getMenuView().getItemsToUpdate()); - } - - var limi = mListIconMenuItems as Lang.Array; - for(var i = 0; i < limi.size(); i++) { - fullList.addAll(limi[i].getMenuView().getItemsToUpdate()); + var lmi = mListMenuItems as Lang.Array; + for(var i = 0; i < mListMenuItems.size(); i++) { + var item = lmi[i]; + if (item instanceof HomeAssistantViewMenuItem) { + fullList.addAll(item.getMenuView().getItemsToUpdate()); + } else if (item instanceof HomeAssistantViewIconMenuItem) { + fullList.addAll(item.getMenuView().getItemsToUpdate()); + } } return fullList.addAll(mListToggleItems); diff --git a/source/HomeAssistantViewIconMenuItem.mc b/source/HomeAssistantViewIconMenuItem.mc index 63333b8..094055d 100644 --- a/source/HomeAssistantViewIconMenuItem.mc +++ b/source/HomeAssistantViewIconMenuItem.mc @@ -9,12 +9,12 @@ // tested on a Venu 2 device. The source code is provided at: // https://github.com/house-of-abbey/GarminHomeAssistant. // -// P A Abbey & J D Abbey, 31 October 2023 +// P A Abbey & J D Abbey & SomeoneOnEarth, 16 November 2023 // // // Description: // -// Menu button that opens a sub-menu. +// Menu button with an icon that opens a sub-menu. // //----------------------------------------------------------------------------------- From 7bd3486724ad49b16e358117ac9725b69a454ba7 Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Fri, 17 Nov 2023 01:41:57 +0100 Subject: [PATCH 05/14] Refactored icon submenu creation --- source/HomeAssistantMenuItemFactory.mc | 12 +++++++++++- source/HomeAssistantViewIconMenuItem.mc | 16 +++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/source/HomeAssistantMenuItemFactory.mc b/source/HomeAssistantMenuItemFactory.mc index 5703829..bc3ba19 100644 --- a/source/HomeAssistantMenuItemFactory.mc +++ b/source/HomeAssistantMenuItemFactory.mc @@ -31,6 +31,8 @@ class HomeAssistantMenuItemFactory { private var mTapIcon; + private var mMenuIcon; + private static var instance; private function initialize() { @@ -47,6 +49,14 @@ class HomeAssistantMenuItemFactory { mTapIcon = new WatchUi.Bitmap({ :rezId=>$.Rez.Drawables.TapIcon }); + + mMenuIcon = new WatchUi.Bitmap({ + :rezId=>Rez.Drawables.MenuIcon, + :locX=>WatchUi.LAYOUT_HALIGN_CENTER, + :locY=>WatchUi.LAYOUT_VALIGN_CENTER + }); + + } @@ -97,7 +107,7 @@ class HomeAssistantMenuItemFactory { function group(definition as Lang.Dictionary) as WatchUi.MenuItem{ if (bLeanDesign) { - return new HomeAssistantViewIconMenuItem(definition); + return new HomeAssistantViewIconMenuItem(definition, mMenuIcon, mRightLabelAlignement); } else { return new HomeAssistantViewMenuItem(definition); } diff --git a/source/HomeAssistantViewIconMenuItem.mc b/source/HomeAssistantViewIconMenuItem.mc index 094055d..a46e4c8 100644 --- a/source/HomeAssistantViewIconMenuItem.mc +++ b/source/HomeAssistantViewIconMenuItem.mc @@ -24,24 +24,18 @@ using Toybox.WatchUi; class HomeAssistantViewIconMenuItem extends WatchUi.IconMenuItem { hidden var mMenu as HomeAssistantView; - function initialize(definition as Lang.Dictionary) { + function initialize(definition as Lang.Dictionary, icon as WatchUi.Drawable, options as { + :alignment as WatchUi.MenuItem.Alignment + } or Null) { var label = definition.get("name") as Lang.String; var identifier = definition.get("entity") as Lang.String; - - var icon = new WatchUi.Bitmap({ - :rezId=>Rez.Drawables.MenuIcon, - :locX=>WatchUi.LAYOUT_HALIGN_CENTER, - :locY=>WatchUi.LAYOUT_VALIGN_CENTER - }); - - var alignement = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT}; - + WatchUi.IconMenuItem.initialize( label, null, identifier, icon, - alignement + options ); mMenu = new HomeAssistantView(definition, null); From eab2af72f8eec920b99d7470fa94a4a5a0cd1dce Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Fri, 17 Nov 2023 20:15:55 +0100 Subject: [PATCH 06/14] Added app setting for left - right menu alignment --- resources/settings/properties.xml | 2 ++ resources/settings/settings.xml | 9 +++++++++ source/HomeAssistantMenuItemFactory.mc | 26 ++++++++++++++++---------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/resources/settings/properties.xml b/resources/settings/properties.xml index 0829069..abfa39e 100644 --- a/resources/settings/properties.xml +++ b/resources/settings/properties.xml @@ -25,4 +25,6 @@ + + diff --git a/resources/settings/settings.xml b/resources/settings/settings.xml index 1b0ef4a..b5be02c 100644 --- a/resources/settings/settings.xml +++ b/resources/settings/settings.xml @@ -51,4 +51,13 @@ type="boolean" /> + + + + diff --git a/source/HomeAssistantMenuItemFactory.mc b/source/HomeAssistantMenuItemFactory.mc index bc3ba19..2f8f7a6 100644 --- a/source/HomeAssistantMenuItemFactory.mc +++ b/source/HomeAssistantMenuItemFactory.mc @@ -24,7 +24,7 @@ using Toybox.WatchUi; class HomeAssistantMenuItemFactory { - private var mRightLabelAlignement; + private var mMenuItemAlignment; private var mLabelToggle; private var strMenuItemTap; private var bLeanDesign; @@ -43,11 +43,20 @@ class HomeAssistantMenuItemFactory { bLeanDesign = Application.Properties.getValue("lean_ui") as Lang.Boolean; - mRightLabelAlignement = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT}; + var menuItemAlignment = Application.Properties.getValue("menu_alignment") as Lang.Boolean; + + if(menuItemAlignment == true){ + mMenuItemAlignment = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT}; + } else { + mMenuItemAlignment = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT}; + } + strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap); mTapIcon = new WatchUi.Bitmap({ - :rezId=>$.Rez.Drawables.TapIcon + :rezId=>$.Rez.Drawables.TapIcon, + :locX=>WatchUi.LAYOUT_HALIGN_CENTER, + :locY=>WatchUi.LAYOUT_VALIGN_CENTER }); mMenuIcon = new WatchUi.Bitmap({ @@ -55,9 +64,6 @@ class HomeAssistantMenuItemFactory { :locX=>WatchUi.LAYOUT_HALIGN_CENTER, :locY=>WatchUi.LAYOUT_VALIGN_CENTER }); - - - } static function create() { @@ -79,7 +85,7 @@ class HomeAssistantMenuItemFactory { subLabel, identifier, false, - null + mMenuItemAlignment ); } @@ -91,7 +97,7 @@ class HomeAssistantMenuItemFactory { identifier, service, mTapIcon, - mRightLabelAlignement + mMenuItemAlignment ); } else { @@ -100,14 +106,14 @@ class HomeAssistantMenuItemFactory { strMenuItemTap, identifier, service, - null + mMenuItemAlignment ); } } function group(definition as Lang.Dictionary) as WatchUi.MenuItem{ if (bLeanDesign) { - return new HomeAssistantViewIconMenuItem(definition, mMenuIcon, mRightLabelAlignement); + return new HomeAssistantViewIconMenuItem(definition, mMenuIcon, mMenuItemAlignment); } else { return new HomeAssistantViewMenuItem(definition); } From 5f8ffdce5ec6ed40d895fac3229682630cd5b755 Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Fri, 17 Nov 2023 20:57:46 +0100 Subject: [PATCH 07/14] Changed color of tap and menu icon --- resources-icons-18/menu.svg | 2 +- resources-icons-18/tap.svg | 2 +- resources-icons-21/menu.svg | 2 +- resources-icons-21/tap.svg | 2 +- resources-icons-24/menu.svg | 2 +- resources-icons-24/tap.svg | 2 +- resources-icons-26/menu.svg | 2 +- resources-icons-26/tap.svg | 2 +- resources-icons-28/menu.svg | 2 +- resources-icons-28/tap.svg | 2 +- resources-icons-30/menu.svg | 2 +- resources-icons-30/tap.svg | 2 +- resources-icons-32/menu.svg | 2 +- resources-icons-32/tap.svg | 2 +- resources-icons-38/menu.svg | 2 +- resources-icons-38/tap.svg | 2 +- resources-icons-42/menu.svg | 2 +- resources-icons-42/tap.svg | 2 +- resources-icons-46/menu.svg | 2 +- resources-icons-46/tap.svg | 2 +- resources-icons-48/menu.svg | 2 +- resources-icons-48/tap.svg | 2 +- resources-icons-53/menu.svg | 2 +- resources-icons-53/tap.svg | 2 +- 24 files changed, 24 insertions(+), 24 deletions(-) diff --git a/resources-icons-18/menu.svg b/resources-icons-18/menu.svg index 64c4a2b..112af83 100644 --- a/resources-icons-18/menu.svg +++ b/resources-icons-18/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-18/tap.svg b/resources-icons-18/tap.svg index 6d52a9a..f1a52ab 100644 --- a/resources-icons-18/tap.svg +++ b/resources-icons-18/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-21/menu.svg b/resources-icons-21/menu.svg index 24eaba1..df98500 100644 --- a/resources-icons-21/menu.svg +++ b/resources-icons-21/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-21/tap.svg b/resources-icons-21/tap.svg index 2fcd199..ce69b53 100644 --- a/resources-icons-21/tap.svg +++ b/resources-icons-21/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-24/menu.svg b/resources-icons-24/menu.svg index 8cb36fe..c00a567 100644 --- a/resources-icons-24/menu.svg +++ b/resources-icons-24/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-24/tap.svg b/resources-icons-24/tap.svg index 419d5b6..bf3d3d7 100644 --- a/resources-icons-24/tap.svg +++ b/resources-icons-24/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-26/menu.svg b/resources-icons-26/menu.svg index 46fb466..42cd60b 100644 --- a/resources-icons-26/menu.svg +++ b/resources-icons-26/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-26/tap.svg b/resources-icons-26/tap.svg index 7e87966..87c41ad 100644 --- a/resources-icons-26/tap.svg +++ b/resources-icons-26/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-28/menu.svg b/resources-icons-28/menu.svg index e73cac4..6275c28 100644 --- a/resources-icons-28/menu.svg +++ b/resources-icons-28/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-28/tap.svg b/resources-icons-28/tap.svg index b4414e8..64506f8 100644 --- a/resources-icons-28/tap.svg +++ b/resources-icons-28/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-30/menu.svg b/resources-icons-30/menu.svg index 85193e9..0011419 100644 --- a/resources-icons-30/menu.svg +++ b/resources-icons-30/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-30/tap.svg b/resources-icons-30/tap.svg index 1cfa7a7..70f99b8 100644 --- a/resources-icons-30/tap.svg +++ b/resources-icons-30/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-32/menu.svg b/resources-icons-32/menu.svg index bcf9830..ba31d0d 100644 --- a/resources-icons-32/menu.svg +++ b/resources-icons-32/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-32/tap.svg b/resources-icons-32/tap.svg index 41439e7..5137c9f 100644 --- a/resources-icons-32/tap.svg +++ b/resources-icons-32/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-38/menu.svg b/resources-icons-38/menu.svg index c2457d1..463d1f3 100644 --- a/resources-icons-38/menu.svg +++ b/resources-icons-38/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-38/tap.svg b/resources-icons-38/tap.svg index e5f0083..4473be2 100644 --- a/resources-icons-38/tap.svg +++ b/resources-icons-38/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-42/menu.svg b/resources-icons-42/menu.svg index 5c3e26b..2ee5924 100644 --- a/resources-icons-42/menu.svg +++ b/resources-icons-42/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-42/tap.svg b/resources-icons-42/tap.svg index 14bdc61..03f4f0e 100644 --- a/resources-icons-42/tap.svg +++ b/resources-icons-42/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-46/menu.svg b/resources-icons-46/menu.svg index ce93b85..6972e86 100644 --- a/resources-icons-46/menu.svg +++ b/resources-icons-46/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-46/tap.svg b/resources-icons-46/tap.svg index c08e8b8..b95b22d 100644 --- a/resources-icons-46/tap.svg +++ b/resources-icons-46/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-48/menu.svg b/resources-icons-48/menu.svg index d51eb4d..3330af1 100644 --- a/resources-icons-48/menu.svg +++ b/resources-icons-48/menu.svg @@ -1 +1 @@ - + diff --git a/resources-icons-48/tap.svg b/resources-icons-48/tap.svg index e59004b..35d175c 100644 --- a/resources-icons-48/tap.svg +++ b/resources-icons-48/tap.svg @@ -1 +1 @@ - + diff --git a/resources-icons-53/menu.svg b/resources-icons-53/menu.svg index da15674..15f81e0 100644 --- a/resources-icons-53/menu.svg +++ b/resources-icons-53/menu.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/resources-icons-53/tap.svg b/resources-icons-53/tap.svg index 1fb7d45..2dad00c 100644 --- a/resources-icons-53/tap.svg +++ b/resources-icons-53/tap.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file From 604294ae0b55b7f1e10581bca691d2405e492d7d Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Sun, 19 Nov 2023 01:29:57 +0100 Subject: [PATCH 08/14] Corrected log strings --- source/HomeAssistantApp.mc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/HomeAssistantApp.mc b/source/HomeAssistantApp.mc index 16f4461..0b63903 100644 --- a/source/HomeAssistantApp.mc +++ b/source/HomeAssistantApp.mc @@ -67,22 +67,22 @@ class HomeAssistantApp extends Application.AppBase { if ((Properties.getValue("api_key") as Lang.String).length() == 0) { if (Globals.scDebug) { - System.println("HomeAssistantMenuItem execScript(): No API key in the application settings."); + System.println("HomeAssistantApp getInitialView(): No API key in the application settings."); } return [new ErrorView(strNoApiKey + "."), new ErrorDelegate()] as Lang.Array; } else if (api_url.length() == 0) { if (Globals.scDebug) { - System.println("HomeAssistantMenuItem execScript(): No API URL in the application settings."); + System.println("HomeAssistantApp getInitialView(): No API URL in the application settings."); } return [new ErrorView(strNoApiUrl + "."), new ErrorDelegate()] as Lang.Array; } else if (api_url.substring(-1, api_url.length()).equals("/")) { if (Globals.scDebug) { - System.println("HomeAssistantMenuItem execScript(): API URL must not have a trailing slash '/'."); + System.println("HomeAssistantApp getInitialView(): API URL must not have a trailing slash '/'."); } return [new ErrorView(strTrailingSlashErr + "."), new ErrorDelegate()] as Lang.Array; } else if ((Properties.getValue("config_url") as Lang.String).length() == 0) { if (Globals.scDebug) { - System.println("HomeAssistantMenuItem execScript(): No configuration URL in the application settings."); + System.println("HomeAssistantApp getInitialView(): No configuration URL in the application settings."); } return [new ErrorView(strNoConfigUrl + "."), new ErrorDelegate()] as Lang.Array; } else if (! System.getDeviceSettings().phoneConnected) { From ab995db5baff71e6482f1eda33b4af45ca70ea09 Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Sun, 19 Nov 2023 01:32:25 +0100 Subject: [PATCH 09/14] Removed redundant code from spike --- source/HomeAssistantIconMenuItem.mc | 122 +------------------- source/HomeAssistantMenuItem.mc | 139 +--------------------- source/HomeAssistantService.mc | 172 ++++++++++++++++++++++++++++ source/HomeAssistantView.mc | 4 +- 4 files changed, 185 insertions(+), 252 deletions(-) create mode 100644 source/HomeAssistantService.mc diff --git a/source/HomeAssistantIconMenuItem.mc b/source/HomeAssistantIconMenuItem.mc index 853cd1b..2e74d06 100644 --- a/source/HomeAssistantIconMenuItem.mc +++ b/source/HomeAssistantIconMenuItem.mc @@ -24,12 +24,7 @@ using Toybox.Graphics; using Toybox.Application.Properties; class HomeAssistantIconMenuItem extends WatchUi.IconMenuItem { - hidden var mApiKey as Lang.String; - hidden var strNoInternet as Lang.String; - hidden var strApiFlood as Lang.String; - hidden var strApiUrlNotFound as Lang.String; - hidden var strUnhandledHttpErr as Lang.String; - hidden var mService as Lang.String; + hidden var mHomeAssistantService as HomeAssistantService; function initialize( label as Lang.String or Lang.Symbol, @@ -41,12 +36,6 @@ class HomeAssistantIconMenuItem extends WatchUi.IconMenuItem { :alignment as WatchUi.MenuItem.Alignment } or Null ) { - strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet); - strApiFlood = WatchUi.loadResource($.Rez.Strings.ApiFlood); - strApiUrlNotFound = WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound); - strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr); - mApiKey = Properties.getValue("api_key"); - mService = service; WatchUi.IconMenuItem.initialize( label, subLabel, @@ -54,112 +43,13 @@ class HomeAssistantIconMenuItem extends WatchUi.IconMenuItem { icon, options ); + + mHomeAssistantService = new HomeAssistantService(service, identifier); + } - // Callback function after completing the POST request to call a script. - // - function onReturnExecScript(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { - if (Globals.scDebug) { - System.println("HomeAssistantIconMenuItem onReturnExecScript() Response Code: " + responseCode); - System.println("HomeAssistantIconMenuItem onReturnExecScript() Response Data: " + data); - } - if (responseCode == Communications.BLE_QUEUE_FULL) { - if (Globals.scDebug) { - System.println("HomeAssistantIconMenuItem onReturnExecScript() Response Code: BLE_QUEUE_FULL, API calls too rapid."); - } - var cw = WatchUi.getCurrentView(); - if (!(cw[0] instanceof ErrorView)) { - // Avoid pushing multiple ErrorViews - WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP); - } - } else if (responseCode == 404) { - if (Globals.scDebug) { - System.println("HomeAssistantIconMenuItem onReturnExecScript() Response Code: 404, page not found. Check API URL setting."); - } - WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP); - } else if (responseCode == 200) { - if (Globals.scDebug) { - System.println("HomeAssistantIconMenuItem onReturnExecScript(): Service executed."); - } - var d = data as Lang.Array; - var toast = "Executed"; - for(var i = 0; i < d.size(); i++) { - if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) { - toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String; - } - } - if (WatchUi has :showToast) { - WatchUi.showToast(toast, null); - } else { - new Alert({ - :timeout => Globals.scAlertTimeout, - :font => Graphics.FONT_MEDIUM, - :text => toast, - :fgcolor => Graphics.COLOR_WHITE, - :bgcolor => Graphics.COLOR_BLACK - }).pushView(WatchUi.SLIDE_IMMEDIATE); - } - } else { - if (Globals.scDebug) { - System.println("HomeAssistantIconMenuItem onReturnExecScript(): Unhandled HTTP response code = " + responseCode); - } - WatchUi.pushView(new ErrorView(strUnhandledHttpErr + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP); - } - } - - function execScript() as Void { - var options = { - :method => Communications.HTTP_REQUEST_METHOD_POST, - :headers => { - "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, - "Authorization" => "Bearer " + mApiKey - }, - :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON - }; - if (System.getDeviceSettings().phoneConnected && System.getDeviceSettings().connectionAvailable) { - // Updated SDK and got a new error - // ERROR: venu: Cannot find symbol ':substring' on type 'PolyType'. - var id = mIdentifier as Lang.String; - if (mService == null) { - var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + id.substring(0, id.find(".")) + "/" + id.substring(id.find(".")+1, null); - if (Globals.scDebug) { - System.println("HomeAssistantIconMenuItem execScript() URL=" + url); - System.println("HomeAssistantIconMenuItem execScript() mIdentifier=" + mIdentifier); - } - Communications.makeWebRequest( - url, - null, - options, - method(:onReturnExecScript) - ); - } else { - var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null); - if (Globals.scDebug) { - System.println("HomeAssistantIconMenuItem execScript() URL=" + url); - System.println("HomeAssistantIconMenuItem execScript() mService=" + mService); - } - Communications.makeWebRequest( - url, - { - "entity_id" => id - }, - options, - method(:onReturnExecScript) - ); - } - if (Attention has :vibrate) { - Attention.vibrate([ - new Attention.VibeProfile(50, 100), // On for 100ms - new Attention.VibeProfile( 0, 100), // Off for 100ms - new Attention.VibeProfile(50, 100) // On for 100ms - ]); - } - } else { - if (Globals.scDebug) { - System.println("HomeAssistantIconMenuItem execScript(): No Internet connection, skipping API call."); - } - WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); - } + function callService() as Void { + mHomeAssistantService.call(); } } diff --git a/source/HomeAssistantMenuItem.mc b/source/HomeAssistantMenuItem.mc index ddb3d63..64c39ed 100644 --- a/source/HomeAssistantMenuItem.mc +++ b/source/HomeAssistantMenuItem.mc @@ -24,14 +24,7 @@ using Toybox.Graphics; using Toybox.Application.Properties; class HomeAssistantMenuItem extends WatchUi.MenuItem { - hidden var mApiKey as Lang.String; - hidden var strNoPhone as Lang.String; - hidden var strNoInternet as Lang.String; - hidden var strNoResponse as Lang.String; - hidden var strApiFlood as Lang.String; - hidden var strApiUrlNotFound as Lang.String; - hidden var strUnhandledHttpErr as Lang.String; - hidden var mService as Lang.String; + hidden var mHomeAssistantService as HomeAssistantService; function initialize( label as Lang.String or Lang.Symbol, @@ -43,140 +36,18 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem { :icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol } or Null ) { - strNoPhone = WatchUi.loadResource($.Rez.Strings.NoPhone); - strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet); - strNoResponse = WatchUi.loadResource($.Rez.Strings.NoResponse); - strApiFlood = WatchUi.loadResource($.Rez.Strings.ApiFlood); - strApiUrlNotFound = WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound); - strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr); - mApiKey = Properties.getValue("api_key"); - mService = service; WatchUi.MenuItem.initialize( label, subLabel, identifier, options ); + + mHomeAssistantService = new HomeAssistantService(service, identifier); } - // Callback function after completing the POST request to call a script. - // - function onReturnExecScript(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: " + responseCode); - System.println("HomeAssistantMenuItem onReturnExecScript() Response Data: " + data); - } - if (responseCode == Communications.BLE_HOST_TIMEOUT || responseCode == Communications.BLE_CONNECTION_UNAVAILABLE) { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed."); - } - WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); - } else if (responseCode == Communications.BLE_QUEUE_FULL) { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: BLE_QUEUE_FULL, API calls too rapid."); - } - if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { - // Avoid pushing multiple ErrorViews - WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP); - } - } else if (responseCode == Communications.NETWORK_REQUEST_TIMED_OUT) { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection."); - } - WatchUi.pushView(new ErrorView(strNoResponse), new ErrorDelegate(), WatchUi.SLIDE_UP); - } else if (responseCode == 404) { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: 404, page not found. Check API URL setting."); - } - WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP); - } else if (responseCode == 200) { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem onReturnExecScript(): Service executed."); - } - var d = data as Lang.Array; - var toast = "Executed"; - for(var i = 0; i < d.size(); i++) { - if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) { - toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String; - } - } - if (WatchUi has :showToast) { - WatchUi.showToast(toast, null); - } else { - new Alert({ - :timeout => Globals.scAlertTimeout, - :font => Graphics.FONT_MEDIUM, - :text => toast, - :fgcolor => Graphics.COLOR_WHITE, - :bgcolor => Graphics.COLOR_BLACK - }).pushView(WatchUi.SLIDE_IMMEDIATE); - } - } else { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem onReturnExecScript(): Unhandled HTTP response code = " + responseCode); - } - WatchUi.pushView(new ErrorView(strUnhandledHttpErr + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP); - } - } - - function execScript() as Void { - var options = { - :method => Communications.HTTP_REQUEST_METHOD_POST, - :headers => { - "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, - "Authorization" => "Bearer " + mApiKey - }, - :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON - }; - if (! System.getDeviceSettings().phoneConnected) { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem execScript(): No Phone connection, skipping API call."); - } - WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); - } else if (! System.getDeviceSettings().connectionAvailable) { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem execScript(): No Internet connection, skipping API call."); - } - WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); - } else { - // Updated SDK and got a new error - // ERROR: venu: Cannot find symbol ':substring' on type 'PolyType'. - var id = mIdentifier as Lang.String; - if (mService == null) { - var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + id.substring(0, id.find(".")) + "/" + id.substring(id.find(".")+1, null); - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem execScript() URL=" + url); - System.println("HomeAssistantMenuItem execScript() mIdentifier=" + mIdentifier); - } - Communications.makeWebRequest( - url, - null, - options, - method(:onReturnExecScript) - ); - } else { - var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null); - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem execScript() URL=" + url); - System.println("HomeAssistantMenuItem execScript() mService=" + mService); - } - Communications.makeWebRequest( - url, - { - "entity_id" => id - }, - options, - method(:onReturnExecScript) - ); - } - if (Attention has :vibrate) { - Attention.vibrate([ - new Attention.VibeProfile(50, 100), // On for 100ms - new Attention.VibeProfile( 0, 100), // Off for 100ms - new Attention.VibeProfile(50, 100) // On for 100ms - ]); - } - } + function callService() as Void { + mHomeAssistantService.call(); } } diff --git a/source/HomeAssistantService.mc b/source/HomeAssistantService.mc new file mode 100644 index 0000000..20999d2 --- /dev/null +++ b/source/HomeAssistantService.mc @@ -0,0 +1,172 @@ +//----------------------------------------------------------------------------------- +// +// Distributed under MIT Licence +// See https://github.com/house-of-abbey/GarminHomeAssistant/blob/main/LICENSE. +// +//----------------------------------------------------------------------------------- +// +// GarminHomeAssistant is a Garmin IQ application written in Monkey C and routinely +// tested on a Venu 2 device. The source code is provided at: +// https://github.com/house-of-abbey/GarminHomeAssistant. +// +// P A Abbey & J D Abbey & SomeoneOnEarth, 19 November 2023 +// +// +// Description: +// +// Calling a Home Assistant Service. +// +//----------------------------------------------------------------------------------- + +using Toybox.Lang; +using Toybox.WatchUi; +using Toybox.Graphics; +using Toybox.Application.Properties; + +class HomeAssistantService{ + hidden var mApiKey as Lang.String; + hidden var strNoPhone as Lang.String; + hidden var strNoInternet as Lang.String; + hidden var strNoResponse as Lang.String; + hidden var strApiFlood as Lang.String; + hidden var strApiUrlNotFound as Lang.String; + hidden var strUnhandledHttpErr as Lang.String; + hidden var mService as Lang.String; + hidden var mIdentifier as Lang.Object; + + function initialize( + service as Lang.String or Null, + identifier as Lang.Object or Null + ) { + strNoPhone = WatchUi.loadResource($.Rez.Strings.NoPhone); + strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet); + strNoResponse = WatchUi.loadResource($.Rez.Strings.NoResponse); + strApiFlood = WatchUi.loadResource($.Rez.Strings.ApiFlood); + strApiUrlNotFound = WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound); + strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr); + mApiKey = Properties.getValue("api_key"); + mService = service; + mIdentifier = identifier; + } + + // Callback function after completing the POST request to call a service. + // + function onReturnCall(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { + if (Globals.scDebug) { + System.println("HomeAssistantService onReturnCall() Response Code: " + responseCode); + System.println("HomeAssistantService onReturnCall() Response Data: " + data); + } + if (responseCode == Communications.BLE_HOST_TIMEOUT || responseCode == Communications.BLE_CONNECTION_UNAVAILABLE) { + if (Globals.scDebug) { + System.println("HomeAssistantService onReturnCall() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed."); + } + WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); + } else if (responseCode == Communications.BLE_QUEUE_FULL) { + if (Globals.scDebug) { + System.println("HomeAssistantService onReturnCall() Response Code: BLE_QUEUE_FULL, API calls too rapid."); + } + if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { + // Avoid pushing multiple ErrorViews + WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP); + } + } else if (responseCode == Communications.NETWORK_REQUEST_TIMED_OUT) { + if (Globals.scDebug) { + System.println("HomeAssistantService onReturnCall() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection."); + } + WatchUi.pushView(new ErrorView(strNoResponse), new ErrorDelegate(), WatchUi.SLIDE_UP); + } else if (responseCode == 404) { + if (Globals.scDebug) { + System.println("HomeAssistantService onReturnCall() Response Code: 404, page not found. Check API URL setting."); + } + WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP); + } else if (responseCode == 200) { + if (Globals.scDebug) { + System.println("HomeAssistantService onReturnCall(): Service executed."); + } + var d = data as Lang.Array; + var toast = "Executed"; + for(var i = 0; i < d.size(); i++) { + if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) { + toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String; + } + } + if (WatchUi has :showToast) { + WatchUi.showToast(toast, null); + } else { + new Alert({ + :timeout => Globals.scAlertTimeout, + :font => Graphics.FONT_MEDIUM, + :text => toast, + :fgcolor => Graphics.COLOR_WHITE, + :bgcolor => Graphics.COLOR_BLACK + }).pushView(WatchUi.SLIDE_IMMEDIATE); + } + } else { + if (Globals.scDebug) { + System.println("HomeAssistantService onReturnCall(): Unhandled HTTP response code = " + responseCode); + } + WatchUi.pushView(new ErrorView(strUnhandledHttpErr + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP); + } + } + + function call() as Void { + var options = { + :method => Communications.HTTP_REQUEST_METHOD_POST, + :headers => { + "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, + "Authorization" => "Bearer " + mApiKey + }, + :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON + }; + if (! System.getDeviceSettings().phoneConnected) { + if (Globals.scDebug) { + System.println("HomeAssistantService call(): No Phone connection, skipping API call."); + } + WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); + } else if (! System.getDeviceSettings().connectionAvailable) { + if (Globals.scDebug) { + System.println("HomeAssistantService call(): No Internet connection, skipping API call."); + } + WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); + } else { + // Updated SDK and got a new error + // ERROR: venu: Cannot find symbol ':substring' on type 'PolyType'. + var id = mIdentifier as Lang.String; + if (mService == null) { + var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + id.substring(0, id.find(".")) + "/" + id.substring(id.find(".")+1, null); + if (Globals.scDebug) { + System.println("HomeAssistantService call() URL=" + url); + System.println("HomeAssistantService call() mIdentifier=" + mIdentifier); + } + Communications.makeWebRequest( + url, + null, + options, + method(:onReturnCall) + ); + } else { + var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null); + if (Globals.scDebug) { + System.println("HomeAssistantService call() URL=" + url); + System.println("HomeAssistantService call() mService=" + mService); + } + Communications.makeWebRequest( + url, + { + "entity_id" => id + }, + options, + method(:onReturnCall) + ); + } + if (Attention has :vibrate) { + Attention.vibrate([ + new Attention.VibeProfile(50, 100), // On for 100ms + new Attention.VibeProfile( 0, 100), // Off for 100ms + new Attention.VibeProfile(50, 100) // On for 100ms + ]); + } + } + } + +} diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc index a2c9ead..7a2318c 100644 --- a/source/HomeAssistantView.mc +++ b/source/HomeAssistantView.mc @@ -113,13 +113,13 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate { if (Globals.scDebug) { System.println(haItem.getLabel() + " " + haItem.getId()); } - haItem.execScript(); + haItem.callService(); } else if (item instanceof HomeAssistantIconMenuItem) { var haItem = item as HomeAssistantIconMenuItem; if (Globals.scDebug) { System.println(haItem.getLabel() + " " + haItem.getId()); } - haItem.execScript(); + haItem.callService(); } else if (item instanceof HomeAssistantViewMenuItem) { var haMenuItem = item as HomeAssistantViewMenuItem; if (Globals.scDebug) { From 440980a96fb47a037ff83fedfb6bb1383f4f33f3 Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Sun, 19 Nov 2023 02:04:23 +0100 Subject: [PATCH 10/14] Aligned naming of menu item types --- resources-icons-18/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-18/{tap.svg => tap_type.svg} | 0 resources-icons-21/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-21/{tap.svg => tap_type.svg} | 0 resources-icons-24/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-24/{tap.svg => tap_type.svg} | 0 resources-icons-26/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-26/{tap.svg => tap_type.svg} | 0 resources-icons-28/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-28/{tap.svg => tap_type.svg} | 0 resources-icons-30/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-30/{tap.svg => tap_type.svg} | 0 resources-icons-32/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-32/{tap.svg => tap_type.svg} | 0 resources-icons-38/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-38/{tap.svg => tap_type.svg} | 0 resources-icons-42/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-42/{tap.svg => tap_type.svg} | 0 resources-icons-46/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-46/{tap.svg => tap_type.svg} | 0 resources-icons-48/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-48/{tap.svg => tap_type.svg} | 0 resources-icons-53/drawables.xml | 4 +-- .../{menu.svg => group_type.svg} | 0 resources-icons-53/{tap.svg => tap_type.svg} | 0 resources/settings/properties.xml | 2 +- resources/settings/settings.xml | 4 +-- source/HomeAssistantMenuItemFactory.mc | 26 +++++++++---------- 39 files changed, 40 insertions(+), 40 deletions(-) rename resources-icons-18/{menu.svg => group_type.svg} (100%) rename resources-icons-18/{tap.svg => tap_type.svg} (100%) rename resources-icons-21/{menu.svg => group_type.svg} (100%) rename resources-icons-21/{tap.svg => tap_type.svg} (100%) rename resources-icons-24/{menu.svg => group_type.svg} (100%) rename resources-icons-24/{tap.svg => tap_type.svg} (100%) rename resources-icons-26/{menu.svg => group_type.svg} (100%) rename resources-icons-26/{tap.svg => tap_type.svg} (100%) rename resources-icons-28/{menu.svg => group_type.svg} (100%) rename resources-icons-28/{tap.svg => tap_type.svg} (100%) rename resources-icons-30/{menu.svg => group_type.svg} (100%) rename resources-icons-30/{tap.svg => tap_type.svg} (100%) rename resources-icons-32/{menu.svg => group_type.svg} (100%) rename resources-icons-32/{tap.svg => tap_type.svg} (100%) rename resources-icons-38/{menu.svg => group_type.svg} (100%) rename resources-icons-38/{tap.svg => tap_type.svg} (100%) rename resources-icons-42/{menu.svg => group_type.svg} (100%) rename resources-icons-42/{tap.svg => tap_type.svg} (100%) rename resources-icons-46/{menu.svg => group_type.svg} (100%) rename resources-icons-46/{tap.svg => tap_type.svg} (100%) rename resources-icons-48/{menu.svg => group_type.svg} (100%) rename resources-icons-48/{tap.svg => tap_type.svg} (100%) rename resources-icons-53/{menu.svg => group_type.svg} (100%) rename resources-icons-53/{tap.svg => tap_type.svg} (100%) diff --git a/resources-icons-18/drawables.xml b/resources-icons-18/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-18/drawables.xml +++ b/resources-icons-18/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-18/menu.svg b/resources-icons-18/group_type.svg similarity index 100% rename from resources-icons-18/menu.svg rename to resources-icons-18/group_type.svg diff --git a/resources-icons-18/tap.svg b/resources-icons-18/tap_type.svg similarity index 100% rename from resources-icons-18/tap.svg rename to resources-icons-18/tap_type.svg diff --git a/resources-icons-21/drawables.xml b/resources-icons-21/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-21/drawables.xml +++ b/resources-icons-21/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-21/menu.svg b/resources-icons-21/group_type.svg similarity index 100% rename from resources-icons-21/menu.svg rename to resources-icons-21/group_type.svg diff --git a/resources-icons-21/tap.svg b/resources-icons-21/tap_type.svg similarity index 100% rename from resources-icons-21/tap.svg rename to resources-icons-21/tap_type.svg diff --git a/resources-icons-24/drawables.xml b/resources-icons-24/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-24/drawables.xml +++ b/resources-icons-24/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-24/menu.svg b/resources-icons-24/group_type.svg similarity index 100% rename from resources-icons-24/menu.svg rename to resources-icons-24/group_type.svg diff --git a/resources-icons-24/tap.svg b/resources-icons-24/tap_type.svg similarity index 100% rename from resources-icons-24/tap.svg rename to resources-icons-24/tap_type.svg diff --git a/resources-icons-26/drawables.xml b/resources-icons-26/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-26/drawables.xml +++ b/resources-icons-26/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-26/menu.svg b/resources-icons-26/group_type.svg similarity index 100% rename from resources-icons-26/menu.svg rename to resources-icons-26/group_type.svg diff --git a/resources-icons-26/tap.svg b/resources-icons-26/tap_type.svg similarity index 100% rename from resources-icons-26/tap.svg rename to resources-icons-26/tap_type.svg diff --git a/resources-icons-28/drawables.xml b/resources-icons-28/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-28/drawables.xml +++ b/resources-icons-28/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-28/menu.svg b/resources-icons-28/group_type.svg similarity index 100% rename from resources-icons-28/menu.svg rename to resources-icons-28/group_type.svg diff --git a/resources-icons-28/tap.svg b/resources-icons-28/tap_type.svg similarity index 100% rename from resources-icons-28/tap.svg rename to resources-icons-28/tap_type.svg diff --git a/resources-icons-30/drawables.xml b/resources-icons-30/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-30/drawables.xml +++ b/resources-icons-30/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-30/menu.svg b/resources-icons-30/group_type.svg similarity index 100% rename from resources-icons-30/menu.svg rename to resources-icons-30/group_type.svg diff --git a/resources-icons-30/tap.svg b/resources-icons-30/tap_type.svg similarity index 100% rename from resources-icons-30/tap.svg rename to resources-icons-30/tap_type.svg diff --git a/resources-icons-32/drawables.xml b/resources-icons-32/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-32/drawables.xml +++ b/resources-icons-32/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-32/menu.svg b/resources-icons-32/group_type.svg similarity index 100% rename from resources-icons-32/menu.svg rename to resources-icons-32/group_type.svg diff --git a/resources-icons-32/tap.svg b/resources-icons-32/tap_type.svg similarity index 100% rename from resources-icons-32/tap.svg rename to resources-icons-32/tap_type.svg diff --git a/resources-icons-38/drawables.xml b/resources-icons-38/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-38/drawables.xml +++ b/resources-icons-38/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-38/menu.svg b/resources-icons-38/group_type.svg similarity index 100% rename from resources-icons-38/menu.svg rename to resources-icons-38/group_type.svg diff --git a/resources-icons-38/tap.svg b/resources-icons-38/tap_type.svg similarity index 100% rename from resources-icons-38/tap.svg rename to resources-icons-38/tap_type.svg diff --git a/resources-icons-42/drawables.xml b/resources-icons-42/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-42/drawables.xml +++ b/resources-icons-42/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-42/menu.svg b/resources-icons-42/group_type.svg similarity index 100% rename from resources-icons-42/menu.svg rename to resources-icons-42/group_type.svg diff --git a/resources-icons-42/tap.svg b/resources-icons-42/tap_type.svg similarity index 100% rename from resources-icons-42/tap.svg rename to resources-icons-42/tap_type.svg diff --git a/resources-icons-46/drawables.xml b/resources-icons-46/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-46/drawables.xml +++ b/resources-icons-46/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-46/menu.svg b/resources-icons-46/group_type.svg similarity index 100% rename from resources-icons-46/menu.svg rename to resources-icons-46/group_type.svg diff --git a/resources-icons-46/tap.svg b/resources-icons-46/tap_type.svg similarity index 100% rename from resources-icons-46/tap.svg rename to resources-icons-46/tap_type.svg diff --git a/resources-icons-48/drawables.xml b/resources-icons-48/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-48/drawables.xml +++ b/resources-icons-48/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-48/menu.svg b/resources-icons-48/group_type.svg similarity index 100% rename from resources-icons-48/menu.svg rename to resources-icons-48/group_type.svg diff --git a/resources-icons-48/tap.svg b/resources-icons-48/tap_type.svg similarity index 100% rename from resources-icons-48/tap.svg rename to resources-icons-48/tap_type.svg diff --git a/resources-icons-53/drawables.xml b/resources-icons-53/drawables.xml index 3d491a1..d560ef6 100644 --- a/resources-icons-53/drawables.xml +++ b/resources-icons-53/drawables.xml @@ -17,6 +17,6 @@ - - + + diff --git a/resources-icons-53/menu.svg b/resources-icons-53/group_type.svg similarity index 100% rename from resources-icons-53/menu.svg rename to resources-icons-53/group_type.svg diff --git a/resources-icons-53/tap.svg b/resources-icons-53/tap_type.svg similarity index 100% rename from resources-icons-53/tap.svg rename to resources-icons-53/tap_type.svg diff --git a/resources/settings/properties.xml b/resources/settings/properties.xml index abfa39e..d3ffc6d 100644 --- a/resources/settings/properties.xml +++ b/resources/settings/properties.xml @@ -24,7 +24,7 @@ - + diff --git a/resources/settings/settings.xml b/resources/settings/settings.xml index b5be02c..f8598ba 100644 --- a/resources/settings/settings.xml +++ b/resources/settings/settings.xml @@ -44,8 +44,8 @@ WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String }; - bLeanDesign = Application.Properties.getValue("lean_ui") as Lang.Boolean; + bRepresentTypesWithIcons = Application.Properties.getValue("types_representation") as Lang.Boolean; var menuItemAlignment = Application.Properties.getValue("menu_alignment") as Lang.Boolean; @@ -53,14 +53,14 @@ class HomeAssistantMenuItemFactory { strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap); - mTapIcon = new WatchUi.Bitmap({ - :rezId=>$.Rez.Drawables.TapIcon, + mTapTypeIcon = new WatchUi.Bitmap({ + :rezId=>$.Rez.Drawables.TapTypeIcon, :locX=>WatchUi.LAYOUT_HALIGN_CENTER, :locY=>WatchUi.LAYOUT_VALIGN_CENTER }); - mMenuIcon = new WatchUi.Bitmap({ - :rezId=>Rez.Drawables.MenuIcon, + mGroupTypeIcon = new WatchUi.Bitmap({ + :rezId=>$.Rez.Drawables.GroupTypeIcon, :locX=>WatchUi.LAYOUT_HALIGN_CENTER, :locY=>WatchUi.LAYOUT_VALIGN_CENTER }); @@ -76,7 +76,7 @@ class HomeAssistantMenuItemFactory { function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem{ var subLabel = null; - if (bLeanDesign == false){ + if (bRepresentTypesWithIcons == false){ subLabel=mLabelToggle; } @@ -90,13 +90,13 @@ class HomeAssistantMenuItemFactory { } function tap(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null, service as Lang.String or Null) as WatchUi.MenuItem{ - if (bLeanDesign) { + if (bRepresentTypesWithIcons) { return new HomeAssistantIconMenuItem( label, null, identifier, service, - mTapIcon, + mTapTypeIcon, mMenuItemAlignment ); @@ -112,8 +112,8 @@ class HomeAssistantMenuItemFactory { } function group(definition as Lang.Dictionary) as WatchUi.MenuItem{ - if (bLeanDesign) { - return new HomeAssistantViewIconMenuItem(definition, mMenuIcon, mMenuItemAlignment); + if (bRepresentTypesWithIcons) { + return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemAlignment); } else { return new HomeAssistantViewMenuItem(definition); } From bcdcfdc66c41d4da57a0b87bc73237bc9ffc3d19 Mon Sep 17 00:00:00 2001 From: SomeoneOnEarth Date: Sun, 19 Nov 2023 17:04:22 +0100 Subject: [PATCH 11/14] Set showing icons for types as default --- resources/settings/settings.xml | 2 +- source/HomeAssistantMenuItemFactory.mc | 33 +++++++++++++------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/resources/settings/settings.xml b/resources/settings/settings.xml index f8598ba..6482d61 100644 --- a/resources/settings/settings.xml +++ b/resources/settings/settings.xml @@ -45,7 +45,7 @@ WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String }; - bRepresentTypesWithIcons = Application.Properties.getValue("types_representation") as Lang.Boolean; + bRepresentTypesWithLabels = Application.Properties.getValue("types_representation") as Lang.Boolean; var menuItemAlignment = Application.Properties.getValue("menu_alignment") as Lang.Boolean; @@ -76,7 +76,7 @@ class HomeAssistantMenuItemFactory { function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem{ var subLabel = null; - if (bRepresentTypesWithIcons == false){ + if (bRepresentTypesWithLabels == true){ subLabel=mLabelToggle; } @@ -90,17 +90,7 @@ class HomeAssistantMenuItemFactory { } function tap(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null, service as Lang.String or Null) as WatchUi.MenuItem{ - if (bRepresentTypesWithIcons) { - return new HomeAssistantIconMenuItem( - label, - null, - identifier, - service, - mTapTypeIcon, - mMenuItemAlignment - ); - - } else { + if (bRepresentTypesWithLabels) { return new HomeAssistantMenuItem( label, strMenuItemTap, @@ -108,14 +98,23 @@ class HomeAssistantMenuItemFactory { service, mMenuItemAlignment ); + } else { + return new HomeAssistantIconMenuItem( + label, + null, + identifier, + service, + mTapTypeIcon, + mMenuItemAlignment + ); } } function group(definition as Lang.Dictionary) as WatchUi.MenuItem{ - if (bRepresentTypesWithIcons) { - return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemAlignment); - } else { + if (bRepresentTypesWithLabels) { return new HomeAssistantViewMenuItem(definition); + } else { + return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemAlignment); } } } From ce90d9d47ff1b7f4a60c0c281b1da42e18e1d79a Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Mon, 20 Nov 2023 21:32:35 +0000 Subject: [PATCH 12/14] Single HomeAssistantService for all Taps 1. Amended code for a single HomeAssistantService for all 'tap's 2. Removed now redundant GET request for taps without a service now that having a service is enforced. 3. Determined that migrating API code from 'toggle's to the HomeAssistantService is awkward due to the close coupling with other methods in the class. --- source/Alert.mc | 12 ++-- source/ErrorView.mc | 10 +-- source/HomeAssistantApp.mc | 28 ++++---- source/HomeAssistantIconMenuItem.mc | 24 ++++--- source/HomeAssistantMenuItem.mc | 11 +-- source/HomeAssistantMenuItemFactory.mc | 90 +++++++++++++------------ source/HomeAssistantService.mc | 78 ++++++++------------- source/HomeAssistantToggleMenuItem.mc | 22 +++--- source/HomeAssistantView.mc | 5 +- source/HomeAssistantViewIconMenuItem.mc | 2 +- source/HomeAssistantViewMenuItem.mc | 2 +- source/ScalableView.mc | 2 +- 12 files changed, 136 insertions(+), 150 deletions(-) diff --git a/source/Alert.mc b/source/Alert.mc index ce2511c..dc61cb9 100644 --- a/source/Alert.mc +++ b/source/Alert.mc @@ -30,12 +30,12 @@ using Toybox.Timer; const bRadius = 10; class Alert extends WatchUi.View { - hidden var mTimer; - hidden var mTimeout; - hidden var mText; - hidden var mFont; - hidden var mFgcolor; - hidden var mBgcolor; + private var mTimer; + private var mTimeout; + private var mText; + private var mFont; + private var mFgcolor; + private var mBgcolor; function initialize(params as Lang.Dictionary) { View.initialize(); diff --git a/source/ErrorView.mc b/source/ErrorView.mc index 8cc6d02..3b8318a 100644 --- a/source/ErrorView.mc +++ b/source/ErrorView.mc @@ -26,14 +26,14 @@ using Toybox.WatchUi; using Toybox.Communications; class ErrorView extends ScalableView { - hidden const cSettings as Lang.Dictionary = { + private const cSettings as Lang.Dictionary = { :errorIconMargin => 7f }; // Vertical spacing between the top of the face and the error icon - hidden var mErrorIconMargin; - hidden var mText as Lang.String; - hidden var mErrorIcon; - hidden var mTextArea; + private var mErrorIconMargin; + private var mText as Lang.String; + private var mErrorIcon; + private var mTextArea; function initialize(text as Lang.String) { ScalableView.initialize(); diff --git a/source/HomeAssistantApp.mc b/source/HomeAssistantApp.mc index 0b63903..33e4649 100644 --- a/source/HomeAssistantApp.mc +++ b/source/HomeAssistantApp.mc @@ -24,20 +24,20 @@ using Toybox.WatchUi; using Toybox.Application.Properties; class HomeAssistantApp extends Application.AppBase { - hidden var mHaMenu; - hidden var strNoApiKey as Lang.String; - hidden var strNoApiUrl as Lang.String; - hidden var strNoConfigUrl as Lang.String; - hidden var strNoPhone as Lang.String; - hidden var strNoInternet as Lang.String; - hidden var strNoResponse as Lang.String; - hidden var strNoMenu as Lang.String; - hidden var strApiFlood as Lang.String; - hidden var strConfigUrlNotFound as Lang.String; - hidden var strUnhandledHttpErr as Lang.String; - hidden var strTrailingSlashErr as Lang.String; - hidden var mItemsToUpdate; // Array initialised by onReturnFetchMenuConfig() - hidden var mNextItemToUpdate = 0; // Index into the above array + private var mHaMenu; + private var strNoApiKey as Lang.String; + private var strNoApiUrl as Lang.String; + private var strNoConfigUrl as Lang.String; + private var strNoPhone as Lang.String; + private var strNoInternet as Lang.String; + private var strNoResponse as Lang.String; + private var strNoMenu as Lang.String; + private var strApiFlood as Lang.String; + private var strConfigUrlNotFound as Lang.String; + private var strUnhandledHttpErr as Lang.String; + private var strTrailingSlashErr as Lang.String; + private var mItemsToUpdate; // Array initialised by onReturnFetchMenuConfig() + private var mNextItemToUpdate = 0; // Index into the above array function initialize() { AppBase.initialize(); diff --git a/source/HomeAssistantIconMenuItem.mc b/source/HomeAssistantIconMenuItem.mc index 2e74d06..5c20df7 100644 --- a/source/HomeAssistantIconMenuItem.mc +++ b/source/HomeAssistantIconMenuItem.mc @@ -24,17 +24,20 @@ using Toybox.Graphics; using Toybox.Application.Properties; class HomeAssistantIconMenuItem extends WatchUi.IconMenuItem { - hidden var mHomeAssistantService as HomeAssistantService; + private var mHomeAssistantService as HomeAssistantService; + private var mService as Lang.String; +// private var mIdentifier as Lang.String; function initialize( - label as Lang.String or Lang.Symbol, - subLabel as Lang.String or Lang.Symbol or Null, + label as Lang.String or Lang.Symbol, + subLabel as Lang.String or Lang.Symbol or Null, identifier as Lang.Object or Null, - service as Lang.String or Null, - icon as Graphics.BitmapType or WatchUi.Drawable, - options as { + service as Lang.String or Null, + icon as Graphics.BitmapType or WatchUi.Drawable, + options as { :alignment as WatchUi.MenuItem.Alignment - } or Null + } or Null, + haService as HomeAssistantService ) { WatchUi.IconMenuItem.initialize( label, @@ -44,12 +47,13 @@ class HomeAssistantIconMenuItem extends WatchUi.IconMenuItem { options ); - mHomeAssistantService = new HomeAssistantService(service, identifier); - + mHomeAssistantService = haService; + mIdentifier = identifier; + mService = service; } function callService() as Void { - mHomeAssistantService.call(); + mHomeAssistantService.call(mIdentifier as Lang.String, mService); } } diff --git a/source/HomeAssistantMenuItem.mc b/source/HomeAssistantMenuItem.mc index 64c39ed..a2f914f 100644 --- a/source/HomeAssistantMenuItem.mc +++ b/source/HomeAssistantMenuItem.mc @@ -24,7 +24,8 @@ using Toybox.Graphics; using Toybox.Application.Properties; class HomeAssistantMenuItem extends WatchUi.MenuItem { - hidden var mHomeAssistantService as HomeAssistantService; + private var mHomeAssistantService as HomeAssistantService; + private var mService as Lang.String; function initialize( label as Lang.String or Lang.Symbol, @@ -34,7 +35,8 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem { options as { :alignment as WatchUi.MenuItem.Alignment, :icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol - } or Null + } or Null, + haService as HomeAssistantService ) { WatchUi.MenuItem.initialize( label, @@ -43,11 +45,12 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem { options ); - mHomeAssistantService = new HomeAssistantService(service, identifier); + mHomeAssistantService = haService; + mService = service; } function callService() as Void { - mHomeAssistantService.call(); + mHomeAssistantService.call(mIdentifier as Lang.String, mService); } } diff --git a/source/HomeAssistantMenuItemFactory.mc b/source/HomeAssistantMenuItemFactory.mc index 09cd209..faa9303 100644 --- a/source/HomeAssistantMenuItemFactory.mc +++ b/source/HomeAssistantMenuItemFactory.mc @@ -23,57 +23,57 @@ using Toybox.Lang; using Toybox.WatchUi; class HomeAssistantMenuItemFactory { - - private var mMenuItemAlignment; - private var mLabelToggle; - private var strMenuItemTap; - private var bRepresentTypesWithLabels; - - private var mTapTypeIcon; - - private var mGroupTypeIcon; + private var mMenuItemOptions as Lang.Dictionary; + private var mLabelToggle as Lang.Dictionary; + private var strMenuItemTap as Lang.String; + private var bRepresentTypesWithLabels as Lang.Boolean; + private var mTapTypeIcon as WatchUi.Bitmap; + private var mGroupTypeIcon as WatchUi.Bitmap; + private var mHomeAssistantService as HomeAssistantService; private static var instance; private function initialize() { mLabelToggle = { - :enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String, - :disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String - }; - + :enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String, + :disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String + }; bRepresentTypesWithLabels = Application.Properties.getValue("types_representation") as Lang.Boolean; var menuItemAlignment = Application.Properties.getValue("menu_alignment") as Lang.Boolean; - - if(menuItemAlignment == true){ - mMenuItemAlignment = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT}; + if(menuItemAlignment){ + mMenuItemOptions = { + :alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT + }; } else { - mMenuItemAlignment = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT}; + mMenuItemOptions = { + :alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT + }; } - strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap); mTapTypeIcon = new WatchUi.Bitmap({ - :rezId=>$.Rez.Drawables.TapTypeIcon, - :locX=>WatchUi.LAYOUT_HALIGN_CENTER, - :locY=>WatchUi.LAYOUT_VALIGN_CENTER - }); + :rezId => $.Rez.Drawables.TapTypeIcon, + :locX => WatchUi.LAYOUT_HALIGN_CENTER, + :locY => WatchUi.LAYOUT_VALIGN_CENTER + }); mGroupTypeIcon = new WatchUi.Bitmap({ - :rezId=>$.Rez.Drawables.GroupTypeIcon, - :locX=>WatchUi.LAYOUT_HALIGN_CENTER, - :locY=>WatchUi.LAYOUT_VALIGN_CENTER + :rezId => $.Rez.Drawables.GroupTypeIcon, + :locX => WatchUi.LAYOUT_HALIGN_CENTER, + :locY => WatchUi.LAYOUT_VALIGN_CENTER }); + mHomeAssistantService = new HomeAssistantService(); } - static function create() { + static function create() as HomeAssistantMenuItemFactory { if (instance == null) { instance = new HomeAssistantMenuItemFactory(); } return instance; } - function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem{ + function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem { var subLabel = null; if (bRepresentTypesWithLabels == true){ @@ -85,36 +85,38 @@ class HomeAssistantMenuItemFactory { subLabel, identifier, false, - mMenuItemAlignment + mMenuItemOptions ); } - function tap(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null, service as Lang.String or Null) as WatchUi.MenuItem{ + function tap(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null, service as Lang.String or Null) as WatchUi.MenuItem { if (bRepresentTypesWithLabels) { return new HomeAssistantMenuItem( - label, - strMenuItemTap, - identifier, - service, - mMenuItemAlignment - ); + label, + strMenuItemTap, + identifier, + service, + mMenuItemOptions, + mHomeAssistantService + ); } else { return new HomeAssistantIconMenuItem( - label, - null, - identifier, - service, - mTapTypeIcon, - mMenuItemAlignment - ); + label, + null, + identifier, + service, + mTapTypeIcon, + mMenuItemOptions, + mHomeAssistantService + ); } } - function group(definition as Lang.Dictionary) as WatchUi.MenuItem{ + function group(definition as Lang.Dictionary) as WatchUi.MenuItem { if (bRepresentTypesWithLabels) { return new HomeAssistantViewMenuItem(definition); } else { - return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemAlignment); + return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemOptions); } } } diff --git a/source/HomeAssistantService.mc b/source/HomeAssistantService.mc index 20999d2..2f371a9 100644 --- a/source/HomeAssistantService.mc +++ b/source/HomeAssistantService.mc @@ -23,21 +23,16 @@ using Toybox.WatchUi; using Toybox.Graphics; using Toybox.Application.Properties; -class HomeAssistantService{ - hidden var mApiKey as Lang.String; - hidden var strNoPhone as Lang.String; - hidden var strNoInternet as Lang.String; - hidden var strNoResponse as Lang.String; - hidden var strApiFlood as Lang.String; - hidden var strApiUrlNotFound as Lang.String; - hidden var strUnhandledHttpErr as Lang.String; - hidden var mService as Lang.String; - hidden var mIdentifier as Lang.Object; +class HomeAssistantService { + private var mApiKey as Lang.String; + private var strNoPhone as Lang.String; + private var strNoInternet as Lang.String; + private var strNoResponse as Lang.String; + private var strApiFlood as Lang.String; + private var strApiUrlNotFound as Lang.String; + private var strUnhandledHttpErr as Lang.String; - function initialize( - service as Lang.String or Null, - identifier as Lang.Object or Null - ) { + function initialize() { strNoPhone = WatchUi.loadResource($.Rez.Strings.NoPhone); strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet); strNoResponse = WatchUi.loadResource($.Rez.Strings.NoResponse); @@ -45,14 +40,13 @@ class HomeAssistantService{ strApiUrlNotFound = WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound); strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr); mApiKey = Properties.getValue("api_key"); - mService = service; - mIdentifier = identifier; } // Callback function after completing the POST request to call a service. // - function onReturnCall(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { - if (Globals.scDebug) { + function onReturnCall(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String, context as Lang.Object) as Void { + var identifier = context as Lang.String; + if (Globals.scDebug) { System.println("HomeAssistantService onReturnCall() Response Code: " + responseCode); System.println("HomeAssistantService onReturnCall() Response Data: " + data); } @@ -86,7 +80,7 @@ class HomeAssistantService{ var d = data as Lang.Array; var toast = "Executed"; for(var i = 0; i < d.size(); i++) { - if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) { + if ((d[i].get("entity_id") as Lang.String).equals(identifier)) { toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String; } } @@ -109,14 +103,15 @@ class HomeAssistantService{ } } - function call() as Void { + function call(identifier as Lang.String, service as Lang.String) as Void { var options = { :method => Communications.HTTP_REQUEST_METHOD_POST, :headers => { "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, "Authorization" => "Bearer " + mApiKey }, - :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON + :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON, + :context => identifier }; if (! System.getDeviceSettings().phoneConnected) { if (Globals.scDebug) { @@ -129,36 +124,19 @@ class HomeAssistantService{ } WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); } else { - // Updated SDK and got a new error - // ERROR: venu: Cannot find symbol ':substring' on type 'PolyType'. - var id = mIdentifier as Lang.String; - if (mService == null) { - var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + id.substring(0, id.find(".")) + "/" + id.substring(id.find(".")+1, null); - if (Globals.scDebug) { - System.println("HomeAssistantService call() URL=" + url); - System.println("HomeAssistantService call() mIdentifier=" + mIdentifier); - } - Communications.makeWebRequest( - url, - null, - options, - method(:onReturnCall) - ); - } else { - var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null); - if (Globals.scDebug) { - System.println("HomeAssistantService call() URL=" + url); - System.println("HomeAssistantService call() mService=" + mService); - } - Communications.makeWebRequest( - url, - { - "entity_id" => id - }, - options, - method(:onReturnCall) - ); + var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + service.substring(0, service.find(".")) + "/" + service.substring(service.find(".")+1, null); + if (Globals.scDebug) { + System.println("HomeAssistantService call() URL=" + url); + System.println("HomeAssistantService call() service=" + service); } + Communications.makeWebRequest( + url, + { + "entity_id" => identifier + }, + options, + method(:onReturnCall) + ); if (Attention has :vibrate) { Attention.vibrate([ new Attention.VibeProfile(50, 100), // On for 100ms diff --git a/source/HomeAssistantToggleMenuItem.mc b/source/HomeAssistantToggleMenuItem.mc index 538ae5a..a92537f 100644 --- a/source/HomeAssistantToggleMenuItem.mc +++ b/source/HomeAssistantToggleMenuItem.mc @@ -25,23 +25,23 @@ using Toybox.Application.Properties; using Toybox.Timer; class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { - hidden var mApiKey as Lang.String; - hidden var strNoPhone as Lang.String; - hidden var strNoInternet as Lang.String; - hidden var strNoResponse as Lang.String; - hidden var strApiFlood as Lang.String; - hidden var strApiUrlNotFound as Lang.String; - hidden var strUnhandledHttpErr as Lang.String; + private var mApiKey as Lang.String; + private var strNoPhone as Lang.String; + private var strNoInternet as Lang.String; + private var strNoResponse as Lang.String; + private var strApiFlood as Lang.String; + private var strApiUrlNotFound as Lang.String; + private var strUnhandledHttpErr as Lang.String; function initialize( - label as Lang.String or Lang.Symbol, - subLabel as Lang.String or Lang.Symbol or { + label as Lang.String or Lang.Symbol, + subLabel as Lang.String or Lang.Symbol or { :enabled as Lang.String or Lang.Symbol or Null, :disabled as Lang.String or Lang.Symbol or Null } or Null, identifier, - enabled as Lang.Boolean, - options as { + enabled as Lang.Boolean, + options as { :alignment as WatchUi.MenuItem.Alignment, :icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol } or Null diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc index 7a2318c..51f9919 100644 --- a/source/HomeAssistantView.mc +++ b/source/HomeAssistantView.mc @@ -24,10 +24,9 @@ using Toybox.Graphics; using Toybox.WatchUi; class HomeAssistantView extends WatchUi.Menu2 { - // List of items that need to have their status updated periodically - hidden var mListToggleItems = []; - hidden var mListMenuItems = []; + private var mListToggleItems = []; + private var mListMenuItems = []; function initialize( definition as Lang.Dictionary, diff --git a/source/HomeAssistantViewIconMenuItem.mc b/source/HomeAssistantViewIconMenuItem.mc index a46e4c8..382444d 100644 --- a/source/HomeAssistantViewIconMenuItem.mc +++ b/source/HomeAssistantViewIconMenuItem.mc @@ -22,7 +22,7 @@ using Toybox.Lang; using Toybox.WatchUi; class HomeAssistantViewIconMenuItem extends WatchUi.IconMenuItem { - hidden var mMenu as HomeAssistantView; + private var mMenu as HomeAssistantView; function initialize(definition as Lang.Dictionary, icon as WatchUi.Drawable, options as { :alignment as WatchUi.MenuItem.Alignment diff --git a/source/HomeAssistantViewMenuItem.mc b/source/HomeAssistantViewMenuItem.mc index 34e3a67..c430b4b 100644 --- a/source/HomeAssistantViewMenuItem.mc +++ b/source/HomeAssistantViewMenuItem.mc @@ -22,7 +22,7 @@ using Toybox.Lang; using Toybox.WatchUi; class HomeAssistantViewMenuItem extends WatchUi.MenuItem { - hidden var mMenu as HomeAssistantView; + private var mMenu as HomeAssistantView; function initialize(definition as Lang.Dictionary) { // definitions.get(...) are Strings here as they have been checked by HomeAssistantView first diff --git a/source/ScalableView.mc b/source/ScalableView.mc index 7744d7a..febf84d 100644 --- a/source/ScalableView.mc +++ b/source/ScalableView.mc @@ -23,7 +23,7 @@ using Toybox.WatchUi; using Toybox.Math; class ScalableView extends WatchUi.View { - hidden var mScreenWidth; + private var mScreenWidth; function initialize() { View.initialize(); From 71c1ccc229b912b09d5d2bac70ed05f632054e01 Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Tue, 21 Nov 2023 17:58:18 +0000 Subject: [PATCH 13/14] Code review comment Removed debug Fixed manifest App ID for release --- manifest.xml | 2 +- source/HomeAssistantToggleMenuItem.mc | 1 - source/HomeAssistantView.mc | 4 +--- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/manifest.xml b/manifest.xml index 6c011ed..3f2a12b 100644 --- a/manifest.xml +++ b/manifest.xml @@ -21,7 +21,7 @@ Use "Monkey C: Edit Application" from the Visual Studio Code command palette to update the application attributes. --> - +