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,
:data => data,
:exit => exit,
}, null),
}),
WatchUi.SLIDE_LEFT
);
} else if (! phoneConnected) {

View File

@ -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);
}
}

View File

@ -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
);

View File

@ -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<data as Dictionary>) 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;