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
},
"$defs": {
"item": {
"toggle": {
"type": "object",
"properties": {
"entity": { "$ref": "#/$defs/entity" },
"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"],
"additionalProperties": false
@ -33,7 +44,11 @@
"items": {
"type": "array",
"items": {
"oneOf": [{ "$ref": "#/$defs/item" }, { "$ref": "#/$defs/menu" }]
"oneOf": [
{ "$ref": "#/$defs/toggle" },
{ "$ref": "#/$defs/tap" },
{ "$ref": "#/$defs/menu" }
]
}
},
"entity": {

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,6 +80,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
};
if (System.getDeviceSettings().phoneConnected && System.getDeviceSettings().connectionAvailable) {
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);
@ -88,6 +92,21 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
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)
);
}
} else {
if (Globals.debug) {
System.println("HomeAssistantMenuItem Note - executeScript(): No Internet connection, skipping API call.");

View File

@ -53,6 +53,7 @@ class HomeAssistantView extends WatchUi.Menu2 {
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
)
);