simplify delegate, toggle switch for empty response body

This commit is contained in:
Vincent Elger Zwanenburg
2025-07-11 22:44:26 +01:00
parent b45f02ef7b
commit ac899ff784
4 changed files with 17 additions and 32 deletions

View File

@ -141,7 +141,7 @@ class HomeAssistantService {
:service => service, :service => service,
:data => data, :data => data,
:exit => exit, :exit => exit,
}, null), }),
WatchUi.SLIDE_LEFT WatchUi.SLIDE_LEFT
); );
} else if (! phoneConnected) { } else if (! phoneConnected) {

View File

@ -61,7 +61,7 @@ class HomeAssistantSyncDelegate extends Communications.SyncDelegate {
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
"Authorization" => "Bearer " + Settings.getApiKey() "Authorization" => "Bearer " + Settings.getApiKey()
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
}, },
method(:haCallback) method(:haCallback)
); );
@ -73,7 +73,7 @@ class HomeAssistantSyncDelegate extends Communications.SyncDelegate {
if (code == 200) { if (code == 200) {
syncError = null; syncError = null;
if (WifiLteExecutionConfirmDelegate.mCommandData[:type].equals("entity")) { if (WifiLteExecutionConfirmDelegate.mCommandData[:type].equals("entity")) {
var callbackMethod = WifiLteExecutionConfirmDelegate.mCommandData[:callback]; var callbackMethod = WifiLteExecutionConfirmDelegate.mCommandData[:callback];
if (callbackMethod != null) { if (callbackMethod != null) {
var d = data as Lang.Array; var d = data as Lang.Array;
callbackMethod.invoke(d); callbackMethod.invoke(d);
@ -107,6 +107,5 @@ class HomeAssistantSyncDelegate extends Communications.SyncDelegate {
Communications.cancelAllRequests(); Communications.cancelAllRequests();
Communications.notifySyncComplete(syncError); Communications.notifySyncComplete(syncError);
} }
} }

View File

@ -218,12 +218,19 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
//! @param data An array of dictionaries, each representing a Home Assistant entity state. //! @param data An array of dictionaries, each representing a Home Assistant entity state.
// //
function setToggleStateWithData(data as Lang.Array) { function setToggleStateWithData(data as Lang.Array) {
for(var i = 0; i < data.size(); i++) { // if there's no response body, let's assume that what we did, happened, and flip the toggle
if ((data[i].get("entity_id") as Lang.String).equals(mData.get("entity_id"))) { if (data.size() == 0) {
var state = data[i].get("state") as Lang.String; setEnabled(!isEnabled());
// System.println((d[i].get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state); }
setUiToggle(state);
WatchUi.requestUpdate(); 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, :data => mData,
:callback => method(:setToggleStateWithData), :callback => method(:setToggleStateWithData),
:exit => mExit, :exit => mExit,
}, {
:confirmMethod => method(:onConfirm),
:state => !isEnabled(),
}), }),
WatchUi.SLIDE_LEFT WatchUi.SLIDE_LEFT
); );

View File

@ -16,12 +16,9 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate {
:exit as Lang.Boolean :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 mHasToast as Lang.Boolean = false;
private var mTimer as Timer.Timer or Null; private var mTimer as Timer.Timer or Null;
//! Initializes a confirmation delegate to confirm a Wi-Fi or LTE command exection //! Initializes a confirmation delegate to confirm a Wi-Fi or LTE command exection
//! //!
//! @param options A dictionary describing the command to be executed: //! @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<data as Dictionary>) to handle the response. //! - callback: (For type `"entity"`) A callback method (Method<data as Dictionary>) to handle the response.
//! - data: (Optional) A dictionary of data to send with the request. //! - data: (Optional) A dictionary of data to send with the request.
//! = exit: Boolean: true to exit after running command. //! = 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 { function initialize(cOptions as {
:type as Lang.String, :type as Lang.String,
:service as Lang.String or Null, :service as Lang.String or Null,
@ -42,10 +35,7 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate {
:url as Lang.String or Null, :url as Lang.String or Null,
:callback as Lang.Method or Null, :callback as Lang.Method or Null,
:exit as Lang.Boolean, :exit as Lang.Boolean,
}, toggleItem as { }) {
:confirmMethod as Lang.Method,
:state as Lang.Boolean
} or Null) {
ConfirmationDelegate.initialize(); ConfirmationDelegate.initialize();
if (WatchUi has :showToast) { if (WatchUi has :showToast) {
@ -60,10 +50,6 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate {
:callback => cOptions[:callback], :callback => cOptions[:callback],
:exit => cOptions[:exit] :exit => cOptions[:exit]
}; };
if (toggleItem != null) {
mToggleMethod = toggleItem[:confirmMethod];
mToggleState = toggleItem[:state];
}
var timeout = Settings.getConfirmTimeout(); // ms var timeout = Settings.getConfirmTimeout(); // ms
if (timeout > 0) { if (timeout > 0) {
@ -83,9 +69,6 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate {
} }
if (response == WatchUi.CONFIRM_YES) { if (response == WatchUi.CONFIRM_YES) {
if (mToggleMethod != null) {
mToggleMethod.invoke(mToggleState);
}
trySync(); trySync();
} }
return true; return true;
@ -93,7 +76,6 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate {
//! Initiates a bulk sync process to execute a command, if connections are available //! Initiates a bulk sync process to execute a command, if connections are available
private function trySync() as Void { private function trySync() as Void {
WatchUi.popView(WatchUi.SLIDE_IMMEDIATE);
var connectionInfo = System.getDeviceSettings().connectionInfo; var connectionInfo = System.getDeviceSettings().connectionInfo;
var keys = connectionInfo.keys(); var keys = connectionInfo.keys();
var possibleConnection = false; var possibleConnection = false;