diff --git a/README.md b/README.md index a7463dc..68476f6 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ Example schema as shown in the images: { "entity": "script.food_on_table", "name": "Food is Ready!", - "type": "tap" + "type": "tap", + "service" : "script.turn_on" }, { "entity": "light.bedside_light_switch", @@ -79,6 +80,12 @@ Example schema as shown in the images: "name": "Garage Door Check", "type": "toggle" }, + { + "entity": "automation.turn_off_usb_chargers", + "name": "Turn off USBs", + "type": "tap", + "service" : "automation.trigger" + }, { "entity": "scene.tv_light", "name": "TV Lights Scene", @@ -99,6 +106,18 @@ The example above illustrates how to configure: * Service invocation, e.g. Scene setting, (tap) * A sub-menu to open (tap) +The example JSON shows an example usage of each of these Home Assistance entity types. Presently, an automation is the only one that can be either a 'tap' or a 'toggle'. + +| HA Type | Tap | Toggle | +|------------|:---:|:------:| +| Switch | N | Y | +| Light | N | Y | +| Automation | Y | Y | +| Script | Y | N | +| Scene | Y | N | + +NB. All 'tap' items must specify a 'service' tag. + Possible future extensions might include specifying the alternative texts to use instead of "On" and "Off", e.g. "Locked" and "Unlocked" (but wouldn't having locks operated from your watch be a security concern ;-)) The [schema](https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json) is checked by using a URL directly back to this GitHub source repository, so you do not need to install that file. You can just copy & paste your entity names from the YAML configuration files used to configure Home Assistant. With a submenu, there's a difference between "title" and "name". The "name" goes on the menu item, and the "title" at the head of the submenu. If your dashboard definition fails to meet the schema, the application will simply drop items with the wrong field names without warning. diff --git a/config.schema.json b/config.schema.json index 99e59b6..1edd1e9 100644 --- a/config.schema.json +++ b/config.schema.json @@ -26,7 +26,7 @@ "type": { "const": "tap" }, "service": { "$ref": "#/$defs/entity" } }, - "required": ["entity", "name", "type"], + "required": ["entity", "name", "type", "service"], "additionalProperties": false }, "menu": { diff --git a/source/HomeAssistantMenuItem.mc b/source/HomeAssistantMenuItem.mc index bedcb82..d72ada3 100644 --- a/source/HomeAssistantMenuItem.mc +++ b/source/HomeAssistantMenuItem.mc @@ -77,36 +77,35 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem { } WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP); } else if (responseCode == 200) { - var d = data as Lang.Array; + if (Globals.scDebug) { + System.println("HomeAssistantMenuItem onReturnExecScript(): Service executed."); + } + var d = data as Lang.Array; + var toast = "Executed"; for(var i = 0; i < d.size(); i++) { if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) { - if (Globals.scDebug) { - System.println("HomeAssistantMenuItem onReturnExecScript(): Correct script executed."); - } - if (WatchUi has :showToast) { - WatchUi.showToast( - (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String, - null - ); - } - if (Attention has :vibrate) { - Attention.vibrate([ - new Attention.VibeProfile(50, 100), // On for 100ms - new Attention.VibeProfile( 0, 100), // Off for 100ms - new Attention.VibeProfile(50, 100) // On for 100ms - ]); - } - if (!(WatchUi has :showToast) && !(Attention has :vibrate)) { - new Alert({ - :timeout => Globals.scAlertTimeout, - :font => Graphics.FONT_MEDIUM, - :text => (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String, - :fgcolor => Graphics.COLOR_WHITE, - :bgcolor => Graphics.COLOR_BLACK - }).pushView(WatchUi.SLIDE_IMMEDIATE); - } + toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String; } } + if (WatchUi has :showToast) { + WatchUi.showToast(toast, null); + } + if (Attention has :vibrate) { + Attention.vibrate([ + new Attention.VibeProfile(50, 100), // On for 100ms + new Attention.VibeProfile( 0, 100), // Off for 100ms + new Attention.VibeProfile(50, 100) // On for 100ms + ]); + } + if (!(WatchUi has :showToast) && !(Attention has :vibrate)) { + new Alert({ + :timeout => Globals.scAlertTimeout, + :font => Graphics.FONT_MEDIUM, + :text => toast, + :fgcolor => Graphics.COLOR_WHITE, + :bgcolor => Graphics.COLOR_BLACK + }).pushView(WatchUi.SLIDE_IMMEDIATE); + } } else { if (Globals.scDebug) { System.println("HomeAssistantMenuItem onReturnExecScript(): Unhandled HTTP response code = " + responseCode); @@ -144,7 +143,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem { var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null); if (Globals.scDebug) { System.println("HomeAssistantMenuItem execScript() URL=" + url); - System.println("HomeAssistantMenuItem execScript() mIdentifier=" + mIdentifier); + System.println("HomeAssistantMenuItem execScript() mService=" + mService); } Communications.makeWebRequest( url,