Service call for tap item

This commit is contained in:
Joseph Abbey
2023-11-09 21:13:09 +00:00
parent 3c0b39ad3e
commit 5afb08d096
3 changed files with 53 additions and 17 deletions

View File

@ -14,7 +14,7 @@
//
// Description:
//
// Menu button that triggers a script.
// Menu button that triggers a service.
//
//-----------------------------------------------------------------------------------
@ -26,17 +26,20 @@ using Toybox.Application.Properties;
class HomeAssistantMenuItem extends WatchUi.MenuItem {
hidden var api_key = Properties.getValue("api_key");
hidden var strNoInternet as Lang.String;
hidden var mService as Lang.String or Null;
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,
options as {
:alignment as WatchUi.MenuItem.Alignment,
:icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol
} or Null
) {
strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet);
mService = service;
WatchUi.MenuItem.initialize(
label,
subLabel,
@ -77,17 +80,33 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
};
if (System.getDeviceSettings().phoneConnected && System.getDeviceSettings().connectionAvailable) {
var url = Properties.getValue("api_url") + "/services/" + mIdentifier.substring(0, mIdentifier.find(".")) + "/" + mIdentifier.substring(mIdentifier.find(".")+1, null);
if (Globals.debug) {
System.println("URL=" + url);
System.println("mIdentifier=" + mIdentifier);
if (mService == null) {
var url = Properties.getValue("api_url") + "/services/" + mIdentifier.substring(0, mIdentifier.find(".")) + "/" + mIdentifier.substring(mIdentifier.find(".")+1, null);
if (Globals.debug) {
System.println("URL=" + url);
System.println("mIdentifier=" + mIdentifier);
}
Communications.makeWebRequest(
url,
null,
options,
method(:onReturnExecScript)
);
} else {
var url = Properties.getValue("api_url") + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null);
if (Globals.debug) {
System.println("URL=" + url);
System.println("mIdentifier=" + mIdentifier);
}
Communications.makeWebRequest(
url,
{
"entity_id" => mIdentifier
},
options,
method(:onReturnSetState)
);
}
Communications.makeWebRequest(
url,
null,
options,
method(:onReturnExecScript)
);
} else {
if (Globals.debug) {
System.println("HomeAssistantMenuItem Note - executeScript(): No Internet connection, skipping API call.");

View File

@ -50,9 +50,10 @@ class HomeAssistantView extends WatchUi.Menu2 {
var items = definition.get("items") as Lang.Dictionary;
for(var i = 0; i < items.size(); i++) {
var type = items[i].get("type") as Lang.String or Null;
var name = items[i].get("name") as Lang.String or Null;
var entity = items[i].get("entity") as Lang.String or Null;
var type = items[i].get("type") as Lang.String or Null;
var name = items[i].get("name") as Lang.String or Null;
var entity = items[i].get("entity") as Lang.String or Null;
var service = items[i].get("service") as Lang.String or Null;
if (type != null && name != null && entity != null) {
if (type.equals("toggle")) {
addItem(
@ -70,6 +71,7 @@ class HomeAssistantView extends WatchUi.Menu2 {
name,
strMenuItemTap,
entity,
service,
null
)
);