From ac899ff78420c8cbcab89819f4dd7e8b971b9961 Mon Sep 17 00:00:00 2001 From: Vincent Elger Zwanenburg Date: Fri, 11 Jul 2025 22:44:26 +0100 Subject: [PATCH] simplify delegate, toggle switch for empty response body --- source/HomeAssistantService.mc | 2 +- source/HomeAssistantSyncDelegate.mc | 5 ++--- source/HomeAssistantToggleMenuItem.mc | 22 +++++++++++++--------- source/WifiLteExecutionConfirmDelegate.mc | 20 +------------------- 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/source/HomeAssistantService.mc b/source/HomeAssistantService.mc index eca2509..53dbf80 100644 --- a/source/HomeAssistantService.mc +++ b/source/HomeAssistantService.mc @@ -141,7 +141,7 @@ class HomeAssistantService { :service => service, :data => data, :exit => exit, - }, null), + }), WatchUi.SLIDE_LEFT ); } else if (! phoneConnected) { diff --git a/source/HomeAssistantSyncDelegate.mc b/source/HomeAssistantSyncDelegate.mc index f8ff6f6..92161d8 100644 --- a/source/HomeAssistantSyncDelegate.mc +++ b/source/HomeAssistantSyncDelegate.mc @@ -61,7 +61,7 @@ class HomeAssistantSyncDelegate extends Communications.SyncDelegate { "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, "Authorization" => "Bearer " + Settings.getApiKey() }, - :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON + :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON, }, method(:haCallback) ); @@ -73,7 +73,7 @@ class HomeAssistantSyncDelegate extends Communications.SyncDelegate { if (code == 200) { syncError = null; if (WifiLteExecutionConfirmDelegate.mCommandData[:type].equals("entity")) { - var callbackMethod = WifiLteExecutionConfirmDelegate.mCommandData[:callback]; + var callbackMethod = WifiLteExecutionConfirmDelegate.mCommandData[:callback]; if (callbackMethod != null) { var d = data as Lang.Array; callbackMethod.invoke(d); @@ -107,6 +107,5 @@ class HomeAssistantSyncDelegate extends Communications.SyncDelegate { Communications.cancelAllRequests(); Communications.notifySyncComplete(syncError); - } } diff --git a/source/HomeAssistantToggleMenuItem.mc b/source/HomeAssistantToggleMenuItem.mc index be2e51c..6554598 100644 --- a/source/HomeAssistantToggleMenuItem.mc +++ b/source/HomeAssistantToggleMenuItem.mc @@ -218,12 +218,19 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { //! @param data An array of dictionaries, each representing a Home Assistant entity state. // function setToggleStateWithData(data as Lang.Array) { - for(var i = 0; i < data.size(); i++) { - if ((data[i].get("entity_id") as Lang.String).equals(mData.get("entity_id"))) { - var state = data[i].get("state") as Lang.String; - // System.println((d[i].get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state); - setUiToggle(state); - WatchUi.requestUpdate(); + // if there's no response body, let's assume that what we did, happened, and flip the toggle + if (data.size() == 0) { + setEnabled(!isEnabled()); + } + + else { + for(var i = 0; i < data.size(); i++) { + if ((data[i].get("entity_id") as Lang.String).equals(mData.get("entity_id"))) { + var state = data[i].get("state") as Lang.String; + // System.println((d[i].get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state); + setUiToggle(state); + WatchUi.requestUpdate(); + } } } } @@ -268,9 +275,6 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { :data => mData, :callback => method(:setToggleStateWithData), :exit => mExit, - }, { - :confirmMethod => method(:onConfirm), - :state => !isEnabled(), }), WatchUi.SLIDE_LEFT ); diff --git a/source/WifiLteExecutionConfirmDelegate.mc b/source/WifiLteExecutionConfirmDelegate.mc index 0a4b5f4..fc95304 100644 --- a/source/WifiLteExecutionConfirmDelegate.mc +++ b/source/WifiLteExecutionConfirmDelegate.mc @@ -16,12 +16,9 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { :exit as Lang.Boolean }; - private var mToggleMethod as Method(b as Lang.Boolean) as Void or Null; - private var mToggleState as Lang.Boolean or Null; private var mHasToast as Lang.Boolean = false; private var mTimer as Timer.Timer or Null; - //! Initializes a confirmation delegate to confirm a Wi-Fi or LTE command exection //! //! @param options A dictionary describing the command to be executed: @@ -31,10 +28,6 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { //! - callback: (For type `"entity"`) A callback method (Method) to handle the response. //! - data: (Optional) A dictionary of data to send with the request. //! = exit: Boolean: true to exit after running command. - //! - //! @param toggleItem Optional toggle state information: - //! - confirmMethod: A method to call after confirmation. - //! - state: The state (boolean) that will be passed to the confirmMethod. function initialize(cOptions as { :type as Lang.String, :service as Lang.String or Null, @@ -42,10 +35,7 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { :url as Lang.String or Null, :callback as Lang.Method or Null, :exit as Lang.Boolean, - }, toggleItem as { - :confirmMethod as Lang.Method, - :state as Lang.Boolean - } or Null) { + }) { ConfirmationDelegate.initialize(); if (WatchUi has :showToast) { @@ -60,10 +50,6 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { :callback => cOptions[:callback], :exit => cOptions[:exit] }; - if (toggleItem != null) { - mToggleMethod = toggleItem[:confirmMethod]; - mToggleState = toggleItem[:state]; - } var timeout = Settings.getConfirmTimeout(); // ms if (timeout > 0) { @@ -83,9 +69,6 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { } if (response == WatchUi.CONFIRM_YES) { - if (mToggleMethod != null) { - mToggleMethod.invoke(mToggleState); - } trySync(); } return true; @@ -93,7 +76,6 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { //! Initiates a bulk sync process to execute a command, if connections are available private function trySync() as Void { - WatchUi.popView(WatchUi.SLIDE_IMMEDIATE); var connectionInfo = System.getDeviceSettings().connectionInfo; var keys = connectionInfo.keys(); var possibleConnection = false;