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

@ -8,12 +8,23 @@
"additionalProperties": false "additionalProperties": false
}, },
"$defs": { "$defs": {
"item": { "toggle": {
"type": "object", "type": "object",
"properties": { "properties": {
"entity": { "$ref": "#/$defs/entity" }, "entity": { "$ref": "#/$defs/entity" },
"name": { "type": "string" }, "name": { "type": "string" },
"type": { "enum": ["toggle", "tap"] } "type": { "const": "toggle" }
},
"required": ["entity", "name", "type"],
"additionalProperties": false
},
"tap": {
"type": "object",
"properties": {
"entity": { "$ref": "#/$defs/entity" },
"name": { "type": "string" },
"type": { "const": "tap" },
"service": { "$ref": "#/$defs/entity" }
}, },
"required": ["entity", "name", "type"], "required": ["entity", "name", "type"],
"additionalProperties": false "additionalProperties": false
@ -33,7 +44,11 @@
"items": { "items": {
"type": "array", "type": "array",
"items": { "items": {
"oneOf": [{ "$ref": "#/$defs/item" }, { "$ref": "#/$defs/menu" }] "oneOf": [
{ "$ref": "#/$defs/toggle" },
{ "$ref": "#/$defs/tap" },
{ "$ref": "#/$defs/menu" }
]
} }
}, },
"entity": { "entity": {

View File

@ -14,7 +14,7 @@
// //
// Description: // 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 { class HomeAssistantMenuItem extends WatchUi.MenuItem {
hidden var api_key = Properties.getValue("api_key"); hidden var api_key = Properties.getValue("api_key");
hidden var strNoInternet as Lang.String; hidden var strNoInternet as Lang.String;
hidden var mService as Lang.String or Null;
function initialize( function initialize(
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
subLabel as Lang.String or Lang.Symbol or Null, subLabel as Lang.String or Lang.Symbol or Null,
identifier as Lang.Object or Null, identifier as Lang.Object or Null,
service as Lang.String or Null,
options as { options as {
:alignment as WatchUi.MenuItem.Alignment, :alignment as WatchUi.MenuItem.Alignment,
:icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol :icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol
} or Null } or Null
) { ) {
strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet); strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet);
mService = service;
WatchUi.MenuItem.initialize( WatchUi.MenuItem.initialize(
label, label,
subLabel, subLabel,
@ -77,17 +80,33 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
}; };
if (System.getDeviceSettings().phoneConnected && System.getDeviceSettings().connectionAvailable) { 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 (mService == null) {
if (Globals.debug) { var url = Properties.getValue("api_url") + "/services/" + mIdentifier.substring(0, mIdentifier.find(".")) + "/" + mIdentifier.substring(mIdentifier.find(".")+1, null);
System.println("URL=" + url); if (Globals.debug) {
System.println("mIdentifier=" + mIdentifier); 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 { } else {
if (Globals.debug) { if (Globals.debug) {
System.println("HomeAssistantMenuItem Note - executeScript(): No Internet connection, skipping API call."); 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; var items = definition.get("items") as Lang.Dictionary;
for(var i = 0; i < items.size(); i++) { for(var i = 0; i < items.size(); i++) {
var type = items[i].get("type") 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 name = items[i].get("name") as Lang.String or Null;
var entity = items[i].get("entity") 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 != null && name != null && entity != null) {
if (type.equals("toggle")) { if (type.equals("toggle")) {
addItem( addItem(
@ -70,6 +71,7 @@ class HomeAssistantView extends WatchUi.Menu2 {
name, name,
strMenuItemTap, strMenuItemTap,
entity, entity,
service,
null null
) )
); );